15

I am creating WordPress plugin to display Total Twitter counter & Feed Subscriber. You can manage it via widget.

I am getting this error.

The plugin generated 123 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

<?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');
?>
brasofilo
  • 25,496
  • 15
  • 91
  • 179
Ajay Patel
  • 5,298
  • 11
  • 55
  • 87
  • finally i have launched this plugin link : http://wordpress.org/extend/plugins/twitter-rss-social-stats/ – Ajay Patel Nov 08 '11 at 11:50
  • Disable your error reporting i.e define('WP_DEBUG', false); – Manik Thakur Feb 16 '16 at 08:11
  • 1
    @ManikThakur this is not generally a good idea. It is advised that you resolve all errors and warnings before completing your plugin development. – Ehsan88 Oct 14 '18 at 09:48
  • Sometimes this happens because you have non-php code in your plugin file. In other words, you shouldn't `echo` anything directly and your `` end right at the end. My 2 character unexpected input was just because of a new line before ` – Ehsan88 Oct 14 '18 at 09:50

10 Answers10

13

I got extra "xxx" white spaces after the final php closing tag. Removed it and the warning is gone. Those white spaces were the "unexpected output".

Binod Kalathil
  • 1,939
  • 1
  • 30
  • 43
8

Remove space from start of tags. remove addHeaderCode(); from top and add this code add_action('wp_head', 'addHeaderCode'); to your file after addHeaderCode() function. its definitely resolve your problem.

Ram Ram
  • 300
  • 3
  • 7
5

it was a UTF8 related issue. I converted it with Notepad++ in UTF8 but I had to conert it in "UTF8 (without BOM)". Now I don't have any notice.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Umair Hamid
  • 3,509
  • 3
  • 23
  • 25
2

You should try this:

  1. remove white spaces
  2. run function plugin_error()
  3. in wp_config.php file set wp_DEBUG true , that will help you to see errors

This might help you.

prakash
  • 99
  • 1
  • 7
1

I don't think you should be sending any output in addHeaderCode(), this occurs far too early. See Actions Run During a Typical Request.

Instead, try hooking to wp_head to add your stylesheet, which should be triggered between <head>...</head>.

agtb
  • 477
  • 4
  • 15
  • i have solved it 70% the problem is speacing and comments make it buggy ,now i am syuck in to include css in this plugin – Ajay Patel Oct 11 '11 at 12:42
  • The problem is you're sending output to the browser before the rest of the page - add your stylesheet reference in the appropriate place with add_action('wp_head', 'addHeaderCode'); – agtb Oct 11 '11 at 12:50
  • fin thanks for css help now apptox all r solved man thanks a lot – Ajay Patel Oct 11 '11 at 12:57
  • No worries =) It's also worth bearing in mind that you can also accidentally send output before headers if you've got spaces or blank lines before – agtb Oct 11 '11 at 13:03
0

You can remove update() function without this function it will work fine and will do same thing you are doing.

Mandeep Gill
  • 4,577
  • 1
  • 28
  • 34
0

I had the same issue with WooCommerce Smart Coupons plugin. I removed all extra new lines from plugin php files and saved them in "UTF8 (without BOM)" (as @Umaie Hamid mentioned). This fixed the issue.

Vajira Lasantha
  • 2,435
  • 3
  • 23
  • 39
0

I've fixed this by removing the spaces in the code using the "edit plugin" in the WP admin. I think when WP save the changes it also does all the necessary settings like saving the file as UTF-8.

Wreeecks
  • 2,186
  • 1
  • 33
  • 53
0

Try wrapping the message with __() like __("Hello, world!") did the trick for me.

Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
-1

I Was getting same error just remove white spacing in your code.

Aniket
  • 1