Merijndk
Merijndk

Reputation: 1693

wordpress. show shortcodes in lightbox plugin

I am working on a WordPress theme. I use a plugin to display pictures on a page (for each post 1 picture), Whenever one of this pictures is clicked the following code registers this and opens a lightbox with the content of the post in it:

<?php
if($_REQUEST['popup']!=''){

$postObj = get_post( $_REQUEST['pid'] );


echo '<div class="ostContent">'.$postObj->post_content.'</div>';

exit;
?>

This all works fine.

Now the problem is that all content get displayed nicely. but for some reason shortcodes don't work. and also when I use a widget in post plugin to display a widget in the post, it doesn't get displayed.

First I tought I needed to enable shortcodes. So I changed this:

echo '<div class="ostContent">'.$postObj->post_content.'</div>';

with this:

echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';

But still nothing. So now I have no idea what to change to make the lightbox show widgets Hope anyone knows the solution!

EDIT: when I open the post outside the lightbox (by just going to the single page) the shortcode get used like it should be. so somehow the code above don't recognize the shortcode or...

Upvotes: 0

Views: 998

Answers (2)

Jylan
Jylan

Reputation: 1

I think I understand your problem.

The following should work, assuming the posts in question have a post_type of post:

<?php     

    // Check for existence of 'popup' & 'pid' query vars
    if ( $_REQUEST['popup'] && $_REQUEST['pid'] ) {    

        // Select single post by ID (using value of the 'pid' query var)
        $query = new WP_Query( array ( 'p' => $_REQUEST['pid'] ) );    

        // Check that the query has returned something
        if ($query->have_posts()) {    

            /* Loop through query until we run out of posts
            (should only happen once in this case!) */
            while ($query->have_posts()) {    

                // Setup post, so we can use the_content() and stuff
                the_post();    

                echo '<div class="ostContent">';    

                /* The part we've been waiting for! the_content() will
                display your post content as expected */
                the_content();

                echo '</div>';    

            }    

        }    

    }    

?>

WP_Query is the way to go the majority of the time when needing to retrieve posts: http://codex.wordpress.org/Class_Reference/WP_Query

Your code was retrieving and displaying data almost directly from the WordPress posts table, giving WordPress no chance to apply any of its internal actions and filters (e.g. automatic paragraphs from double line breaks, shortcode execution).

Upvotes: 0

Shawn
Shawn

Reputation: 3369

According to a sample here: http://codex.wordpress.org/Function_Reference/do_shortcode

It looks like you need to change: echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';

to:

echo '<div class="ostContent">'.do_shortcode([shortcode_in_brackets]).'</div>';

This should actually display the code. I am assuming that you have defined the actual text in the widget in which the shortcode applies.

Otherwise in the way you currently do it the PHP fires before the post_content even has a value.

Upvotes: 0

Related Questions