page metabox 範例程式1


/* 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
return $post_id;

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

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

function meta_box_markup($object)/*$object conatins the information about the post*/
/*nonce for security purpose. more on it at */
wp_nonce_field(basename(__FILE__), ‘qnimate-email-nonce’);

<!– Markup inside the custom meta box –>
<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); ?>">

function my_custom_meta_box()
Parameters for add_meta_box() are $id, $title, $callback, $page, $context, $priority, $callback_args
-> $id is a unqiue id given to every meta box
-> $title is the title displayed in custom meta box
-> $callback is a function that outputs markup inside the custom meta box
-> $page respresents the admin page on which the mata box will be displayed on. It can be page, post, custom post type
-> $context respresents the postiton of the meta box. It can be normal, advanced or side.
-> $priority is the position of the box insde the context. It can be high, core, default or low
-> $cannback_args is used to pass arguements to the callback function.
add_meta_box("qnimate-meta-box", "Author Information", "meta_box_markup", "page", "normal", "default", null);

/* This action is responsibe for creating and displaying meta boxes */
add_action("add_meta_boxes", "my_custom_meta_box");

/* Used to store the meta data in database */
add_action("save_post", "save_my_custom_meta_box" , 10, 3);

CH02用戶資料CH03基本APICH05 分類頁面Category.phpCH09Screen Options(顯示選項)CH10外掛pluginsCH11 add_actionCH12 add_filterCH13Meta Box(自訂區塊)CH14widget(自訂模組)CH15 DBCH16AJAXCH17ShortcodeCH20 wordpress muCH30WooCommerce