Create custom fields for a custom post type in WordPress to use in functions.php

|

To create custom fields for a custom post type in WordPress, you can use the add_meta_box function. Here’s an example of how you can use this function to create a custom field for a custom post type called “book”:

// Register the custom post type
function create_book_post_type() {
    register_post_type( 'book',
        array(
            'labels' => array(
                'name' => __( 'Books' ),
                'singular_name' => __( 'Book' )
            ),
            'public' => true,
            'has_archive' => true,
        )
    );
}
add_action( 'init', 'create_book_post_type' );

// Add the custom field to the custom post type
function add_book_custom_fields() {
    add_meta_box( 'book_custom_fields', 'Book Custom Fields', 'display_book_custom_fields', 'book', 'normal', 'default' );
}
add_action( 'add_meta_boxes', 'add_book_custom_fields' );

// Display the custom field in the admin interface
function display_book_custom_fields( $post ) {
    $book_author = get_post_meta( $post->ID, 'book_author', true );
    echo '<label for="book_author">Author: </label>';
    echo '<input type="text" id="book_author" name="book_author" value="' . $book_author . '" />';
}

// Save the custom field when the post is saved
function save_book_custom_fields( $post_id ) {
    if ( isset( $_POST['book_author'] ) ) {
        update_post_meta( $post_id, 'book_author', sanitize_text_field( $_POST['book_author'] ) );
    }
}
add_action( 'save_post', 'save_book_custom_fields' );

You can add more fields as per your requirement and use in the display_book_custom_fields function.

Here’s an example of how you can add more custom fields to the “book” custom post type:

function display_book_custom_fields( $post ) {
    $book_author = get_post_meta( $post->ID, 'book_author', true );
    echo '<label for="book_author">Author: </label>';
    echo '<input type="text" id="book_author" name="book_author" value="' . $book_author . '" />';

    $book_publisher = get_post_meta( $post->ID, 'book_publisher', true );
    echo '<label for="book_publisher">Publisher: </label>';
    echo '<input type="text" id="book_publisher" name="book_publisher" value="' . $book_publisher . '" />';

    $book_year = get_post_meta( $post->ID, 'book_year', true );
    echo '<label for="book_year">Year: </label>';
    echo '<input type="text" id="book_year" name="book_year" value="' . $book_year . '" />';
}

function save_book_custom_fields( $post_id ) {
    if ( isset( $_POST['book_author'] ) ) {
        update_post_meta( $post_id, 'book_author', sanitize_text_field( $_POST['book_author'] ) );
    }
    if ( isset( $_POST['book_publisher'] ) ) {
        update_post_meta( $post_id, 'book_publisher', sanitize_text_field( $_POST['book_publisher'] ) );
    }
    if ( isset( $_POST['book_year'] ) ) {
        update_post_meta( $post_id, 'book_year', sanitize_text_field( $_POST['book_year'] ) );
    }
}

In this example, I’ve added two additional fields: “Publisher” and “Year”. The code in the display_book_custom_fields function will display these fields in the admin interface, and the code in the save_book_custom_fields function will save the values of these fields when the post is saved.

You can add as many fields as you like and also you can use different types of input fields like textarea, select, checkbox, radio buttons etc.

Also, make sure to sanitize all the input fields before saving to the database for security reasons.

Similar Posts