Absolutely ✅ — here’s a clear, practical step-by-step guide to implement Module 7: Custom Post Types & Meta Boxes in your own WordPress plugin.
π§± Module 7 — Implementation Steps
Topic: Custom Post Types & Meta Boxes
πͺ Step 1 – Create or Open Your Plugin File
Go to your plugin folder, e.g.
/wp-content/plugins/my-first-plugin/Open or create your main plugin file (for example,
my-first-plugin.php).Make sure the header comment exists:
<?php /** * Plugin Name: My First Plugin * Description: Learning WordPress Plugin Development – Module 7. * Version: 1.0 * Author: You */πͺ Step 2 – Register a Custom Post Type (CPT)
Add this function below the header:
function mfp_register_books_cpt() { $labels = array( 'name' => 'Books', 'singular_name' => 'Book', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'menu_name' => 'Books' ); $args = array( 'labels' => $labels, 'public' => true, 'show_in_menu' => true, 'menu_icon' => 'dashicons-book-alt', 'supports' => array('title', 'editor', 'thumbnail'), 'has_archive' => true, ); register_post_type('book', $args); } add_action('init', 'mfp_register_books_cpt');π What it does:
Adds a new “Books” section in the WordPress admin sidebar.πͺ Step 3 – Add a Meta Box for Extra Fields
Add the meta box to your new post type:
function mfp_add_book_meta_box() { add_meta_box( 'mfp_book_details', 'Book Details', 'mfp_render_book_meta_box', 'book', 'normal', 'default' ); } add_action('add_meta_boxes', 'mfp_add_book_meta_box');Now create the callback to display the input:
function mfp_render_book_meta_box($post) { $author = get_post_meta($post->ID, '_mfp_book_author', true); wp_nonce_field('mfp_save_book_meta', 'mfp_book_meta_nonce'); echo '<label>Author Name:</label><br>'; echo '<input type="text" name="mfp_book_author" value="' . esc_attr($author) . '" style="width:100%;" />'; }π What it does:
Adds a new “Author Name” field under the “Book Details” box when editing a Book.πͺ Step 4 – Save the Meta Box Data
Add a save handler:
function mfp_save_book_meta($post_id) { if (!isset($_POST['mfp_book_meta_nonce']) || !wp_verify_nonce($_POST['mfp_book_meta_nonce'], 'mfp_save_book_meta')) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (isset($_POST['mfp_book_author'])) { update_post_meta($post_id, '_mfp_book_author', sanitize_text_field($_POST['mfp_book_author'])); } } add_action('save_post_book', 'mfp_save_book_meta');π What it does:
Securely saves the entered author name as post meta every time you save a Book.πͺ Step 5 – Test It
In the WordPress admin panel → Books > Add New.
You’ll see the Book Details meta box below the content area.
Enter a title, description, and author name, then click Publish.
Verify the author name is saved and editable on future edits.
πͺ Step 6 – Display Meta Data (Optional)
If you want to display the Author Name on the frontend (single book template):
echo '<p><strong>Author:</strong> ' . esc_html(get_post_meta(get_the_ID(), '_mfp_book_author', true)) . '</p>';You can place that in your theme’s
single-book.phptemplate.✅ Summary
Feature Purpose register_post_type()Adds a new content type (Books) add_meta_box()Creates an editable section in the editor get_post_meta()/update_post_meta()Retrieves and saves extra field data Nonces Prevents unauthorized form submissions save_post_{posttype}hookRuns when a CPT item is saved π‘ Pro Tips
Always sanitize inputs using
sanitize_text_field(),esc_attr(), etc.Add multiple meta boxes for richer data (price, ISBN, etc.).
Use
supportsin CPT to add/remove features like comments or excerpts.
Wednesday, November 12, 2025
#7 Steps - Implementation
Subscribe to:
Post Comments (Atom)
Welcome to RMC online Tutorials
WordPress Plugin Development – Full Course Index WordPress Plugin Development 12-Module Learning Portal Mod...
-
WordPress Plugin Development – Full Course Index WordPress Plugin Development 12-Module Learning Portal Mod...
-
WordPress Plugin Tutorial - My First Plugin π My First WordPress Plugin Tutorial Welcome! This is a simple example...
-
WordPress Plugin Tutorial - Modules 1 & 2 π WordPress Plugin Development Tutorial Learn step-by-step from b...
No comments:
Post a Comment