How add metabox to your WordPress posts. Without plugin open

How add metabox to your WordPress posts. Without plugin

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

A standard example of creating a text and select metabox with multiple security checks

Create in yours theme directory file advanced-post-widget.php

And add to it code below:


<?php

function expand_my_post_metabox() {

    add_meta_box('expand_post_rating', esc_html__('Expand Post Settings', 'clean'), 'expand_post_html', 'post', 'normal', 'high');

    // expan_post_rating - metabox id
    // esc_html__('Expand Post Settings', 'clean') - translation ready metabox area name
    // expand_post_html - function for output html
    // post - type of post for which we set the metabox
    // normal - metabox width
    // high - metabox priority
}

add_action('add_meta_boxes', 'expand_my_post_metabox');

function expand_post_html($post) {

    $rating_value = get_post_meta($post->ID, 'expand_rating_value', true);
    $expand_post_type = get_post_meta($post->ID, 'expand_post_type', true);
	
    
wp_nonce_field('noncename111', 'expand_my_post_metabox_verify');
    
    ?>

<p>
    <lebel for="expand_rating_value"><?php esc_html_e('Post Rating', 'clean'); ?></lebel>
    <input type="text" id="expand_rating_value" name="expand_rating_value"
        value="<?php echo esc_attr($rating_value); ?>">
</p>

<p>
    <lebel for="expand_post_type"><?php esc_html_e('Post Type', 'clean'); ?></lebel>
    <select id="expand_post_type" name="expand_post_type">
        <option value=""><?php esc_html_e('Select Post type', 'clean'); ?></option>
        <option value="review" <?php if($expand_post_type == 'review'){echo 'selected';}  ?>>
            <?php esc_html_e('Review', 'clean'); ?>
        </option>
        <option value="interview" <?php if($expand_post_type == 'interview'){echo 'selected';}  ?>>
            <?php esc_html_e('Interview', 'clean'); ?>
        </option>
        <option value="guide" <?php if($expand_post_type == 'guide'){echo 'selected';}  ?>>
            <?php esc_html_e('Guide', 'clean'); ?>
        </option>
    </select>
</p>


<?php

}

function expand_my_post_metabox_save($post_id, $post) {

    if(!isset($_POST['expand_my_post_metabox_verify']) || !wp_verify_nonce($_POST['expand_my_post_metabox_verify'], 'noncename111' )) {
        return $post_id;
    }

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

    if($post->post_type !='post') {
        return $post_id; 
    }
    
    $post_type = get_post_type_object($post->post_type);
    if(!current_user_can($post_type->cap->edit_post, $post_id)) {
        return $post_id;
    }
    
    if(isset($_POST['expand_rating_value'])) {
        update_post_meta($post_id, 'expand_rating_value', sanitize_text_field($_POST['expand_rating_value']));
    } else {
        delete_post_meta($post_id, 'expand_post_type',);
    }

    if(isset($_POST['expand_post_type'])) {
        update_post_meta($post_id, 'expand_post_type', sanitize_text_field($_POST['expand_post_type']));
    } else {
        delete_post_meta($post_id, 'expand_post_type',);
    }
    
    return $post_id;
    
}

add_action('save_post', 'expand_my_post_metabox_save', 10, 2);

?>

Attach your metabox to a file function.php with code

require get_template_directory() . '/advanced-post-widget.php';

Modify metabox to suit your needs. Good luck

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation