在WordPress 3.5中提供警告的StackOverflow类型的徽章插件

时间:2012-12-18 作者:Christine Cooper

为了与Wordpress 3.4.2配合使用,我修改了一个废弃的badge插件Stackoverflow表单,它确实做到了。全文如下:http://pastebin.com/Ta91zXiL

升级到Wordpress v3时。5、我在发布帖子(或发表评论)时收到以下警告:

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 342 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 342 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 342 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 355 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 355 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 342 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 342 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 342 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 355 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Missing argument 2 for wpdb::prepare(), called in /public_html/wp-content/plugins/rockhoist-badges/rh-badges.php on line 355 and defined in /public_html/wp-includes/wp-db.php on line 990

Warning: Cannot modify header information - headers already sent by (output started at /public_html/wp-includes/wp-db.php:990) in /public_html/wp-includes/pluggable.php on line 876
因此,它似乎与第342行和第355行有关:

function rhb_get_user_comment_count( $args = \'\' ) {

        global $wpdb;

        $comment_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*)
                FROM " . $wpdb->prefix . "comments
                WHERE user_id = " . $args[\'user_ID\'] . "
                AND comment_approved = \'1\'" ) ); // line 342

        return $comment_count;
}

function rhb_get_user_post_count( $args = \'\' ) {

        global $wpdb;

        $post_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*)
                FROM " . $wpdb->prefix . "posts
                WHERE post_author = " . $args[\'user_ID\'] . "
                AND post_status = \'publish\'
                AND post_type = \'post\'" ) ); // line 355

        return $post_count;
}
我一整天都在试图理解这些警告,但都失败了。有没有人能帮我解决这个问题,这样我们就可以为WP 3.5提供一个非常有用的徽章系统的工作版本?

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

Lead developer Nacin answers this one:

你好,插件或主题作者!您可能在互联网上搜索上述错误后找到了这篇帖子:“PHP警告:wpdb::prepare()缺少参数2。”

这是3.5中的新警告。没有网站被破坏,一切都像以前一样好。但是,这确实是您需要关注的问题,因为您可能会让您的用户暴露于一个可能的SQL注入漏洞。这可不好玩!

阅读其余部分,以便进一步解释。

关于恢复现有代码:

$wpdb->prepare( 
    "
    SELECT COUNT(*)
    FROM " . $wpdb->prefix . "comments
    WHERE user_id = " . $args[\'user_ID\'] . "
    AND comment_approved = \'1\'
    " 
)
首先,清除不必要的字符串串联,并调用$wpdb->comments 对于注释表:

$wpdb->prepare( 
    "
    SELECT  COUNT(*)
    FROM    $wpdb->comments
    WHERE   user_id = $args[\'user_ID\']
    AND     comment_approved = \'1\'
    " 
)
现在,警告与查询的这一部分有关:

WHERE   user_id = $args[\'user_ID\']
您需要更换$args[\'user_ID\'] 具有$d, 然后使用$args[\'user_ID\'] 缺少第二个参数:

$wpdb->prepare( 
    "
    SELECT  COUNT(*)
    FROM    $wpdb->comments
    WHERE   user_id = %d
    AND     comment_approved = \'1\'
    ",
    $args[\'user_ID\'] // %d
)
第二个应类似:

$wpdb->prepare( 
    "
    SELECT    COUNT(*)
    FROM      $wpdb->posts
    WHERE     post_author = %d
    AND       post_status = \'publish\'
    AND       post_type = \'post\'
    ",
    $args[\'user_ID\'] // %d
)

结束

相关推荐

类别页面不使用Category y.php,而是重定向到主页

我当前正在修改一个主题。此主题没有类别。php我需要一个自定义类别页面。所以,我做了一个。它可以在我的本地服务器上运行,但当我将其上载到真正的服务器时,该页面无法运行。它总是重定向到frontpage。它有一个头版。php和其他页面包括Chive。php,索引。php,一些自定义页面模板,单页面模板,taxonomy-custom\\u cat。php等。我的意思是一切都很正常。其他一切都很好,但在主服务器类别上无论如何都不起作用。它是否违反了模板层次结构?顺便说一下,主服务器是wordpress mul