I am creating theme and I have a need to create global variables within theme functions.php
file.
Currently I am doing it like this:
/**
* Setup globals
*/
global $nb_id, $nb_page;
$nb_id = get_the_ID();
// Prepare & set fallback
$globals_array = array(
'nb_id' => $nb_id,
'nb_page' => array(
'id' => $nb_id,
'end' => 'front',
'name' => 'home',
'type' => 'page'
)
);
// Set each value from globals array as global $var;
foreach ($globals_array as $name => $value) {
global $$name;
$$name = $value;
$_GLOBALS[$name] = $value;
// Set user view of page
if($name == 'nb_page'){
if(is_admin()) $$name['end'] = 'back';
$$name['name'] = $nb_pagenow;
// Determine if is page or single post
if(is_single()) $$name['type'] = 'single';
$_GLOBALS[$name] = $$name;
}
}
The issue is that I cannot retrieve front-end current page/post ID using get_the_ID()
, $wp_query->get_queried_object_id()
or even $wp->public_query_vars
with page_id
.
Currently all the scenarios return these values:
get_the_ID()
=null
$wp_query->get_queried_object_id()
=0
$wp->public_query_vars
="page_id" [34]=> string(5) "error"
Does anyone have any idea why I am having this issue? Am I doing something wrong? Maybe there is another way to call front-end page/post data within functions.php
!?
wp
at the earliest.parse_query
and some others that are ready after$wp
variables are created. But with no luck. In above version I don't use any.get_queried_object_id()
directly.