WordPress 插件开发 激活插件时添加通知 代码实例

最近我们在重构WordPress采集插件WP-JPost,这里记录一下当我们激活插件时在面板中添加通知的方法。

使用场景

我们插件使用register_post_type方法来构建了自定义文章(wordpress使用register_post_type 函数创建自定义文章类型)后出现了404的错误。这里我们使用添加通知的方法来告知用户必须设置一下【固定链接】(WordPress 固定链接设置)才能正常使用。下面我们记录一下具体的实现方法。

WordPress 插件开发 激活插件时添加通知 代码实例

添加通知方法

可以通过admin_notices的add_action来实现。

		add_action( 'admin_notices', array( $this, 'jpost_notice' ) );

实现方法函数

做好输出工作即可。

	public function jpost_notice(){ 		if( get_post_type() == $this->post_type ) 			printf('<div class="jads"></div>'); 		/* Check transient, if available display notice */ 		if (! get_option('permalink_structure') ){ 			printf('<div class="updated notice notice-warning is-dismissible">'); 			printf('<p>%s</p>', __( 'WP-JPost need Navigate to [ <strong>Dashboard</strong> ] -> [ <strong>Settings</strong> ] -> [ <strong>Permalink</strong> ] page -> click [ <strong>Save</strong> ] button to flush rewrite rules. [ <a href="https://jiloc.com/44235.html">?</a> ]') ); 			printf('</div>'); 			/* Delete transient, only display this notice once. */ 			// delete_transient( 'jpost-notice' ); 		} 	}

激活插件事件

当没有设置固定链接的时候输出 jpost-notice 的通知。

	static function active() { 		if (! get_option('permalink_structure') ) 			set_transient( 'jpost-notice', true ); 		flush_rewrite_rules(); 	}

全部代码

//register activation function register_activation_hook( __FILE__, array( 'WP_JPost', 'active') ); //register deactivation function register_deactivation_hook( __FILE__ , array( 'WP_JPost', 'deactive') ); class WP_JPost { 	protected $jpost_readme = 'https://jiloc.com/wp-jpost'; 	protected $post_type = 'wp-jpost'; 	protected $rewrite_slug = 'jpost'; 	/** 	 * Constructor. 	 */ 	public function __construct() {                 add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); 		add_action( 'init', array( $this, 'register_post_type' ) ); 		// add_filter( 'post_type_link', array($this , 'custom_link' ), 1, 3); 		add_filter( 'query_vars', array( $this, 'jpost_query_vars') ); 		add_filter( 'post_updated_messages', array( $this, 'updated_message' ) ); 		// add_action( 'admin_init',array($this,'register_setting_page')); 		add_action( 'admin_menu', array( $this, 'add_meta_box' ) ); 		add_action( 'save_post', array( $this, 'meta_box_save' ), 1, 2 ); 		add_action( 'manage_wp-jpost_posts_custom_column', array( $this, 'columns_data' ) ); 		add_filter( 'manage_wp-jpost_posts_columns', array( $this, 'columns_filter' ) ); 		add_action( 'template_redirect', array( $this, 'jpost_grab' ) ); 		add_action( 'admin_notices', array( $this, 'jpost_notice' ) ); 	} 	 	public function custom_link( $permalink, $post ){ 		if ( $post->post_type == $this->post_type ){ 			return $permalink . ( strpos( $permalink ,'?' ) !== false ? '&' : '?') . 'jpage='.get_post_meta( get_the_ID(), '_jpost_task_list_max_page', true ) . '&debug=1'; 		} else { 			return $permalink; 		} 	}  	public function jpost_notice(){ 		if( get_post_type() == $this->post_type ) 			printf('<div class="jads"></div>'); 		/* Check transient, if available display notice */ 		if (! get_option('permalink_structure') ){ 			printf('<div class="updated notice notice-warning is-dismissible">'); 			printf('<p>%s</p>', __( 'WP-JPost need Navigate to [ <strong>Dashboard</strong> ] -> [ <strong>Settings</strong> ] -> [ <strong>Permalink</strong> ] page -> click [ <strong>Save</strong> ] button to flush rewrite rules. [ <a href="https://jiloc.com/44235.html">?</a> ]') ); 			printf('</div>'); 			/* Delete transient, only display this notice once. */ 			// delete_transient( 'jpost-notice' ); 		} 	}  	static function active() { 		if (! get_option('permalink_structure') ) 			set_transient( 'jpost-notice', true ); 		flush_rewrite_rules(); 	} }

腾讯云限时秒杀【点击购买】

搬瓦工,CN2高速线路,1GB带宽,电信联通优化KVM,延迟低,速度快,建站稳定,搬瓦工BandwagonHost VPS优惠码BWH26FXH3HIQ,支持<支付宝> 【点击购买】!

Vultr$3.5日本节点,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快【点击购买】!

阿里云香港、新加坡VPS/1核/1G/25G SSD/1T流量/30M带宽/年付¥288【点击购买】

WordPress 插件开发 激活插件时添加通知 代码实例

`微信`扫码 加好友

链接到文章: https://gkxyz.com/wordpress-chajiankaifa-jihuochajianshitianjiatongzhi-daimashili.html

推荐站点

评论已关闭