• nat3

    (@roboticsguy)


    I’ve done a lot of modifications to my WP site, mostly putting snippets of php code in widgets, pages, posts, and the nav bar. Most of these code snippets grab a GET variable from the URL, query a database to see if it matches something, and then depending on the query result changes what the snippet does to that part of the page.

    It recently occurred to me that it would be really great if I could just do this once, instead of doing the exact same thing many times on the same page. Is there some place I could put my php code so that the result would be available to php code in any widget, post, page, or wordpress php file?

    Thanks a lot for your advice 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • shortcodes can work in all the places you mention (well, nav would require some special coding)

    Here’s a good example of how it works
    http://aaron.jorb.in/blog/2010/02/wordpress-shortcodes-a-how-to-by-example/

    Codex: http://codex.ww.wp.xz.cn/Shortcode_API
    They can be quite elaborate – the WordPress built in gallery system is a shortcode

    Thread Starter nat3

    (@roboticsguy)

    Thanks for your reply!

    If I put my php code in a shortcode function, then it’s still going to run many times for the same page, right? Maybe I’m missing something.. I’m just looking for a way to have the code run once and then a few variables from the code be accessible to whatever else loads on the page.

    I misunderstood what you were asking for. If the same code is running many times on the same page, then run the code once at the top of the page. Put its output in an array that can be accessed elsewhere on the page and have the rest of the code on the page access the values in the array rather than rerun the code each time.

    if the array is named $my_data then if its not found by code you are running elsewhere add
    global $my_data;
    before accessing the values.

    It is not the best idea to put PHP in posts and pages and widgets. its better to use shortcodes when you need to do that.

    shortcodes need activation before they work in widgets. in the thene’s functions.php
    add_filter('widget_text', 'do_shortcode');

    Thread Starter nat3

    (@roboticsguy)

    run the code once at the top of the page. Put its output in an array that can be accessed elsewhere on the page and have the rest of the code on the page access the values in the array rather than rerun the code each time.

    Exactly! That’s what I’m wanting to do!

    I tried to do this by putting

    <?php global $headerVar = 'is this going to work?'; ?>

    in wp_head and then putting

    <?php echo 'hv: ' . $headerVar; ?>

    in a widget, but the output is just “hv,” so it’s not working.

    Don’t put the code in wp_head.

    If this is running on pages, start by putting the code in your theme’s page.php before the WordPress loop.

    Also, the global declaration goes in the widget code, not with the definition of the variable. You are telling the widget to use a variable that is outside of its local context.

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

The topic ‘Global variables in WordPress?’ is closed to new replies.