Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- KODE MEMBUAT METABOX di function.php:
- function custom_post_metaboxes() {
- add_meta_box('ob-post', 'Custom Post', 'custom_post_func', 'post', 'normal', 'high');
- }
- add_action('admin_menu', 'custom_post_metaboxes');
- function custom_post_func(){
- global $post;
- echo '<input type="hidden" name="custom_post_custom_nonce" id="ob_post_custom_nonce" value="' .
- wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
- $custom_post_value = get_post_meta($post->ID, 'custom_post_value', true);
- ?>
- <table>
- <tbody>
- <tr>
- <td>Value :</td>
- <td><input type="text" name="custom_post_value" value="<?php echo $custom_post_value; ?>"/></td>
- </tr>
- </tbody>
- </table>
- <?php
- }
- function custom_post_func_save($post_id, $post){
- if ( !wp_verify_nonce( $_POST['custom_post_custom_nonce'], plugin_basename(__FILE__) )) {
- return $post->ID;
- }
- if ( !current_user_can( 'edit_post', $post->ID ))
- return $post->ID;
- $custom_post_value['custom_post_value'] = $_POST['custom_post_value'];
- foreach ($custom_post_value as $key => $value) {
- if( $post->post_type == 'revision' ) return;
- if(get_post_meta($post->ID, $key, FALSE)) {
- update_post_meta($post->ID, $key, $value);
- } else {
- add_post_meta($post->ID, $key, $value);
- }
- if(!$value) delete_post_meta($post->ID, $key);
- }
- }
- add_action('save_post', 'custom_post_func_save', 1, 2);
- ==========================================
- KODE MENAMPILKAN METABOX di single.php
- $custom_post_box = get_post_meta(get_the_ID(), "custom_post_value", true);
- echo $custom_post_box;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement