I’m trying to get the post id inside of my custom wordpress plugin and I’m using the following code:
global $post;
$current_page_id = $post->ID;
var_dump($current_page_id);
But without any success. With var_dump
I’m getting on every call null
.
Than, if I add to a template than the output works:
add_action('wp_footer', 'test');
function test() {
global $post;
$current_page_id = $post->ID;
}
What I would like to achieve inside of my plugin, is to pass the current post id to one of my functions. So something like:
my_function($base_url, array('variable_to_post' => $post->ID));
If you need to get post id from wordpress plugin, you can run your plugin in
wp
action hook, to get access to global$wp_query
object.Here is simple test solution.
Hope this will be of help for someone.
It is possible, when you are using this in your plugin:
And declare following function: