1.Xác định meta filed cho post type nào? Vị trí meta box ở đâu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function fcwordpress_add_meta_box() { //show which post type $screens = array( 'post' ); foreach ( $screens as $screen ) { add_meta_box( 'fcwordpress_sectionid', __( 'Meta Field', 'textdomain' ), //Function callback 'fcwordpress_meta_box_callback', $screen, //side: on the right //advanced: below editor //normal: below editor 'side', 'high' ); } } add_action( 'add_meta_boxes', 'fcwordpress_add_meta_box' ); |
2.Function callback chính là các thành phần của form và những thông tin chúng ta thêm vào
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function fcwordpress_meta_box_callback( $post ) { // Add an nonce field so we can check for it later. wp_nonce_field( 'fcwordpress_meta_box', 'tonyteegarden_meta_box_nonce' ); /* * Use get_post_meta() to retrieve an existing value * from the database and use the value for the form. */ $fcwordpress_this_is_podcast = get_post_meta( $post->ID, 'fcwordpress_this_is_podcast', true ); echo '<label for="fcwordpress_new_field">'; _e( 'Check for set this post is podcast', 'textdomain' ); echo '</label>'; echo '<input '.checked($fcwordpress_this_is_podcast,"1").' type="checkbox" value="1" id="fcwordpress_this_is_podcast" name="fcwordpress_this_is_podcast" >'; } |
3. Xử lý lưu vào post meta
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function fcwordpress_save_meta_box_data( $post_id ) { /* * We need to verify this came from our screen and with proper authorization, * because the save_post action can be triggered at other times. */ // Check if our nonce is set. if ( ! isset( $_POST['fcwordpress_meta_box_nonce'] ) ) { return; } // Verify that the nonce is valid. if ( ! wp_verify_nonce( $_POST['fcwordpress_meta_box_nonce'], 'fcwordpress_meta_box' ) ) { return; } // 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; } /* OK, it's safe for us to save the data now. */ $fcwordpress_this_is_podcast = !empty($_POST['fcwordpress_this_is_podcast']) ? $_POST['fcwordpress_this_is_podcast'] : '0'; // Update the meta field in the database. update_post_meta( $post_id, 'fcwordpress_this_is_podcast', $fcwordpress_this_is_podcast ); } add_action( 'save_post', 'fcwordpress_save_meta_box_data' ); |
4. Tất cả code để anh chị copy về và sửa theo yêu cầu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
function fcwordpress_add_meta_box() { //show which post type $screens = array( 'post' ); foreach ( $screens as $screen ) { add_meta_box( 'fcwordpress_sectionid', __( 'Meta Field', 'textdomain' ), //Function callback 'fcwordpress_meta_box_callback', $screen, //side: on the right //advanced: below editor //normal: below editor 'side', 'high' ); } } add_action( 'add_meta_boxes', 'fcwordpress_add_meta_box' ) function fcwordpress_meta_box_callback( $post ) { // Add an nonce field so we can check for it later. wp_nonce_field( 'fcwordpress_meta_box', 'tonyteegarden_meta_box_nonce' ); /* * Use get_post_meta() to retrieve an existing value * from the database and use the value for the form. */ $fcwordpress_this_is_podcast = get_post_meta( $post->ID, 'fcwordpress_this_is_podcast', true ); echo '<label for="fcwordpress_new_field">'; _e( 'Check for set this post is podcast', 'textdomain' ); echo '</label>'; echo '<input '.checked($fcwordpress_this_is_podcast,"1").' type="checkbox" value="1" id="fcwordpress_this_is_podcast" name="fcwordpress_this_is_podcast" >'; } function fcwordpress_save_meta_box_data( $post_id ) { /* * We need to verify this came from our screen and with proper authorization, * because the save_post action can be triggered at other times. */ // Check if our nonce is set. if ( ! isset( $_POST['fcwordpress_meta_box_nonce'] ) ) { return; } // Verify that the nonce is valid. if ( ! wp_verify_nonce( $_POST['fcwordpress_meta_box_nonce'], 'fcwordpress_meta_box' ) ) { return; } // 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; } /* OK, it's safe for us to save the data now. */ $fcwordpress_this_is_podcast = !empty($_POST['fcwordpress_this_is_podcast']) ? $_POST['fcwordpress_this_is_podcast'] : '0'; // Update the meta field in the database. update_post_meta( $post_id, 'fcwordpress_this_is_podcast', $fcwordpress_this_is_podcast ); } add_action( 'save_post', 'fcwordpress_save_meta_box_data' );</pre--> |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress