WPDIR
Everything and everyone WordPress

Declare PHP variable in functions.php and use it with theme files?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #442 Reply
    Alexander
    Guest

    How can I use static PHP variables in functions.php and use them for other coding in the WordPress template theme files

    #443 Reply
    Andrea
    Guest

    Easiest way is global variables:

    https://wordpress.stackexchange.com/questions/151319/add-a-variable-in-functions-php-and-call-it-in-single-php

    In your functions.php

    global $welcome;
    $welcome = "hey guys";

    In your theme files

    global $welcome;
        echo $welcome;
    #464 Reply
    Adam
    Guest

    And yah, you have to put the global twice, which looks goofy, but it’s required.

    #585 Reply
    Elijah
    Guest

    But using global vars is not recommended, it’s bad practice

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Declare PHP variable in functions.php and use it with theme files?