How to do it...

Follow these steps to create and manage advanced post data through built-in custom fields:

  1. Log in to the WordPress Dashboard as an administrator.
  2. Click the Books menu item from the left menu to get the book list, as shown in the following screenshot:

  1. Click the Edit link of the book we created in the previous recipe.
  2. Click the More tools and options icon in the top right hand section of the screen to get a similar screen to the following:

  1. Click Options from the popup menu to get a screen similar to the following:

  1. Click Custom Fields option and click Enable and Reload button
  2. Scroll down to the Custom Fields section, as shown in the following screenshot:

  1. Click the Enter New link to create a new field.
  2. Add a unique key for the Name field. You can use alphanumeric values, dashes, and underscores.
  3. Add the custom field value to the Value field. We can use price as the Name and 25 as the Value.
  4. Click the Add Custom Field button to add another field.
  5. Use the Select field to choose an existing custom field key from the database. Add a Value for the custom field.
  6. Click the Update button to save the book's details.

The custom field data will be saved in the database for later use. This field data will not be visible on the frontend until we assign the data to the book template. 

In this case, we used the existing custom field feature of WordPress. Now, we can look into the process of adding our own custom fields using built-in hooks. Follow these steps to create custom fields and capture data for custom post types:

  1. Open the wpcookbookchapter7.php file of the WPCookbook Chapter 7 plugin in the code editor.
  2. Add the following code block at the end of the file to register a new meta box to display the custom fields:
add_action( 'add_meta_boxes', 'wpccp_chapter7_book_meta_box');
function wpccp_chapter7_book_meta_box(){
if(current_user_can('manage_options')){
add_meta_box(
'wpccp-chapter7-book-meta-box',
__( 'Book Custom Fields', 'wpccp_ch7' ),
'wpccp_chapter7_display_book_fields',
'book',
'normal',
'high'
);
}
}

// Step 3 code should be placed in the next line
  1. Add the following code block after the code in step 2 to display the custom fields for books in the book editing screen:
function wpccp_chapter7_display_book_fields($post){
global $wpdb;
$wpccp_book_price = get_post_meta( $post->ID,
'wpccp_book_price', true );
$wpccp_book_pages = get_post_meta( $post->ID,
'wpccp_book_pages', true );

$display .= "<p><span>".__('Book Pages :','wpccp_ch7').
"</span><input type='text' name='wpccp_book_pages'
value='".$wpccp_book_pages."' /></p>";
$display .= "<p><span>".__('Book Price :','wpccp_ch7').
"</span><input type='text' name='wpccp_book_price'
value='".$wpccp_book_price."' /></p>";

$display .= wp_nonce_field('wpccp_backend_book_nonce',
'wpccp_backend_book_nonce');
echo $display;
}

// Step 4 code should be placed after this line
  1. Add the following code block after the code in step 3 to save the custom data for books:
add_action( 'save_post', 'wpccp_chapter7_save_book_data',10,3 );
function wpccp_chapter7_save_book_data($post_id, $post, $update){
global $post,$wpdb;
if(isset($post->post_type) && $post->post_type != 'book'){
return;
}

if ( isset( $_POST['wpccp_backend_book_nonce'] ) && !
wp_verify_nonce( $_POST['wpccp_backend_book_nonce'],
'wpccp_backend_book_nonce' ) ) {
return;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}

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

$wpccp_book_price = isset( $_POST['wpccp_book_price'] ) ? (int)
($_POST['wpccp_book_price']) : '';
$wpccp_book_pages = isset( $_POST['wpccp_book_pages'] ) ? (int)
($_POST['wpccp_book_pages']) : '';

update_post_meta( $post_id, 'wpccp_book_price', $wpccp_book_price );
update_post_meta( $post_id, 'wpccp_book_pages', $wpccp_book_pages );
}
  1. Log in to the WordPress Dashboard as an administrator.
  2. Click the Books menu item on the left menu.
  3. Click the Add New button to create a book. You will see a screen similar to the following, showing a custom meta box with custom fields:

  1. Add a sample title and content for the post.
  2. Add Price and Pages for the book using the new custom fields section.
  3. Click the Publish button to publish the post.

Now, the book will be available in the frontend with default details such as title and content. However, the custom fields will not be visible in the frontend without modifications being made to the theme code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset