page  meta box  範例程式2 用Class 包起來

page meta box 範例程式2 用Class 包起來

class icblockMetaBox{
 /* Called by save_post action to save meta data in database. */
    function save_my_custom_meta_box($post_id,  $post, $update)/*$post contains info about the post and $post_id is assigned to the id of the post*/
    {
        /* Verify the nonce before proceeding. For security */
        if (!isset($_POST["qnimate-email-nonce"]) || !wp_verify_nonce($_POST["qnimate-email-nonce"], basename(__FILE__)))
          return $post_id;
      
        /* Check if the current user has permission to edit the post. For security */
        if(!current_user_can("edit_page", $post_id))
            return $post_id;

        //check if it is autosave
        if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
            return $post_id;

        //check if the post type is correct
        /*
        $slug = "page";
        if($slug != $post->post_type)
            return;
*/

        /* Store the user enter value in a variable */
        $user_entered_email_value = "";
        if(isset($_POST["author-email"]))
        {
            $user_entered_email_value = $_POST["author-email"];
        }
        else
        {
            $user_entered_email_value = "";
        }
        update_post_meta(get_the_ID(), "author-email", $user_entered_email_value);
    }




    /*Callback*/
    function meta_box_markup($object)/*$object conatins the information about the post*/
    {
        /*nonce for security purpose. more on it at httpss://codex.wordpress.org/WordPress_Nonces */
        wp_nonce_field(basename(__FILE__), 'qnimate-email-nonce');




        ?>
            <!-- Markup inside the custom meta box -->
            <div>
                <label for="author-email">E-Mail Of Author</label>
                <!-- Retrieve the email from database. If not yet set then empty string is returned. -->
                <input name="author-email" type="email" validate="true" value="<?php echo get_post_meta(get_the_ID(), "author-email", true); ?>">
            </div>
        <?php
    }




    function my_custom_meta_box()
    {
        add_meta_box("qnimate-meta-box", "Author Information", array( 'icblockMetaBox',"meta_box_markup"), "page", "normal", "default", null);
    }
}
    add_action("add_meta_boxes", array( 'icblockMetaBox', "my_custom_meta_box"));
    add_action("save_post",array( 'icblockMetaBox', "save_my_custom_meta_box" ), 10, 3);