I need to capture the current page id, execute some conditional script in footer, in my own plugin function hooks to wp_footer
.
Here is my plugin code, but page id returning wrong value?
<?php
/*
Plugin Name: My Plugin
Author: GS
Version: 1.1
*/
Function footer_page_id()
{
global $post;
echo '<div> page_id:'.$post->ID.'</div>';// output wrong value
global $wp_query;
echo 'page_id:'.get_the_ID();// output wrong value
echo 'post_id:'.$wp_query->post->ID; // output wrong value
echo 'page_id:'.$wp_query->get_queried_object_id(); // output always 0
echo'</br>';exit;
}
add_action('wp_footer', ' footer_page_id ');
?>
Thanks in advance.
I need to capture the current page id, execute some conditional script in footer, in my own plugin function hooks to wp_footer
.
Here is my plugin code, but page id returning wrong value?
<?php
/*
Plugin Name: My Plugin
Author: GS
Version: 1.1
*/
Function footer_page_id()
{
global $post;
echo '<div> page_id:'.$post->ID.'</div>';// output wrong value
global $wp_query;
echo 'page_id:'.get_the_ID();// output wrong value
echo 'post_id:'.$wp_query->post->ID; // output wrong value
echo 'page_id:'.$wp_query->get_queried_object_id(); // output always 0
echo'</br>';exit;
}
add_action('wp_footer', ' footer_page_id ');
?>
Thanks in advance.
Share Improve this question edited Sep 26, 2018 at 11:13 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Sep 26, 2018 at 10:14 sridharnethasridharnetha 1011 silver badge4 bronze badges 1 |2 Answers
Reset to default 1To 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 a valid page ID. On categories it will return the category ID, but on archives or the blog it will not have a value because those things do not have an ID.
If your theme or another plugin uses query_posts()
, this could interfere with this function. Using wp_reset_query()
will fix this, but whichever theme or plugin is running query_posts()
should be doing this.
If you are using query_posts()
, don't. Use a custom query with new WP_Query()
or get_posts()
.
for above requirement you can rewrite the code like this
function footer_page_id()
{
global $post;
echo '<div> page_id:'.$post->ID.'</div>';
// remaining codes here
}
add_action('wp_footer', 'footer_page_id');
Please note that inorder to check the value of a variable inside a wordpress function please use debug log technique.
First enable debugging as per wordpress official tutorial https://codex.wordpress/Debugging_in_WordPress
Then you can find debugg.log file inside wp-content folder
function footer_page_id()
{
global $post;
echo '<div> page_id:'.$post->ID.'</div>';// output wrong value
error_log(print_r($post->ID,true));
}
add_action('wp_footer', 'footer_page_id');
Like this you can check the variable values in plugin.
$wp_query
— try this: addwp_reset_query();
after theglobal $post;
. – Sally CJ Commented Sep 26, 2018 at 10:29