且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WordPress-将额外的列添加到wp_posts并发布到它

更新时间:2023-11-30 23:28:58

如果您已将字段手动添加到wp_posts表中,则只需使用一些钩子即可将该字段添加到帖子页面中然后保存.

If you've already manually added the field to the wp_posts table, then you'll just need to use a few hooks to add the field to the posts page and then save it.

// Function to register the meta box
function add_meta_boxes_callback( $post_type, $post ) {
    add_meta_box( 'my_field', 'My Field', 'output_my_meta_box', 'post' );
}
add_action( 'add_meta_boxes', 'add_meta_boxes_callback', 10, 2 );

// Function to actually output the meta box
function output_my_meta_box( $post ) {
    echo '<input type="text" name="my_field" value="' . $post->my_field . '" />';
}

// Function to save the field to the DB
function wp_insert_post_data_filter( $data, $postarr ) {
    $data['my_field'] = $_POST['my_field'];
    return $data;
}
add_filter( 'wp_insert_post_data', 'wp_insert_post_data_filter', 10, 2 );