add_meta_box

/**
 * Calls the class on the post edit screen.
 */
function call_someClass() {
    new someClass();
}
 
if ( is_admin() ) {
    add_action( 'load-post.php',     'call_someClass' );
    add_action( 'load-post-new.php', 'call_someClass' );
}
 
/**
 * The Class.
 */
class someClass {
 
    /**
     * Hook into the appropriate actions when the class is constructed.
     */
    public function __construct() {
        add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
        add_action( 'save_post',      array( $this, 'save'         ) );
    }
 
    /**
     * Adds the meta box container.
     */
    public function add_meta_box( $post_type ) {
        // Limit meta box to certain post types.
        $post_types = array( 'post', 'page' );
 
        if ( in_array( $post_type, $post_types ) ) {
            add_meta_box(
                'some_meta_box_name',
                __( 'Some Meta Box Headline', 'textdomain' ),
                array( $this, 'render_meta_box_content' ),
                $post_type,
                'advanced',
                'high'
            );
        }
    }
 
    /**
     * Save the meta when the post is saved.
     *
     * @param int $post_id The ID of the post being saved.
     */
    public function save( $post_id ) {
 
        /*
         * We need to verify this came from the our screen and with proper authorization,
         * because save_post can be triggered at other times.
         */
 
        // Check if our nonce is set.
        if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) ) {
            return $post_id;
        }
 
        $nonce = $_POST['myplugin_inner_custom_box_nonce'];
 
        // Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
            return $post_id;
        }
 
        /*
         * If this is an autosave, our form has not been submitted,
         * so we don't want to do anything.
         */
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }
 
        // Check the user's permissions.
        if ( 'page' == $_POST['post_type'] ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }
 
        /* OK, it's safe for us to save the data now. */
 
        // Sanitize the user input.
        $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
 
        // Update the meta field.
        update_post_meta( $post_id, '_my_meta_value_key', $mydata );
    }
 
 
    /**
     * Render Meta Box content.
     *
     * @param WP_Post $post The post object.
     */
    public function render_meta_box_content( $post ) {
 
        // Add an nonce field so we can check for it later.
        wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
 
        // Use get_post_meta to retrieve an existing value from the database.
        $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
 
        // Display the form, using the current value.
        ?>
        <label for="myplugin_new_field">
            <?php _e( 'Description for this field', 'textdomain' ); ?>
        </label>
        <input type="text" id="myplugin_new_field" name="myplugin_new_field" value="<?php echo esc_attr( $value ); ?>" size="25" />
        <?php
    }
}

5
1
Awgiedawgie 440220 points

                                    function custom_meta_box_markup($object)
{
    wp_nonce_field(basename(__FILE__), &quot;meta-box-nonce&quot;);

    ?&gt;
        &lt;div&gt;
            &lt;label for=&quot;meta-box-text&quot;&gt;Text&lt;/label&gt;
            &lt;input name=&quot;meta-box-text&quot; type=&quot;text&quot; value=&quot;&lt;?php echo get_post_meta($object-&gt;ID, &quot;meta-box-text&quot;, true); ?&gt;&quot;&gt;

            &lt;br&gt;

            &lt;label for=&quot;meta-box-dropdown&quot;&gt;Dropdown&lt;/label&gt;
            &lt;select name=&quot;meta-box-dropdown&quot;&gt;
                &lt;?php 
                    $option_values = array(1, 2, 3);

                    foreach($option_values as $key =&gt; $value) 
                    {
                        if($value == get_post_meta($object-&gt;ID, &quot;meta-box-dropdown&quot;, true))
                        {
                            ?&gt;
                                &lt;option selected&gt;&lt;?php echo $value; ?&gt;&lt;/option&gt;
                            &lt;?php    
                        }
                        else
                        {
                            ?&gt;
                                &lt;option&gt;&lt;?php echo $value; ?&gt;&lt;/option&gt;
                            &lt;?php
                        }
                    }
                ?&gt;
            &lt;/select&gt;

            &lt;br&gt;

            &lt;label for=&quot;meta-box-checkbox&quot;&gt;Check Box&lt;/label&gt;
            &lt;?php
                $checkbox_value = get_post_meta($object-&gt;ID, &quot;meta-box-checkbox&quot;, true);

                if($checkbox_value == &quot;&quot;)
                {
                    ?&gt;
                        &lt;input name=&quot;meta-box-checkbox&quot; type=&quot;checkbox&quot; value=&quot;true&quot;&gt;
                    &lt;?php
                }
                else if($checkbox_value == &quot;true&quot;)
                {
                    ?&gt;  
                        &lt;input name=&quot;meta-box-checkbox&quot; type=&quot;checkbox&quot; value=&quot;true&quot; checked&gt;
                    &lt;?php
                }
            ?&gt;
        &lt;/div&gt;
    &lt;?php  
}

5 (1 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source