我试图在插件激活时创建表,我尝试在类内创建相同的函数并从构造函数调用它,我尝试在类外调用它,最后我尝试创建另一个php文件来使用register_activation_hook
调用它,但没有成功
插件代码:
<?php
/**
*@package pageauthors
**/
/**
* Plugin Name: Page Authors
* Plugin URI: https://www.localhost.com/plugin
* Description: Allows you to add multiple authors to your wordpress post.
* Version: 1.0.0
* Author: Raphael Eid.
* Author URI: https://www.localhost.com/
* License: GPL v2 or Later
* Text Domain: PageAuthors
*/
$db = mysqli_connect('localhost','x','x','x');
//We can use require_once('../../../wp-config.php');
//mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
defined('ABSPATH') or die('Error my friend');
class pageauthors{
function register(){
//Call the Scripts
add_action('admin_enqueue_scripts', array($this, 'enqueue'));
//Create in the admin function
add_action('admin_menu', array($this, 'add_admin_pages'));
}
public function add_admin_pages(){
//add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null )
add_menu_page('Add Author', 'Add Author', 'manage_options', 'raphaelprefix_plugin', array($this, 'admin_index'), 'dashicons-insert', 110);
}
public function admin_index(){
define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
include( MY_PLUGIN_PATH . 'templates/index.php');
// include( MY_PLUGIN_PATH . 'index.php');
include( MY_PLUGIN_PATH . 'templates/showAuthors.php');
// require_once plugin_dir_path(__FILE__) . 'templates/index.php';
}
function deactivate(){
global $wpdb;
$table_name = $wpdb->prefix . 'postsauthors';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
delete_option("my_plugin_db_version");
}
function enqueue(){
//Here we can enqueue and we should create the assets (css and js)
wp_enqueue_style('mypluginstyle', plugins_url('/assets/mystyle.css', __FILE__) );
wp_enqueue_script('mypluginstyle', plugins_url('/assets/mystyle.js', __FILE__) );
}
}
if(class_exists('pageauthors')){
$raphplugin = new pageauthors();
$raphplugin->register();
// $raphplugin->activate();
$raphplugin->deactivate();
}
function installer(){
include('installer.php');
}
//Activate - Register Activation Hook(__FILE__, array($instance, function))
register_activation_hook(__FILE__, 'installer');
//Deactivate - Register Deactivation Hook(__FILE__, array($instance, function))
register_deactivation_hook(__FILE__, array($raphplugin, 'deactivate') );
?>
installer.php
<?php
$db = mysqli_connect('localhost','x','x','x');
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
if (count($wpdb->get_var('SHOW TABLES LIKE "wp_postsauthors"')) == 0){
$sql = "CREATE TABLE `wp_postsauthors`
( `ID` INT(200) NOT NULL AUTO_INCREMENT , `post_ID` INT(200) NOT NULL ,
`author_name` VARCHAR(200) NOT NULL , `title` VARCHAR(200) NOT NULL ,
`description` VARCHAR(200) NOT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB;";
dbDelta($sql);
}
即使我把所有的东西都打对了,也没有任何东西能用
我要尽可能地简化事情。由于激活钩子具有类似异步的行为,因此调试起来总是很麻烦。
首先,您实际上不需要if
检查表是否存在,这就是dbdelta
已经为您做的事情。您可以稍后更改$SQL
中定义的模式,dbdelta
也将尝试解决这一问题,并为您自动处理。然而,在现实世界中,我发现半复杂的表逻辑并不总是通过alter table
。相反,大多数人跟踪他们的模式版本,并在插件更新时运行表升级。但那是以后再说的。
第二,永远不要使用WordPress表前缀,那是自找麻烦。而是始终使用$WPDB->prefix
。
下面的代码是一个插件的最小版本,它可以完成您所要寻找的功能。而不是文件中的函数,我只是使用一个本地匿名函数。代码的大部分与您的代码相同,只是我的IDE自动为我设置了一些格式。这段代码是一个完整的插件,我建议首先测试它。一旦您确认了它的工作,您应该能够拉出激活逻辑。使用另一个文件是没有错的,我只是试着在我调试东西的时候移除一样多的分心。
<?php
/**
* Plugin Name: Page Authors
*/
register_activation_hook(
__FILE__,
static function () {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
// Never assume the prefix
$table_name = $wpdb->prefix . 'postsauthors';
$sql = "CREATE TABLE `${table_name}`
(
`ID` INT(200) NOT NULL AUTO_INCREMENT ,
`post_ID` INT(200) NOT NULL ,
`author_name` VARCHAR(200) NOT NULL ,
`title` VARCHAR(200) NOT NULL ,
`description` VARCHAR(200) NOT NULL ,
PRIMARY KEY (`ID`)) ENGINE = InnoDB;";
dbDelta($sql);
}
);