0

I use the plugin https://wordpress.org/plugins/show-post-in-lightbox/ to display all featured image of every post on a page. whenever one of these images get clicked on a lightbox will appear showing the post content.

This all works fine but the original code that display the content uses:

if($_REQUEST['popup']!=''){
    $postObj = get_post( $_REQUEST['pid'] );
    echo $postObj->post_content;
    exit;
}

This works fine besides the fact that it's not very needed because WordPress can't format the HTML. Also, WordPress won't recognize shortcodes. So I decided to change the script to make sure it works good and I can use shortcodes:

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

    $postObj = $_POST['pid'];
    $content_post = get_post($postObj);
    $content = $content_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;
    exit;
}

But this gives me the error: error loading lightbox on opening the lightbox. When I delete the APPLT_FILTERS line it doesn't error (but because I need that line it doesn't display anything as well)

But I think it is safe to assume the

$content = apply_filters('the_content', $content);

is the one that the lightbox crashes on. This code is all inside the plugin files. Does anyone know why the above code generates and 500 crash?

Hope someone can help me out!

the whole code:

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

    $postObj = $_POST['pid'];
    $content_post = get_post($$postObj);
    $content = $content_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;
}

class spbc_showPostsWidget extends WP_Widget{

    function spbc_showPostsWidget() {
        $options = array('description' => 'Show posts from selected categories.');
        parent::WP_Widget(false, $name = 'Show Posts By Category', $options);
    }
    /*-----------------------------------------------------------*/
    function widget($args, $instance) {
        extract($args, EXTR_SKIP);

        $ost_title              = empty($instance['ost_title']) ? ' ' :     apply_filters('widget_title', $instance['ost_title']);
        $ost_limit              = (is_numeric($instance['ost_limit'])) ? $instance['ost_limit'] : 5;
        $ost_orderby            = ($instance['ost_orderby']) ? $instance['ost_orderby'] : 'date';
        $ost_order              = ($instance['ost_order']) ? $instance['ost_order'] : 'desc';
        $ost_exclude            = ($instance['ost_exclude'] != '') ? $instance['ost_exclude'] : 0;
        $ost_excludeposts       = ($instance['ost_excludeposts'] != '') ? $instance['ost_excludeposts'] : 0;
        $ost_category_id        = $instance['ost_categoryid'];
        $ost_showdate           = ($instance['ost_show_date'] == 'on') ? 'yes' : 'no';
        $ost_thumbnail          = ($instance['ost_thumbnail'] == 'on') ? 'yes' : 'no';
        $ost_thumbnail_size     = ($instance['ost_thumbnail_size']) ? $instance['ost_thumbnail_size'] : 'thumbnail';

        echo $before_widget;

        $this->spbc_showWidget($instance);
        echo $after_widget;
    }
    /*-----------------------------------------------------------*/
    public static function get_UrlFromText($content,$url='Y'){

        if($url=='Y'){
            $imgpattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
            preg_match($imgpattern, $content, $article_image);
        }else{
            preg_match_all('/<img[^>]+>/i',$content, $article_image);
        }
        return $article_image;
    }

    /*-----------------------------------------------------------*/
    function spbc_showWidget($instance){
        global $post;
        $query    = array(
            'posts_per_page' => $instance['ost_limit'],
            'cat' => $instance['ost_categoryid'],
            'orderby' => $instance['ost_orderby'],
            'order' => $instance['ost_order'],
            'category__not_in' => array($instance['ost_exclude']),
            'post__not_in' => array($instance['ost_excludeposts'])
        );

        $wp_query = new WP_Query($query);

        if ($wp_query->have_posts()):

            echo '
                            <div class="list-posts-by-category">                                   
                                <ul>';
            while ($wp_query->have_posts()):
                $wp_query->the_post();
                $image_id = get_post_thumbnail_id();


                if(!empty($instance['ost_thumbnail'])){
                    if ( in_array($instance['ost_thumbnail_size'],array('thumbnail', 'medium', 'large', 'full'))) {
                        $ost_thumb_size = $instance['ost_thumbnail_size'];
                    }elseif ($instance['ost_thumbnail_size']){
                        $ost_thumb_size = array($instance['ost_thumbnail_size']);
                    }else {
                        $ost_thumb_size = 'thumbnail';
                    }
                    $ost_thumbnail = get_the_post_thumbnail($post->ID, $ost_thumb_size);
                }else{
                    $ost_thumbnail = "";
                }
                ?>

                <li>
                <a class="ostlightbox"  href="<?php echo get_site_url().'/index.php?pid='.$post->ID.'&popup=Y'; ?>" title="<?php echo the_title_attribute(); ?>">
                    <?php echo '<div class="ostoverlay"></div>'; ?>
                    <?php echo '<div class="titleconthidden"><p class="osttitle">'.$post->post_title.'</p></div>'; ?>
                    <?php echo '<div class="titlecont"></div>'; ?>


                    <?php

                    echo $ost_thumbnail;

                    ?>
                </a>
                <?php if(!empty($instance['ost_show_date'])){ ?><span><?php      echo get_the_time('F jS, Y'); ?></span><?php } ?>
                </li><?php
            endwhile;
            echo '
                                </ul>
                            </div>';
        endif;
    }
2
  • Well, for starters, you have a double $$ in $$postObj.
    – Otto
    Commented Sep 18, 2014 at 13:53
  • oops. changed it. but thats not the solution. thanx anyway!
    – Merijn dk
    Commented Sep 18, 2014 at 14:10

2 Answers 2

2

First of all, you should learn how to debug. If you're getting a 500 internal error, then the error should show up in your error logs.

I've made the following corrections to your code below:

NEVER USE extract() instead get the values using their various keys from the array.

Your class didn't have a closing brace

You should use PHP's output buffering, that way you get all the output from spbc_showWidget() and can then echo it in your widget function

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

    $postObj = $_POST['pid'];
    $content_post = get_post($postObj);
    $content = $content_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    echo $content;
}

class spbc_showPostsWidget extends WP_Widget
{

    function spbc_showPostsWidget()
    {
        $options = array('description' => 'Show posts from selected categories.');
        parent::WP_Widget(false, $name = 'Show Posts By Category', $options);
    }

    /*-----------------------------------------------------------*/
    function widget($args, $instance)
    {
        extract($args, EXTR_SKIP); // NEVER USE EXTRACT

        $ost_title = empty($instance['ost_title']) ? ' ' : apply_filters('widget_title', $instance['ost_title']);
        $ost_limit = (is_numeric($instance['ost_limit'])) ? $instance['ost_limit'] : 5;
        $ost_orderby = ($instance['ost_orderby']) ? $instance['ost_orderby'] : 'date';
        $ost_order = ($instance['ost_order']) ? $instance['ost_order'] : 'desc';
        $ost_exclude = ($instance['ost_exclude'] != '') ? $instance['ost_exclude'] : 0;
        $ost_excludeposts = ($instance['ost_excludeposts'] != '') ? $instance['ost_excludeposts'] : 0;
        $ost_category_id = $instance['ost_categoryid'];
        $ost_showdate = ($instance['ost_show_date'] == 'on') ? 'yes' : 'no';
        $ost_thumbnail = ($instance['ost_thumbnail'] == 'on') ? 'yes' : 'no';
        $ost_thumbnail_size = ($instance['ost_thumbnail_size']) ? $instance['ost_thumbnail_size'] : 'thumbnail';

        echo $args['before_widget'];

        echo $this->spbc_showWidget($instance);
        echo $args['after_widget'];
    }

    /*-----------------------------------------------------------*/
    public static function get_UrlFromText($content, $url = 'Y')
    {

        if ($url == 'Y') {
            $imgpattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
            preg_match($imgpattern, $content, $article_image);
        } else {
            preg_match_all('/<img[^>]+>/i', $content, $article_image);
        }
        return $article_image;
    }

    /*-----------------------------------------------------------*/
    function spbc_showWidget($instance)
    {
        global $post;
        $query = array(
            'posts_per_page' => $instance['ost_limit'],
            'cat' => $instance['ost_categoryid'],
            'orderby' => $instance['ost_orderby'],
            'order' => $instance['ost_order'],
            'category__not_in' => array($instance['ost_exclude']),
            'post__not_in' => array($instance['ost_excludeposts'])
        );

        $wp_query = new WP_Query($query);

        if ($wp_query->have_posts()):

            ob_start() ?>
            <div class="list-posts-by-category">
                <ul>
                <?php while ($wp_query->have_posts()):
                    $wp_query->the_post();
                    $image_id = get_post_thumbnail_id();


                    if (!empty($instance['ost_thumbnail'])) {
                        if (in_array($instance['ost_thumbnail_size'], array('thumbnail', 'medium', 'large', 'full'))) {
                            $ost_thumb_size = $instance['ost_thumbnail_size'];
                        } elseif ($instance['ost_thumbnail_size']) {
                            $ost_thumb_size = array($instance['ost_thumbnail_size']);
                        } else {
                            $ost_thumb_size = 'thumbnail';
                        }
                        $ost_thumbnail = get_the_post_thumbnail($post->ID, $ost_thumb_size);
                    } else {
                        $ost_thumbnail = "";
                    }
                    ?>

                    <li>
                    <a class="ostlightbox" href="<?php echo get_site_url() . '/index.php?pid=' . $post->ID . '&popup=Y'; ?>"
                       title="<?php echo the_title_attribute(); ?>">
                        <?php echo '<div class="ostoverlay"></div>'; ?>
                        <?php echo '<div class="titleconthidden"><p class="osttitle">' . $post->post_title . '</p></div>'; ?>
                        <?php echo '<div class="titlecont"></div>'; ?>


                        <?php echo $ost_thumbnail; ?>
                    </a>
                    <?php if (!empty($instance['ost_show_date'])) { ?>
                    <span><?php echo get_the_time('F jS, Y'); ?></span><?php } ?>
                    </li><?php
                endwhile; ?>
                </ul>
            </div>;
        <?php endif;
        return ob_get_clean();

    }
}
0

I think the one of the problems is you are echoing the results of the shortcode instead of 'returning' it.

If PHP isn't showing recognizing the shortcode then you should use output buffer. See example

function my_shortcode(){

    ob_start();
    //codes here;
    $something = ob_get_clean();
    return $something;

}

add_shortcode('shortcode','my_shortcode');

It's always safer to update the plugin than to edit the codes of the plugin for obvious reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.