我做了一个触发器,我希望WordPress在激活插件时创建此触发器。我尝试了很多方法但未能创建触发器。但是如果我使用SQL Query通过phpMyAdmin创建。 PhpMyAdmin创建触发器。这意味着它在触发器的语法中没有问题。但正如我通过phpMyAdmin创建的,在停用插件时我写了删除触发器并触发了从phpMyAdmin真正删除的内容。这意味着用户有权删除触发器。因此,如果用户具有触发删除权限,则可以理解用户还具有创建触发器的权限。
以下是代码:
关于插件的激活: require_once(ABSPATH。' wp-admin / includes / upgrade.php');
global $wpdb;
$sql = "DELIMITER $$ CREATE TRIGGER message_filter BEFORE INSERT ON v2g_posts FOR EACH ROW BEGIN IF(NEW.post_type = 'wpestate_message') THEN SET NEW.post_status = 'draft'; END IF; END; $$ DELIMITER ;";
$wpdb->query($sql);
关于停用插件: 全球$ wpdb;
$sql = " DROP TRIGGER IF EXISTS message_filter; ";
$wpdb->query($sql);
这是完整的CODE:
function message_filter_activate() {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
global $wpdb;
$sql = "DELIMITER $$ CREATE TRIGGER message_filter BEFORE INSERT ON v2g_posts FOR EACH ROW BEGIN IF(NEW.post_type = 'wpestate_message') THEN SET NEW.post_status = 'draft'; END IF; END; $$ DELIMITER ;";
$wpdb->query($sql);
}
function message_filter_deactivate() {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
global $wpdb;
$sql = "DROP TRIGGER IF EXISTS message_filter;";
$wpdb->query($sql);
}
// register activation function
register_activation_hook(__FILE__, 'message_filter_activate');
// register deactivation function
register_deactivation_hook(__FILE__, 'message_filter_deactivate');