admin管理员组

文章数量:1287775

I'm creating a wordpress plugin which will create a custom post with custom fields like "time", "rating" etc whose will be fillable in admin. So far all tutorials I've seen only showing how enable "custom-fields" in the plugin, but I want those fields to be predefined and available in the post creation form. so far my plugin is :

    function create_test_recipe_cpt() {

    $labels = array(
        'name' => _x( 'Test Recipe', 'Post Type General Name', 'textdomain' ),
        'singular_name' => _x( 'Test Recipe', 'Post Type Singular Name', 'textdomain' ),
        ...
    );
    $args = array(
        'label' => __( 'Test Recipe', 'textdomain' ),
        'description' => __( '', 'textdomain' ),
        'labels' => $labels,
        'menu_icon' => '',
        'supports' => array('title','excerpt', 'thumbnail'),
        'taxonomies' => array(),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'hierarchical' => false,
        'exclude_from_search' => false,
        'show_in_rest' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'register_meta_box_cb' => 'cooking_time_meta_box' //<--added cb
    );
    register_post_type( 'custompost', $args );

    }       
    
    add_action( 'init', 'create_test_recipe_cpt', 0 );

    function cooking_time_meta_box() {

    $screens = get_post_types();

    foreach ( $screens as $screen ) {
        add_meta_box(
            'cooking-time',
            __( 'Cooking Time', 'sitepoint' ),
            'cooking_time_meta_box_callback',
            $screen
        );

    }
}

function cooking_time_meta_box_callback( $post ) {

    wp_nonce_field( 'cooking_time_nonce', 'cooking_time_nonce' );

    $value = get_post_meta( $post->ID, '_cooking_time', true );

    echo '<input type="number" style="width:100%" id="cooking_time" name="cooking_time" value="' . esc_attr( $value ) . '" />';
}

add_action( 'init', 'create_test_recipe_cpt', 0 );


function save_cooking_time_meta_box_data( $post_id ) {

    // Check if our nonce is set.
    if ( ! isset( $_POST['cooking_time'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['cooking_time'], 'cooking_time' ) ) {
        return;
    }

    // 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;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    }
    else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    if ( ! isset( $_POST['cooking_time'] ) ) {
        return;
    }

    $my_data = sanitize_text_field( $_POST['cooking_time'] );

    update_post_meta( $post_id, '_cooking_time', $my_data );
}
    
    add_action( 'save_post', 'save_cooking_time_meta_box_data' );

    function cooking_time_before_post( $content ) {

    global $post;

    $cooking_time = esc_attr( get_post_meta( $post->ID, '_cooking_time', true ) );

    $notice = "<div class='sp_cooking_time'>$cooking_time</div>";

    return $notice . $content;

}

add_filter( 'the_content', 'cooking_time_before_post' );

please suggest how to preset those field programmatically

UPDATED: so far I managed to add one field called cooking_time by using register_meta_box_cb. But new field is not being filled up after saving changes of post editing

本文标签: pluginsPreset custom fields