WordPress在插件中应用筛选器导致500内部错误

时间:2014-09-18 作者:Merijn dk

我使用插件https://wordpress.org/plugins/show-post-in-lightbox/ 显示页面上每个帖子的所有特色图片。每当点击其中一幅图像时,就会出现一个灯箱,显示帖子内容。

这一切都很好,但显示内容的原始代码使用:

if($_REQUEST[\'popup\']!=\'\'){
    $postObj = get_post( $_REQUEST[\'pid\'] );
    echo $postObj->post_content;
    exit;
}
除了因为WordPress不能格式化HTML,所以不太需要它之外,它工作得很好。此外,WordPress不会识别短代码。因此,我决定更改脚本,以确保它工作正常,并且我可以使用短代码:

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;
}
但这给了我一个错误:打开灯箱时加载灯箱时出错。当我删除APPLT\\u FILTERS行时,它不会出错(但因为我需要该行,所以它也不会显示任何内容)

但我认为可以假设

$content = apply_filters(\'the_content\', $content);
就是灯箱撞坏的那个。这些代码都在插件文件中。有人知道为什么上面的代码会产生500次崩溃吗?

希望有人能帮我!

整个代码:

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 个回复
最合适的回答,由SO网友:Tunji 整理而成

首先,您应该学习如何调试。如果出现500个内部错误,那么错误应该显示在错误日志中。

我对您的代码进行了以下更正:

NEVER USE extract() 而是使用数组中的不同键获取值。

你们班没有右括号

您应该使用PHP的输出缓冲,这样您就可以从spbc_showWidget() 然后可以在小部件功能中回显它

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();

    }
}

SO网友:Thinkerman

我认为其中一个问题是你在重复短代码的结果,而不是“返回”它。

如果PHP没有显示出识别短代码的能力,那么应该使用输出缓冲区。参见示例

function my_shortcode(){

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

}

add_shortcode(\'shortcode\',\'my_shortcode\');
出于明显的原因,更新插件总是比编辑插件的代码更安全。

结束