Bartek
Bartek

Reputation: 55

Using current post ID in Wordpress shortcode

I'm using an event management plugin, and I want to display some specific event details via shortcodes.

I've found this very useful guide regarding this plugin's shortcodes:

https://urjtechhelp.zendesk.com/hc/en-us/articles/115002801594-Embedding-Single-Events-with-the-tribe-event-inline-Shortcode

But I have one problem. As stated in the guide you must always supply an event’s ID via the id shortcode attribute, like this:

[tribe_event_inline id="167"]

What I'm trying to achieve is for the shortcode to always use the ID of the post it's placed in.

I've tried adding additional shortcode

add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );

function the_dramatist_return_post_id() {
    return get_the_ID();
}

And then nest the shortcode in the original one, but apparently it doesn't work that way.

Any idea on how to achieve this?

Thank you in advance for any ideas.

Upvotes: 2

Views: 9115

Answers (4)

WhereDidMyBrainGo
WhereDidMyBrainGo

Reputation: 311

Idea: Use a template for your shortcode like [tribe_event_inline id="ZXZX"]

Add filter for the_content.

Use your filter to replace ZXZX with post ID.

Upvotes: 0

Dmitry Leiko
Dmitry Leiko

Reputation: 4412

Maybe it's help:

add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );

function the_dramatist_return_post_id() {
    global $post;

    return $post->ID ?? '';
}

Upvotes: 4

Ivijan Stefan Stipić
Ivijan Stefan Stipić

Reputation: 6678

Here is one my function what I use time to time. I simplify it for you but with small work can be expanded to more levels.

function current_ID($post_id=NULL){
    global $product, $page;
    if(is_numeric($post_id) && $post_id == intval($post_id)) {} else {
        // Get ID from the current object
        if(!is_object($post_id)) {} else if(property_exists($post_id, 'ID')) {
            $post_id = $post_id->ID;
        }
        // Woocommerce product page
        if(empty($post_id) && property_exists($product, 'ID')) $post_id = $product->ID;
        // Standard page/post ID
        if(empty($post_id)) $post_id =  get_the_ID();
        // Get ID from the $page global
        if(empty($post_id) && property_exists($page, 'ID')) $post_id =  $page->ID;
        // GET request
        if(isset($_GET['post']) && is_numeric($_GET['post']) && $_GET['post'] > 0 && intval($_GET['post']) == $_GET['post']) $post_id = intval($_GET['post']);
        if(isset($_GET['p']) && is_numeric($_GET['p']) && $_GET['p'] > 0 && intval($_GET['p']) == $_GET['p']) $post_id = intval($_GET['p']);
        // Get ID from the get_queried_object_id() function
        if(empty($post_id) && function_exists('get_queried_object_id')) $post_id =  get_queried_object_id(); // Need tests
    }
    // The end
    return $post_id;
}

It support also Woocommerce.

Now you need to include it into your project and add it to shortcode or combine with some other function to accept it.

Upvotes: 0

Alexandru Topală
Alexandru Topală

Reputation: 329

This would not work using the WP text editor. It would also not be a clean implementation.

I would recommend going straight to the problem and edit the function responsible for the "tribe_event_inline" shortcode.

Try searching for add_shortcode('tribe_event_inline' in your source code files.

Upvotes: 0

Related Questions