插件在激活时产生xxx个字符的意外输出


The plugin generated xxx characters of unexpected output during activation

我正在创建一个WordPress插件来显示Twitter总数&Feed订阅者。您可以通过widget来管理它。

我得到这个错误。

插件在激活过程中生成了123个字符的意外输出。

如果您注意到"标题已发送"消息,联合feed问题或其他问题,请尝试停用或删除此插件
<?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');
?>

我得到了额外的"xxx"空白后的最后php关闭标签。删除它,警告就消失了。这些空格是"意外输出"

去掉标签开始部分的空格。删除addHeaderCode ();从顶部添加代码add_action('wp_head', 'addHeaderCode');addHeaderCode()函数后的文件。这绝对能解决你的问题。

这是一个UTF8相关的问题。我用notepad++在UTF8中转换了它,但我不得不在"UTF8(没有BOM)"中转换它。现在我没有任何提示

你应该试试这个:

  1. 删除空白
  2. 运行函数plugin_error()
  3. wp_config.php文件集wp_DEBUG true,这将帮助您看到错误

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

相反,尝试挂接wp_head来添加样式表,它应该在<head>...</head> .

之间触发。

试试用__()来包装消息,就像__("Hello, world!")对我一样。

你可以在没有这个函数的情况下删除update()函数,它会工作得很好,并且会做你正在做的事情。

我在WooCommerce智能优惠券插件上遇到了同样的问题。我从插件php文件中删除了所有额外的新行,并将它们保存在"UTF8(没有BOM)"中。(正如@Umaie Hamid提到的)。

我通过在WP管理中使用"编辑插件"删除代码中的空格来修复这个问题。我想当WP保存更改时,它也做了所有必要的设置,比如将文件保存为UTF-8。

我得到同样的错误,只是在你的代码中删除白色间距