我得到了著名的“插件在激活期间生成了xxx个字符的意外输出。”激活正在使用的插件时出错。
终端显示我的文件在us-ascii, 但当我在PHP风暴中处理这些文件时UTF-8 标记显示在右下角。
我想我没有空的空格,因为我的PHP风暴会在命令+s上自动删除它们。
我试过:
有无:?>
带和不带$table\\u name动态前缀声明$tbyb_db_version
(版本)声明add_option
和update_option
对于add_option( \'tbyb_db_version\', self::$tbyb_db_version );
这是我的class.TbybInstall.php
类别:<?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 );
}
}
}
这是我的plugin-name.php
文件:<?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();
}
欢迎提供任何帮助/建议。提前谢谢。
最合适的回答,由SO网友:Tahi Reu 整理而成
我解决了这个问题。问题在于我使用的方式register_activation_hook
.
我加载了TbybInstall
与其他课程一起:
/*
* 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\';
。。。我用过
register_activation_hook
在某种程度上更类似于
official WordPress guide:
/* Create database table on plugin install */
function create_tables(){
TbybInstall::create_tbyb_prepared_carts_table();
}
register_activation_hook( __FILE__, \'create_tables\' );
就这样。