0

I am getting the famous "The plugin generated xxx characters of unexpected output during activation." error when I activate plugin I am working on.

Terminal says my files are in us-ascii, but when I am working on this files in my PHP Storm, there is UTF-8 mark displayed in the bottom right corner.

I think I don't have empty spaces because my PHP Storm removes them automatically on command+s.

I tried:

  • With and without: ?>
  • With and without $table_name dynamic prefix declaration
  • With and without $tbyb_db_version (version) declaration
  • With both add_option and update_option for add_option( 'tbyb_db_version', self::$tbyb_db_version ); line
  • With both procedural and OOP static function defining

This is my class.TbybInstall.php class:

<?php
/**
 * Prevent intruders from sneaking around
 */
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );


/**
 * TbybInstall class
 */
if ( !class_exists( 'TbybInstall' ) ) {
    class TbybInstall
    {

        public static $tbyb_db_version = '1.0';

        public static function create_tbyb_prepared_carts_table() {

            global $wpdb;
            $table_name = $wpdb->prefix . 'tbyb_prepared_carts';
            $charset_collate = $wpdb->get_charset_collate();

            $sql = "CREATE TABLE $table_name (
  id int(11) NOT NULL AUTO_INCREMENT,
  user_id int(11) NOT NULL,
  product_id int(11) NOT NULL,
  variation_id int(11) DEFAULT NULL,
  imported_to_cart tinyint(1) DEFAULT '0' NOT NULL,
  ordered tinyint(1) DEFAULT '0' NOT NULL,
  quantity int(11) DEFAULT NULL,
  PRIMARY KEY  (id)
) $charset_collate;";

            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            dbDelta( $sql );

            add_option( 'tbyb_db_version', self::$tbyb_db_version );
        }
    }
}

This is my plugin-name.php file:

<?php
/**
 * Plugin Name:     Plugin name
 * Plugin URI:      plugin-uri
 * Description:     Description
 * Version:         1.0.0
 * Author:          Tahi reu
 * Author URI:      github-link
 * License:         GPL
 * License URI:     http://www.opensource.org/licenses/gpl-license.php
 */


/*
 * Prevent intruders from sneaking around
 */
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );



/*
 * Current plugin version - https://semver.org
 * This should be updated as new versions are released
 */
define( 'PLUGIN_NAME', '1.0.0' );



/*
 * Variables
 * */
const TEXT_DOMAIN = "plugin-name";



/*
 * Load functions
 */
require plugin_dir_path( __FILE__ ) . 'functions.php';



/*
 * Load classes
 */
require plugin_dir_path( __FILE__ ) . 'classes/class.PreparedCarts.php';
require plugin_dir_path( __FILE__ ) . 'classes/class.ReturnOptions.php';



/*
 * Check if WooCommerce is installed and active
 */
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {


    /* Create database table on plugin install */
    function create_tables(){

        require_once plugin_dir_path( __FILE__ ) . 'classes/class.TbybInstall.php';
        TbybInstall::create_tbyb_prepared_carts_table();

    }

    register_activation_hook( __FILE__, array( 'TbybInstall', 'create_tables' ) );


    /* Do the work */
    PreparedCarts::on_load();
    ReturnOptions::on_load();


} else {

    /* Abort and display info message */
    PreparedCarts::abort();

}

Any help/suggestions are appreciated. Thanks in advance.

5
  • 1
    The "unexpected output" is probably a fatal error message. Check your error logs. Nov 16, 2018 at 17:02
  • I see 2 spaces before the opening <?php in your class.TbybInstall.php file -- are they present on the server, or are they a copy-and-paste artifact present only here?
    – Pat J
    Nov 16, 2018 at 17:03
  • 1
    I don't have anything related to this in my debug.log file. Those two spaces somehow occurred when I pasted code here (I fixed that now). There is no any empty space before <?php in my .php file.
    – Tahi Reu
    Nov 16, 2018 at 17:29
  • Check your Apache (or whatever webserver you're using) error logs as well as debug.log.
    – Pat J
    Nov 16, 2018 at 17:36
  • Seems like there is no any recent errors in /var/log/apache2/error_log
    – Tahi Reu
    Nov 16, 2018 at 17:50

1 Answer 1

0

I solved this issue. Problem was in way I was using register_activation_hook.

I loaded TbybInstall along with other classes:

/*
 * Load classes
 */
require plugin_dir_path( __FILE__ ) . 'classes/class.PreparedCarts.php';
require plugin_dir_path( __FILE__ ) . 'classes/class.ReturnOptions.php';
require plugin_dir_path( __FILE__ ) . 'classes/class.TbybInstall.php';

...and I used register_activation_hook in a way which is more similar to official WordPress guide:

/* Create database table on plugin install */
function create_tables(){
    TbybInstall::create_tbyb_prepared_carts_table();
}

register_activation_hook( __FILE__, 'create_tables' );

And that's it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.