[英]Wordpress - Add custom field to custom post created programmatically
I have created a custom plugin on Wordpress that creates a custom post on installation.我在 Wordpress 上创建了一个自定义插件,它在安装时创建了一个自定义帖子。 The code I use is as follows:我使用的代码如下:
function custom_post_type() {
$labels = array(
'name' => _x( 'Books', 'Post Type General Name', 'my-custom-domain' ),
'singular_name' => _x( 'Book', 'Post Type Singular Name', 'my-custom-domain' ),
'menu_name' => __( 'Book', 'my-custom-domain' ),
'parent_item_colon' => __( 'Parent Book', 'my-custom-domain' ),
'all_items' => __( 'All Books', 'my-custom-domain' ),
'view_item' => __( 'Show Book', 'my-custom-domain' )
);
$args = array(
'label' => __( 'Books', 'my-custom-domain' ),
'description' => __( 'Books and news', 'my-custom-domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'genres' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'libri', $args );
}
add_action( 'init', 'custom_post_type', 0 );
What I would like to do is to be able to create a custom field (again from code) to be inserted under this function and add it to the post created above.我想要做的是能够创建一个自定义字段(再次通过代码)插入到这个 function 下并将其添加到上面创建的帖子中。
A custom field such as a text field with a title and a label box in which to insert text, or a field consisting of two text boxes.自定义字段,例如带有标题的文本字段和用于插入文本的 label 框,或由两个文本框组成的字段。
How can I create it in code and add it to the post so that when I do "Add new" I get this field to fill in?我如何在代码中创建它并将其添加到帖子中,以便在我执行“添加新”时我可以填写此字段?
There's some really great tutorials that cover this.有一些非常棒的教程涵盖了这一点。 My answer is taken from this one written by Aileen Javier from WPMU Dev我的答案摘自WPMU Dev 的 Aileen Javier 所写的这篇文章
It's a 3 step process to create custom metaboxes on a page.在页面上创建自定义元数据框是一个 3 步过程。 The first step is to create the metabox using the add_meta_box() function第一步是使用add_meta_box() function 创建 metabox
function libri_add_meta_box( $post ){
add_meta_box( 'libri_meta_box', 'Book Attributes', 'libri_build_meta_box', 'libri', 'side', 'low' );
}
add_action( 'add_meta_boxes_libri', 'libri_add_meta_box' );
The next step is to create a function which we use as the callback parameter in our add_meta_box (in this case 'libri_build_meta_box') - We use this to create inputs for our custom fields.下一步是创建一个 function,我们将其用作 add_meta_box 中的回调参数(在本例中为“libri_build_meta_box”)——我们使用它来为我们的自定义字段创建输入。 In this instance a text field for Book Author.在本例中,Book Author 的文本字段。
function libri_build_meta_box( $post ){
wp_nonce_field( basename( __FILE__ ), 'libri_meta_box_nonce' );
$book_author = get_post_meta( $post->ID, '_book_author', true); ?>
<div class='inside'>
<h3>Book Author</h3>
<p>
<input type="text" name="book_author" value="<?php echo $book_author; ?>" />
</p>
</div>
<?php }
And the final step is to hook into the save_post_{$post->post_type} action, to take the request and then update the post_meta when the post is saved最后一步是挂接到save_post_{$post->post_type}操作,接受请求,然后在保存帖子时更新 post_meta
function libri_save_meta_box_data( $post_id ){
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
if ( isset( $_REQUEST['book_author'] ) ) {
update_post_meta( $post_id, '_book_author', sanitize_text_field( $_POST['book_author'] ) );
}
}
add_action( 'save_post_libri', 'libri_save_meta_box_data', 10, 2 );
This will create a metabox on the side of the page that has a single text input for a book author.这将在页面一侧创建一个 metabox,其中有一个书籍作者的单一文本输入。 I'd really recommend reading the whole tutorial from wpmudev, because it's really informative and helpful.我真的建议您阅读 wpmudev 的整个教程,因为它确实提供了丰富的信息和帮助。
Here's the full code from my example:这是我示例中的完整代码:
<?php function libri_add_meta_box( $post ){
add_meta_box( 'libri_meta_box', 'Book Attributes', 'libri_build_meta_box', 'libri', 'side', 'low' );
}
add_action( 'add_meta_boxes_libri', 'libri_add_meta_box' );
function libri_build_meta_box( $post ){
wp_nonce_field( basename( __FILE__ ), 'libri_meta_box_nonce' );
$book_author = get_post_meta( $post->ID, '_book_author', true); ?>
<div class='inside'>
<h3>Book Author</h3>
<p>
<input type="text" name="book_author" value="<?php echo $book_author; ?>" />
</p>
</div>
<?php }
function libri_save_meta_box_data( $post_id ){
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
if ( isset( $_REQUEST['book_author'] ) ) {
update_post_meta( $post_id, '_book_author', sanitize_text_field( $_POST['book_author'] ) );
}
}
add_action( 'save_post_libri', 'libri_save_meta_box_data', 10, 2 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.