Creating a (global)-mapping

I figured it out myself. function generate_the_excluded_array() { global $excluded_pages; $excluded_pages = array(‘Foo’, ‘Bar’); } add_action( ‘after_setup_theme’, ‘generate_the_excluded_array’ ); And then it can be ‘imported’ using the global, as such: function example_shortcode_definition() { global $excluded_pages; echo $excluded[0]; } add_shortcode( ‘example_shortcode’, ‘example_shortcode_definition’ );

$GLOBALS[‘value1’] is not working

The correct syntax for using with global keyword. To access a global variable in WordPress, you first need to globalize the variable with global $variable; Write inside function.php as: function myfunction(){ global $get_variable; $get_value = $db->query(“SELECT * FROM mytable”)->fetch(); $get_variable = $get_value; } add_action( ‘after_theme_setup’, ‘myfunction’ ); Inside the function you can now able to … Read more

How to save a viewer specific option

Is there a standard interface in WordPress for saving a global PHP variable $bgcolor to the user and read it from him anytime he browses to another blog page. There is nothing that will automagically do it for you but it sounds like you just need to save a little bit of user meta, and … Read more

WP user agent returns random variables

You can examine wp-includes/vars.php for specific logic which implements sniffing for these globals in WordPress. In a nutshell the checks are very basic, especially considering user agent sniffing is inherently a mess. If you need a more reliable implementation you would have to get one elsewhere (or code it yourself). That or use different technique … Read more

Recommendations on accessing current user data in WordPress

You can use wp_get_current_user() instead of using the $current_user global. $my_current_user = wp_get_current_user(); if ( $my_current_user->display_name ) { echo ‘<p>Welcome ‘ . $my_current_user->display_name . ‘</p>’; } Note that I used $my_current_user instead of $current_user as a variable name, because you can run into trouble if you use the global variable’s name.

How does WordPress make its functions globally available?

Themes and plugins are just files that are loaded alongside all the other WordPress files that contain its functions, and those are all loaded whenever WordPress is run. So there’s no special trick or anything weird going on. WordPress just loads the files that contain these functions before it loads themes and plugins. Keep in … Read more