最近我们在重构WordPress采集插件WP-JPost,这里记录一下当我们激活插件时在面板中添加通知的方法。
Table of Contents
使用场景
我们插件使用register_post_type
方法来构建了自定义文章(wordpress使用register_post_type 函数创建自定义文章类型)后出现了404的错误。这里我们使用添加通知的方法来告知用户必须设置一下【固定链接】(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(); } }
评论已关闭