David S. David S. - 1 month ago 21
PHP Question

Shortcode with current page id

I've hit a wall with my little of knowledge of php.

Is there any way how to "put current page id" instead of particular id number in do_shortcode function in wordpress? Or is it matter of files inside of the plugin?

<?php echo do_shortcode('[plugin-review id="2"]'); ?>


I dont want to display content from page with id=2 on every page on my website when I put this code into a sidebar.

Thank you for a possible solution.

edit: I have just also found this function (there is more functions with same content) which might be maybe helpful?

public function do_shortcode_ratings( $atts )
{
extract( shortcode_atts( array(
'id' => '',
'template' => '',
'post' => get_the_ID()
), $atts ) );


$shortcode = '[plugin-review id="'. $id .'" branch="ratings" post="'. $post .'" template="'. $template .'"]';

return do_shortcode( $shortcode );

}

Answer

Current post/page ID is available via the global $post variable, i.e.

global $post;
$current_id = $post->ID;

Now you can use the $current_id in your shortcode :)