Wednesday, November 12, 2025

#7 Steps - Implementation

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

  1. In the WordPress admin panel → Books > Add New.

  2. You’ll see the Book Details meta box below the content area.

  3. Enter a title, description, and author name, then click Publish.

  4. 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.php template.


✅ 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} hook Runs 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 supports in CPT to add/remove features like comments or excerpts.



No comments:

Post a Comment

Welcome to RMC online Tutorials

WordPress Plugin Development – Full Course Index WordPress Plugin Development 12-Module Learning Portal Mod...