• Resolved piopruua

    (@piopruua)


    For example, if I add this code to functions.php:

    $mysite_address_url="http://my-site.com";

    And then add this to one of my .php template files(like index.php or header.php):

    <?php echo($mysite_address_url); ?>

    It won’t work. Why is this happens and how can I achieve this with wordpress please?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Thread Starter piopruua

    (@piopruua)

    So, with my example, I should do this, right(?):

    function my_global_vars() {
    
        global $myglobvar;
        $myglobvar= array(
    
            'mysite_address_url'  => get_query_var('http://my-site.com'),
    
        );
    
    }
    add_action( 'parse_query', 'my_global_vars' );

    And what then?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Then it’s available in your template under:

    $GLOBALS['mysite_address_url']

    Thread Starter piopruua

    (@piopruua)

    Like this(?):
    <?php echo($GLOBALS[‘mysite_address_url’]); ?>

    And another question please, is it safe to use this globals?

    And thank you for helping me!

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Yes try that

    Thread Starter piopruua

    (@piopruua)

    I just tried it and it is not working.
    I tried the above and did this in my index.php:
    <a href="<?php echo($GLOBALS['mysite_address_url']); ?>">TEST</a>

    and I got this instead:

    <a href="<br />
    <b>Notice</b>:  Undefined index: mysite_address_url in <b>/home/www/blablabla/wp-content/themes/blabla/index.php</b> on line <b>blabla</b><br />
    ">TEST</a>

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Oh sorry, it’s meant to be this:

    <?php echo($GLOBALS['myglobvar']['mysite_address_url']); ?>

    Thread Starter piopruua

    (@piopruua)

    Thanks, Andrew! It worked! But can you please tell me how safe is this to use global variables or there’s nothing to worry about it?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Have you read something that says it’s insecure? I couldn’t find anything about global variables being insecure, but I don’t know

    Thread Starter piopruua

    (@piopruua)

    Thanks once again, Andrew!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘how can I include some custom variable from functions.php?’ is closed to new replies.