如何在自定义 post 类型的管理屏幕编辑页面上显示自定义数据?
How to show custom data on custom post type admin screen edit page?
我已创建查询表格。当用户填写此表单时,我将其作为自定义 post 类型保存到数据库中。在后端,我需要在自定义 post 编辑页面上显示额外信息。但是没有找到任何钩子。
我试过这个代码:
[![function add_extra_info_column( $columns ) {
return array_merge( $columns,
array( 'sticky' => __( 'Extra Info', 'your_text_domain' ) ) );
}
add_filter( 'manage_posts_columns' , 'add_extra_info_column' );][1]][1]
但它在自定义 post 中添加了一个新列。
当我们点击每个 post 的编辑页面 link 时,我需要显示额外的信息。
这只是一个示例,您必须根据需要对其进行自定义:
第一步: 将 Meta 容器挂钩添加到后端(例如这里的 products post type):
add_action( 'add_meta_boxes', 'extra_info_add_meta_boxes' );
if ( ! function_exists( 'extra_info_add_meta_boxes' ) )
{
function extra_info_add_meta_boxes()
{
global $post;
add_meta_box( 'extra_info_data', __('Extra Info','your_text_domain'), 'extra_info_data_content', 'product', 'side', 'core' );
}
}
(将 'product'
替换为您的 post 类型,此元框可能会在 'side'
或 'normal' 上出现主栏)
第二步: 在此 metabox 中添加信息(字段、数据等...)
function extra_info_data_content()
{
global $post;
// Here you show your data <=====
}
参考文献:
How to add custom fields for WooCommerce order page (admin)?
How to add option to woo commerce edit order page?
第三步(optional):保存自定义meta的数据post(如果有一些字段需要).
add_action( 'save_post', 'extra_info_save_woocommerce_other_fields', 10, 1 );
if ( ! function_exists( 'extra_info_save_woocommerce_other_fields' ) )
{
function extra_info_save_woocommerce_other_fields( $post_id )
{
// Check if our nonce is set.
if ( ! isset( $_POST[ 'extra_info_other_meta_field_nonce' ] ) )
{
return $post_id;
}
$nonce = $_REQUEST[ 'extra_info__other_meta_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
{
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, its safe for us to save the data now. !!! --- */
// Sanitize user input and Update the meta field in the database.
}
}
我已创建查询表格。当用户填写此表单时,我将其作为自定义 post 类型保存到数据库中。在后端,我需要在自定义 post 编辑页面上显示额外信息。但是没有找到任何钩子。
我试过这个代码:
[![function add_extra_info_column( $columns ) {
return array_merge( $columns,
array( 'sticky' => __( 'Extra Info', 'your_text_domain' ) ) );
}
add_filter( 'manage_posts_columns' , 'add_extra_info_column' );][1]][1]
但它在自定义 post 中添加了一个新列。
当我们点击每个 post 的编辑页面 link 时,我需要显示额外的信息。
这只是一个示例,您必须根据需要对其进行自定义:
第一步: 将 Meta 容器挂钩添加到后端(例如这里的 products post type):
add_action( 'add_meta_boxes', 'extra_info_add_meta_boxes' );
if ( ! function_exists( 'extra_info_add_meta_boxes' ) )
{
function extra_info_add_meta_boxes()
{
global $post;
add_meta_box( 'extra_info_data', __('Extra Info','your_text_domain'), 'extra_info_data_content', 'product', 'side', 'core' );
}
}
(将 'product'
替换为您的 post 类型,此元框可能会在 'side'
或 'normal' 上出现主栏)
第二步: 在此 metabox 中添加信息(字段、数据等...)
function extra_info_data_content()
{
global $post;
// Here you show your data <=====
}
参考文献:
How to add custom fields for WooCommerce order page (admin)?
How to add option to woo commerce edit order page?
第三步(optional):保存自定义meta的数据post(如果有一些字段需要).
add_action( 'save_post', 'extra_info_save_woocommerce_other_fields', 10, 1 );
if ( ! function_exists( 'extra_info_save_woocommerce_other_fields' ) )
{
function extra_info_save_woocommerce_other_fields( $post_id )
{
// Check if our nonce is set.
if ( ! isset( $_POST[ 'extra_info_other_meta_field_nonce' ] ) )
{
return $post_id;
}
$nonce = $_REQUEST[ 'extra_info__other_meta_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
{
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, its safe for us to save the data now. !!! --- */
// Sanitize user input and Update the meta field in the database.
}
}