问题描述
WordPress在主题、插件开发中,如果我们直接使用 ` $(‘#ID’) ` 这种写法就会报错:
Uncaught ReferenceError: jQuery is not defined
TypeError: $ is not a function
解决方法
jQuery(function($) {
// use $
});
WordPress在主题、插件开发中,如果我们直接使用 ` $(‘#ID’) ` 这种写法就会报错:
Uncaught ReferenceError: jQuery is not defined
TypeError: $ is not a function
jQuery(function($) {
// use $
});
WordPress插件/主题开发中,如何获取文章发表用户的ID ?
官方文档:
get_the_author_meta( string $field = '', int|false $user_id = false )Retrieves the requested data of the author of the current post.
Valid values for the $field parameter include:
$field
(string) (Optional) The user field to retrieve.
Default value: ”$user_id
(int|false) (Optional) User ID.
Default value: false
(string) The author’s field from the current author’s DB object, otherwise an empty string.
$author_id = get_post_field ('post_author', get_the_ID() );
最近我们在重构WordPress采集插件WP-JPost,这里记录一下当我们激活插件时在面板中添加通知的方法。
我们插件使用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://www.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://www.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://www.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();
}
}
前面我们讲述了利用 register_post_type 进行注册了自定义的文章(wordpress使用register_post_type 函数创建自定义文章类型), 然后我们为这个自定义的文章添加了自定义的设置页面。要把菜单放在一个栏目中。如下图所示:

下面我们直接给出示例代码。
require_once JPOST_DIR . '/includes/class-wp-jpost.php';
new WP_JPost();
if ( is_admin() ) {
require_once JPOST_DIR . '/includes/class-wp-jpost-settings.php';
new JPostSettings();
} add_action( 'admin_menu', array( $this, 'add_meta_box' ) ); public function add_meta_box() {
add_menu_page( 'wp_jpost', 'wp-jpost', 'administrator', 'wp-jpost', '', '', 100);
add_submenu_page('wp-jpost', __('Add New','wp-jpost'), __('Add New','wp-jpost'), 'administrator', 'post-new.php?post_type=wp-jpost', '' );
// do something
} register_post_type(
$slug,
array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => apply_filters( 'jposts_exclude_from_search', true ),
'show_ui' => true,
'query_var' => true,
'menu_position' => 999,
'supports' => $supports_array,
'rewrite' => array(
'slug' => $rewrite_slug,
'with_front' => false,
),
'show_in_menu' => 'wp-jpost',
'show_in_rest' => true,
)
);注意参数 show_in_menu 。这里设置显示的位置。

public function __construct(){
add_action( 'admin_menu', array( $this, 'settings' ) );
} // Add Menu
public function settings(){
add_submenu_page('wp-jpost', __('Settings','wp-jpost'), __('Settings','wp-jpost'), 'administrator', 'wp-jpost', array($this, 'create_admin_page'));
}
/**
* Options page callback
*/
public function create_admin_page(){
// Set class property
$this->options = get_option( '_jpost_setting' );
settings_errors();//show settings messages.
printf( '<div class="wrap"><h1>%s</h1><form method="post" action="options.php">',__('WP-JPost Settings','wp-jpost') );
settings_fields( '_jpost_option_group' );
do_settings_sections( '_jpost_settings' );
submit_button();
printf( '</form></div>' );
}
到这里我们的菜单就建立完成了。

最近写了wordpress 采集的脚本,但是插入时ed2k magnet等字符被自动过滤了,其实这是wordpress的自动保护体制。具体代码在wp-includes目录内找到post.php,搜索以下内容:
$postarr = sanitize_post($postarr, 'db'); //这行注释掉,就可以了。但是这样做整体后台就会失去保护了,下面我们提供另外一种方法,在脚本执行插入时添加代码如下:
kses_remove_filters(); //停止过滤字符
$post_ID = wp_insert_post( $my_post);//wordpress自带的插入功能
kses_init_filters();//重新开启过滤字符注意:此处只要是安全性能的过滤,比如PHP的’单引号等字符。
这样就不会影响整体影响使用啦~