widgets - The plugin generated 3 characters of unexpected output during activation

admin2025-06-02  1

I'm creating a WordPress Widget for show name of advertiser.

When I try to activate the plugin a error

The plugin generated 3 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.

I did these things:

1- remove white space before or after the PHP opening or closing tags

2- change encoding to UTF8-BOM

<?php
class adsMain extends WP_Widget {

    public function __construct() {

        parent::__construct(
            'adsMain',
            __( 'تبلیغات', 'text' ),
            array(
                'classname'   => 'adsMain',
                'description' => __( 'برای ایجاد تبلیغات جدید این کادر را به مکان دلخواه خود بکشید.', 'text' )
            )
        ); 
    }
    /**  
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {

        extract( $args );

        $advertiser = apply_filters( 'widget_title', $instance['advertiser'] );

        echo $advertiser;

    }

    /**
      * Sanitize widget form values as they are saved.
      *
      * @see WP_Widget::update()
      *
      * @param array $new_instance Values just sent to be saved.
      * @param array $old_instance Previously saved values from database.
      *
      * @return array Updated safe values to be saved.
      */
    public function update( $new_instance, $old_instance ) {

        $instance = $old_instance;

        $instance['advertiser'] = strip_tags( $new_instance['advertiser'] );

        return $instance;

    }

    /**
      * Back-end widget form.
      *
      * @see WP_Widget::form()
      *
      * @param array $instance Previously saved values from database.
      */
    public function form( $instance ) {

        $advertiser = ( isset($instance['advertiser']) ? esc_attr ( $instance['advertiser'] ) : '' );
?>

        <p>
            <label for="<?php echo $this->get_field_id('advertiser'); ?>"><?php _e('نام شخص / شرکت تبلیغ دهنده:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('advertiser'); ?>" name="<?php echo $this->get_field_name('advertiser'); ?>" type="text" value="<?php echo $advertiser; ?>" />
        </p>
<?php
    }

}

/* Register uploader */
add_action ('admin_enqueue_scripts', function () {
    wp_register_script('adsMain-script' , plugins_url( '/js/adsMain-script.js', __FILE__ ), array( 'jquery' ), '20160904', true );
    wp_enqueue_script('adsMain-script');
    wp_enqueue_media();
});

/* Register the widget */
add_action('widgets_init', function() {
     register_widget( 'adsMain' );
});
?>

I'm creating a WordPress Widget for show name of advertiser.

When I try to activate the plugin a error

The plugin generated 3 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.

I did these things:

1- remove white space before or after the PHP opening or closing tags

2- change encoding to UTF8-BOM

<?php
class adsMain extends WP_Widget {

    public function __construct() {

        parent::__construct(
            'adsMain',
            __( 'تبلیغات', 'text' ),
            array(
                'classname'   => 'adsMain',
                'description' => __( 'برای ایجاد تبلیغات جدید این کادر را به مکان دلخواه خود بکشید.', 'text' )
            )
        ); 
    }
    /**  
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {

        extract( $args );

        $advertiser = apply_filters( 'widget_title', $instance['advertiser'] );

        echo $advertiser;

    }

    /**
      * Sanitize widget form values as they are saved.
      *
      * @see WP_Widget::update()
      *
      * @param array $new_instance Values just sent to be saved.
      * @param array $old_instance Previously saved values from database.
      *
      * @return array Updated safe values to be saved.
      */
    public function update( $new_instance, $old_instance ) {

        $instance = $old_instance;

        $instance['advertiser'] = strip_tags( $new_instance['advertiser'] );

        return $instance;

    }

    /**
      * Back-end widget form.
      *
      * @see WP_Widget::form()
      *
      * @param array $instance Previously saved values from database.
      */
    public function form( $instance ) {

        $advertiser = ( isset($instance['advertiser']) ? esc_attr ( $instance['advertiser'] ) : '' );
?>

        <p>
            <label for="<?php echo $this->get_field_id('advertiser'); ?>"><?php _e('نام شخص / شرکت تبلیغ دهنده:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('advertiser'); ?>" name="<?php echo $this->get_field_name('advertiser'); ?>" type="text" value="<?php echo $advertiser; ?>" />
        </p>
<?php
    }

}

/* Register uploader */
add_action ('admin_enqueue_scripts', function () {
    wp_register_script('adsMain-script' , plugins_url( '/js/adsMain-script.js', __FILE__ ), array( 'jquery' ), '20160904', true );
    wp_enqueue_script('adsMain-script');
    wp_enqueue_media();
});

/* Register the widget */
add_action('widgets_init', function() {
     register_widget( 'adsMain' );
});
?>
Share Improve this question asked Sep 4, 2016 at 7:35 EposoEposo 131 silver badge4 bronze badges 3
  • 2 try in UTF-8 without BOM – mmm Commented Sep 4, 2016 at 8:41
  • @mmm please include your comment as an answer and try to add some explanation why you thought this would work. – cjbj Commented Sep 4, 2016 at 14:53
  • If you are using a debugger, then put a breakpoint around in the activate_plugin method in wordpress/wp-admin/includes/plugin.php. The content of the $output variable might show you the data loaded into the Exception invariably being thrown, and then obfuscated by Wordpress. – Todd Holmberg Commented Feb 1, 2017 at 22:09
Add a comment  | 

1 Answer 1

Reset to default 1

What is BOM?

The UTF-8 byte order mark or simply UTF-8 BOM is a series of bytes that helps the reader to understand the encoding of the current document.

These 3 bytes are:

EF BB BF

Or simply the Unicode character : U+FEFF

In a plugin file encoded as BOM, these bytes will be output when the template is called, causing early output in header, that triggers a common error:

The plugin generated X characters of unexpected output during activation.

This can also happen to theme's files. Since using BOM for UTF-8 files is not recommended by Unicode Standards, you can save your templates as UTF-8 without BOM.

You will also notice that every UTF-8 file that is encoded under BOM has at least a size of 3 bytes, even if it's empty.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748811097a313918.html

最新回复(0)