Current page id returns the incorrect value

To get the current page ID outside of The Loop, you need to use get_queried_object_id(); ($wp_query-> is unnecessary). However, this will only work when you are viewing a single Page (so anything under Pages > All Pages in the back-end). If you are viewing the blog, an archive, or a category it will not be … Read more

Use global variables or function that returns said variables for site-wide private-ish WP settings?

No overriding benefit, other than collision avoidance and safeguard against unintended modification. Not sure if it makes you feel any better but WP core itself has many dependencies on global variables. I’m not saying that’s a good thing; just a fact. Also remember you have a database and functions to handle storing and retrieving options, … Read more

What is the use of $content_width?

$content_width is used to set the default width of embeds, for situations when an element needed to specify a width. This is how WP knew the width of the main content area. However, this was back before the push for responsive design, and modern CSS with fluid embeds and responsive breakpoints. So it isn’t needed … Read more

Best way to pass variables around a WordPress site?

It is possible to do this using the function wp_get_current_user(). This function will get the contents of the WP_User class. It can be stored in a global variable: global $current_user; $current_user = wp_get_current_user(); $current_user will then become an array matching the properties of WP_User for the currently logged in user. If no user is logged … Read more

Does WP have a global of $id?

If you are using netbeans and xdebug, or have set up your debugging environment correctly then you can observe the variables, objects e.t.c as they change. To see when they are created and what is contained within objects such as wp_query or wp_rewrite your posts->ID is set once the function wp() is called in wp-blog-header.php. … Read more

How can I save custom meta to one global value?

Consider that when you add a metabox, nothing force you to save the value in post meta field, you can save a global option from metabox as well. function my_add_custom_box() { $screens = array( ‘post’ ); // add or replace with your cpt name foreach ( $screens as $screen ) { add_meta_box( ‘my_sectionid’, __( ‘Featured … Read more

Declare Global Variable In OOP PHP

The $current_screen is passed as a variable to callables hooked from the current_screen hook. Before this hook, the $current_screen isn’t set up, so globalizing the variable won’t do anything anyway. Also, WordPress offers the convenience function get_current_screen() that returns the global $current_screen variable if it exists. class wpse { protected $current_screen; public function load() { … Read more