函数原型:
add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null, string $context = 'advanced', string $priority = 'default', array $callback_args = null )
将属性框添加到一个或多个屏幕
参数说明:
$id 属性框ID。
$title 属性框的标题。
$callback 用于在属性框中填充所需内容。
$screen 属性框的一个或多个屏幕(如帖子类型,“link”或“comment”)。
$context 属性框应显示的屏幕中的上下文。可用的上下文因屏幕而异。编辑后的屏幕上下文包括“normal”、“side”和“advanced”。评论屏幕上下文包括“normal”和“side”。菜单元框(手风琴部分)都使用“side”上下文。全局默认值为“advanced”。
$priority 属性框应显示的上下文中的优先级。接受“high”、“core”、“normal”或“low”。默认值“default”。
$callback_args 应设置为box数组的$args属性的数据(这是传递给回调的第二个参数)。默认值:null
函数源码:
function add_meta_box( $id, $title, $callback, $screen = null, $context = 'advanced', $priority = 'default', $callback_args = null ) {
global $wp_meta_boxes;
if ( empty( $screen ) ) {
$screen = get_current_screen();
} elseif ( is_string( $screen ) ) {
$screen = convert_to_screen( $screen );
} elseif ( is_array( $screen ) ) {
foreach ( $screen as $single_screen ) {
add_meta_box( $id, $title, $callback, $single_screen, $context, $priority, $callback_args );
}
}
if ( ! isset( $screen->id ) ) {
return;
}
$page = $screen->id;
if ( ! isset( $wp_meta_boxes ) ) {
$wp_meta_boxes = array();
}
if ( ! isset( $wp_meta_boxes[ $page ] ) ) {
$wp_meta_boxes[ $page ] = array();
}
if ( ! isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
$wp_meta_boxes[ $page ][ $context ] = array();
}
foreach ( array_keys( $wp_meta_boxes[ $page ] ) as $a_context ) {
foreach ( array( 'high', 'core', 'default', 'low' ) as $a_priority ) {
if ( ! isset( $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ] ) ) {
continue;
}
// If a core box was previously removed, don't add.
if ( ( 'core' === $priority || 'sorted' === $priority )
&& false === $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]
) {
return;
}
// If a core box was previously added by a plugin, don't add.
if ( 'core' === $priority ) {
/*
* If the box was added with default priority, give it core priority
* to maintain sort order.
*/
if ( 'default' === $a_priority ) {
$wp_meta_boxes[ $page ][ $a_context ]['core'][ $id ] = $wp_meta_boxes[ $page ][ $a_context ]['default'][ $id ];
unset( $wp_meta_boxes[ $page ][ $a_context ]['default'][ $id ] );
}
return;
}
// If no priority given and ID already present, use existing priority.
if ( empty( $priority ) ) {
$priority = $a_priority;
/*
* Else, if we're adding to the sorted priority, we don't know the title
* or callback. Grab them from the previously added context/priority.
*/
} elseif ( 'sorted' === $priority ) {
$title = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['title'];
$callback = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['callback'];
$callback_args = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['args'];
}
// An ID can be in only one priority and one context.
if ( $priority !== $a_priority || $context !== $a_context ) {
unset( $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ] );
}
}
}
if ( empty( $priority ) ) {
$priority = 'low';
}
if ( ! isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
$wp_meta_boxes[ $page ][ $context ][ $priority ] = array();
}
$wp_meta_boxes[ $page ][ $context ][ $priority ][ $id ] = array(
'id' => $id,
'title' => $title,
'callback' => $callback,
'args' => $callback_args,
);
}
使用举例:
/**
* Calls the class on the post edit screen.
*/
function call_someClass() {
new someClass();
}
if ( is_admin() ) {
add_action( 'load-post.php', 'call_someClass' );
add_action( 'load-post-new.php', 'call_someClass' );
}
/**
* The Class.
*/
class someClass {
/**
* Hook into the appropriate actions when the class is constructed.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
}
/**
* Adds the meta box container.
*/
public function add_meta_box( $post_type ) {
// Limit meta box to certain post types.
$post_types = array( 'post', 'page' );
if ( in_array( $post_type, $post_types ) ) {
add_meta_box(
'some_meta_box_name',
__( 'Some Meta Box Headline', 'textdomain' ),
array( $this, 'render_meta_box_content' ),
$post_type,
'advanced',
'high'
);
}
}
/**
* Save the meta when the post is saved.
*
* @param int $post_id The ID of the post being saved.
*/
public function save( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) ) {
return $post_id;
}
$nonce = $_POST['myplugin_inner_custom_box_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
return $post_id;
}
/*
* If this is an autosave, our form has not been submitted,
* so we don't want to do anything.
*/
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
/* OK, it's safe for us to save the data now. */
// Sanitize the user input.
$mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field.
update_post_meta( $post_id, '_my_meta_value_key', $mydata );
}
/**
* Render Meta Box content.
*
* @param WP_Post $post The post object.
*/
public function render_meta_box_content( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
// Use get_post_meta to retrieve an existing value from the database.
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
// Display the form, using the current value.
?>
<label for="myplugin_new_field">
<?php _e( 'Description for this field', 'textdomain' ); ?>
</label>
<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="<?php echo esc_attr( $value ); ?>" size="25" />
<?php
}
}
-
WordPress函数:esc_attr__ 转义属性及翻译WordPress函数:esc_attr__ 转义属性及翻译
-
WordPress函数:esc_attr_e 属性转义、翻译、显示WordPress函数:esc_attr_e 属性转义、翻译、显示
-
WordPress函数:esc_attr_x 带上下文的转义属性,翻译显示WordPress函数:esc_attr_x 带上下文的转义属性,翻译显示
-
WP Multilang WordPress翻译插件WP Multilang插件的主要功能是提供多语言支持,使用户能够创建多语言版本的网站,满足不同语言用户的需求。
-
WordPress公司官网主题在众多的WordPress企业官网主题中,追格公司推出了多款关于WordPress企业官网主题作品。我们之前已经分享过一些关于追格的企业官网主题,包括收费和免费版本,这些主题都因其独特的设计和出色的功能而深受用户喜爱。
-
WordPress必备:使用wp_get_theme()函数获取当前主题详情在WordPress中,wp_get_theme() 函数用于获取当前启用的主题或指定主题的信息。这个函数返回一个 WP_Theme 对象,该对象包含了主题的详细信息,如主题名称、版本、模板目录、样式表目录等。
暂无评论,抢个沙发...