WordPress Theme variables scope
Apparently global does the trick. The problem was that my variable $categories should have been redefined with a global in front of it, in each template I needed to use it.
Apparently global does the trick. The problem was that my variable $categories should have been redefined with a global in front of it, in each template I needed to use it.
No, $post_id is not a global variable. You can see a list of global variables WordPress creates here: https://codex.wordpress.org/Global_Variables $post_id is just a common naming convention for a variable that contains a post ID. In tutorials and example code it shows that the value is expected to be a post ID, but you still need … Read more
This is perhaps too broad a question to answer well. The $post global could be used in all sorts of ways. Whether or not it should be used depends on each circumstance. However, as I understand it, the main intended purpose of the $post global is its use in theme template files. When you use … Read more
I’ve defined a variable – let’s call it $header_var, which is placed in the header.php file. I would like this variable to be passed onto my template file (in this case taxonomy.php). global is not a very recommended way to do the trick, but it works if you use it properly: put it before to … Read more
Keep in mind that the $content_width global is not only used for images, but also for embedded objects, such as video. So, any solution won’t merely be a case of dropping use/support of $content_width itself. Usually a Theme will constrain content-area images in a few ways: Large Image size: Defining $content_width something appropriate for the … Read more
Look at the declaration for the function: function add_settings_field( $id, $title, $callback, $page, $section = ‘default’, $args = array() ) { } The last parameter takes your arguments and passes them to the callback function. Example from my plugin Public Contact Data foreach ($this->fields as $type => $desc) { $handle = $this->option_name . “_$type”; $args … Read more
function __add_global_categories( $term_id ) { if ( get_current_blog_id() !== BLOG_ID_CURRENT_SITE || ( !$term = get_term( $term_id, ‘category’ ) ) ) return $term_id; // bail if ( !$term->parent || ( !$parent = get_term( $term->parent, ‘category’ ) ) ) $parent = null; global $wpdb; $blogs = $wpdb->get_col( “SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = ‘{$wpdb->siteid}'” ); foreach … Read more
There is a sad truth: you can never ever be sure that some code will not break your code, and there is nothing you can do to prevent that. Especially in WordPress, where everything is global. That said, yes, global $post is one of the most used global var, so using special care for it … Read more
Or, if you’re lazy, just install the Debug Bar plugin. It adds a button to the admin bar that, when clicked, reveals a panel with all kinds of useful information, including deprecation notices, WP_Query variables and an SQL query log.
While I strongly advise against this, and it will not speed things up, your usage is incorrect. WordPress already caches these things in the object cache/memory so it doesn’t have to fetch it multiple times in the same request, you don’t need to store the result and reuse, WP does that already out of the … Read more