插件在激活期间生成了 xxx 字符的意外输出

15
我正在创建 WordPress 插件,用于显示 Twitter 总计数器和 Feed 订阅者。您可以通过小部件进行管理。
我遇到了这个错误。 插件在激活期间生成了123个意外输出字符。如果您注意到“头已发送”消息、syndication feeds 或其他问题,请尝试停用或删除此插件。
<?php
/*
 * Plugin Name: Twitter & RSS Stats
 * Version: 1.0
 * Plugin URI: http://sss.com/
 * Description: Facebook, Twitter & RSS Social Stats widget <a href="http://jessealtman.com/2009/06/08/tutorial-wordpress-28-widget-api/">tutorial</a>.
 * Author: Ajay Patel
 * Author URI: http://sss.com/
 */

addHeaderCode();
 function addHeaderCode() {

            echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/TRR_Stats/css/style.css" />' . "\n";    
            }


 /*******************************/
 /*     Create table            */
 /********************************/
function my_plugin_create_table()
{
        // do NOT forget this global
    global $wpdb;

    // this if statement makes sure that the table doe not exist already
    if($wpdb->get_var("show tables like TRR_Stats") != 'TRR_Stats') 
    {
        $sql = "CREATE TABLE TRR_Stats (
        id mediumint(9) NOT NULL,
        rss_email tinytext NOT NULL,
        twitter tinytext NOT NULL,
        rss tinytext NOT NULL,
        UNIQUE KEY id (id)
        );";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
}
// this hook will cause our creation function to run when the plugin is activated
register_activation_hook( __FILE__, 'my_plugin_create_table' );


class FTRWidget extends WP_Widget
{
    /**
    * Declares the FTRWidget class.
    *
    */
    function FTRWidget(){
        $widget_ops = array('classname' => 'widget_hello_world', 'description' => __( "Example widget demoing WordPress 2.8 widget API") );
        $control_ops = array('width' => 300, 'height' => 300);
        $this->WP_Widget('helloworld', __('Twitter & RSS Social Stats'), $widget_ops, $control_ops);
    }

    /**
    * Displays the Widget
    *
    */
    function widget($args, $instance){
        extract($args);
        $rss_email = empty($instance['rss_email']) ? 'webdesignergeeks' : $instance['rss_email'];
        $twitter = empty($instance['twitter']) ? 'webdesignergeek' : $instance['twitter'];
        $rss = empty($instance['rss']) ? 'webdesignergeeks' : $instance['rss'];


        # Featch Data from table
        global $wpdb;
        $item_info = $wpdb->get_row("SELECT * FROM TRR_Stats WHERE id=1;");
        $rss_email_f = $item_info->rss_email;

        $url = file_get_contents('https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=24thfloor');
        preg_match( '/circulation="(\d+)"/', $url, $matches );
        if ( $matches[1] )
        $rss_f = $matches[1] . " Subscribers";
        else
        echo "0";

        $twit = file_get_contents('http://twitter.com/users/show/'.$twitter.'.xml');
        preg_match( '/\<followers_count\>(\d+)\<\/followers_count\>/', $twit, $matches );
        if ( $matches[1] )
        $twitter_f = $matches[1] . " Followers";
        else
        $twitter_f = "0";



        echo '
            <div class="sidebarContainer" id="sidebarSubscribe">

            <a target="_blank" href="http://twitter.com/'.$twitter.'" class="subscribeSidebarBox" id="followTwitter">
                <span class="icon"><img src="'.get_bloginfo('url').'/wp-content/plugins/TRR_Stats/img/twitter.png" alt="Twitter" /></span>
                <span class="title">Follow Us on Twitter</span>
                <span class="count">'.$twitter_f.'+</span>
            </a>

            <a target="_blank" href="http://feeds.feedburner.com/'.$rss.'" class="subscribeSidebarBox" id="subscribeRSS">
                <span class="icon"><img src="'.get_bloginfo('url').'/wp-content/plugins/TRR_Stats/img/rss_feed.png" alt="RSS"/></span>
                <span class="title">Subscribe to our RSS feed</span>
                <span class="count">'.$rss_f.'+</span>
            </a>

            <a target="_blank" href="http://feedburner.google.com/fb/a/mailverify?uri='.$rss_email_f.'" class="subscribeSidebarBox" id="subscribeEmail">
                <span class="icon"><img src="'.get_bloginfo('url').'/wp-content/plugins/TRR_Stats/img/rss_email.png" alt="rss_email" /></span>

                <span class="title">Subscribe for updates via</span>
                <span class="count">EMAIL</span>
            </a>
        </div>';

        # After the widget
        echo $after_widget;
    }


    /**
    * Saves the widgets settings.
    *
    */
    function update($new_instance, $old_instance){
        $instance = $old_instance;
        $instance['rss_email'] = strip_tags(stripslashes($new_instance['rss_email']));
        $instance['twitter'] = strip_tags(stripslashes($new_instance['twitter']));
        $instance['rss'] = strip_tags(stripslashes($new_instance['rss']));

        global $wpdb;
            //Insert First time
            $wpdb->insert( 'TRR_Stats', array(
            'id'    => 1,
            'rss_email' => $instance['rss_email'], 
            'twitter' => $instance['twitter'],
            'rss' => $instance['rss']
            ) 
        );

        //Rest Update Data
        global $wpdb;
            $wpdb->update( 'TRR_Stats', 
            array( 
                'rss_email' => $instance['rss_email'], 
                'twitter' => $instance['twitter'],
                'rss' => $instance['rss']
            ),
            array(
                'id' => 1
            )

        );

        return $instance;
    }

    /**
    * Creates the edit form for the widget.
    *
    */
    function form($instance){
        //Defaults
        $instance = wp_parse_args( (array) $instance, array('rss_email'=>'', 'twitter'=>'engiguide', 'rss'=>'www.rss_email.com/engiguide') );

        $rss_email = htmlspecialchars($instance['rss_email']);
        $twitter = htmlspecialchars($instance['twitter']);
        $rss = htmlspecialchars($instance['rss']);

        # Output the options

        # Twitter
        echo '<p style="text-align:right;"><label for="' . $this->get_field_name('twitter') . '">' . ('Twitter:') . ' <input style="width: 200px;" id="' . $this->get_field_id('twitter') . '" name="' . $this->get_field_name('twitter') . '" type="text" value="' . $twitter . '" /></label></p>';
        echo '<p style="padding-left: 110;">i.e: webdesignergeeks</p>';
        # Rss
        echo '<p style="text-align:right;"><label for="' . $this->get_field_name('rss') . '">' . __('Rss:') . ' <input style="width: 200px;" id="' . $this->get_field_id('rss') . '" name="' . $this->get_field_name('rss') . '" type="text" value="' . $rss . '" /></label></p>';
        echo '<p style="padding-left: 110;">i.e: webdesignergeeks</p>';
        # Rss Email
        echo '<p style="text-align:right;"><label for="' . $this->get_field_name('rss_email') . '">' . ('Rss Email:') . ' <input style="width: 200px;" id="' . $this->get_field_id('rss_email') . '" name="' . $this->get_field_name('rss_email') . '" type="text" value="' . $rss_email . '" /></label></p>';
        echo '<p style="padding-left: 110;">i.e: webdesignergeeks</p>';

    }

}// END class

    /**
    * 
    * Calls 'widgets_init' action after the Hello World widget has been registered.
    */
    function TTRInit() {
    register_widget('FTRWidget');
    }   
    add_action('widgets_init', 'TTRInit');
?>

最终我已经发布了这个插件链接:http://wordpress.org/extend/plugins/twitter-rss-social-stats/。 - Ajay Patel
禁用错误报告,即定义('WP_DEBUG',false)。 - Manik Thakur
1
@ManikThakur 这通常不是一个好主意。建议在完成插件开发之前解决所有错误和警告。 - Ehsan88
有时这种情况发生是因为您的插件文件中有非 PHP 代码。换句话说,您不应该直接输出任何内容,您的 <php? 标签应该从开头开始,?> 应该在结尾处结束。我的两个字符意外输入只是因为在 <php? 前面有一个新行。 - Ehsan88
10个回答

13

在最终的 PHP 结束标记后,我有额外的 "xxx" 空白字符。删除它后,警告消失了。这些空白字符是 "意外输出"。


1
哇!非常感谢。它起作用了! - KD.
这是我情况下的正确答案。从您插件中的每个PHP文件中删除关闭php标记后的任何空格或行,例如此处的 ?> - Atif
这个答案只是有效的,哈哈... - w411 3
它对我不起作用。 - user9437856
你检查了插件的所有文件吗? - Binod Kalathil

8

删除标签开头的空格。将addHeaderCode()从顶部删除,并在addHeaderCode()函数后将此代码add_action('wp_head','addHeaderCode')添加到您的文件中。这肯定会解决您的问题。


5

这是一个与UTF8相关的问题。我用Notepad++将其转换为UTF8格式,但我需要将其转换为“无BOM的UTF8”格式。 现在我没有任何通知。


2

您可以尝试以下方法:

  1. 去除空格
  2. 运行函数 plugin_error()
  3. wp_config.php 文件中设置 wp_DEBUG true,这将帮助您查看错误

这些方法可能会对您有所帮助。


我已经对你的回答进行了负评,因为你提到了一个没有被引用的 plugin_error() 函数。 - Stephen Miller

1

我认为你不应该在addHeaderCode()中发送任何输出,因为这太早了。请参见典型请求期间运行的操作

相反,尝试挂钩到wp_head以添加样式表,这将在<head>...</head>之间触发。


我已经解决了70%的问题,问题在于空格和注释使其出现错误,现在我卡在了如何将CSS包含在这个插件中。 - Ajay Patel
问题在于你在页面的其余部分之前向浏览器发送了输出 - 使用add_action('wp_head', 'addHeaderCode')在适当的位置添加样式表引用。 - agtb
Fin,感谢你的CSS帮助,现在所有的问题都解决了,太感谢了。 - Ajay Patel
没关系 =) 还值得注意的是,如果在 <?php 前有空格或空行(有时很难发现!),您也可能会意外发送输出。 - agtb

0
我通过在WP管理中使用“编辑插件”删除代码中的空格来解决了这个问题。我认为当WP保存更改时,它也会执行所有必要的设置,如将文件保存为UTF-8。

0

您可以删除update()函数,没有这个函数它也能正常工作,并且会执行您正在执行的相同操作。


0
我在使用WooCommerce智能优惠券插件时遇到了同样的问题。我从插件php文件中删除了所有额外的换行符,并将它们保存为“UTF8(无BOM)”格式(正如@Umaie Hamid所提到的)。这解决了问题。

0

尝试使用__()将消息包装起来,就像__("你好,世界!")一样,对我很有用。


__()_e() 函数仅处理翻译。 - Ty Bailey

-1

我之前也遇到了同样的错误,只需要删除你代码中的空格即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接