PHP Developer

Wednesday, 8 April 2015

Add Meta Box Wordpress Editor for Post or Custom Post in Wordpress

Add Meta Box Wordpress Editor for Post or Custom Post in Wordpress

<?php
/*-------- Add Meta Box Wordpress Editor for Post Code START ----------*/

function myplugin_add_custom_box() {
    $screens = array( 'post' );  // Post Type
    foreach ( $screens as $screen ) {
        add_meta_box(
            'myplugin_sectionid',
            __( 'Product Detail', 'myplugin_textdomain' ),
            'myplugin_inner_custom_box',
            $screen
        );
    }
}


add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
function myplugin_inner_custom_box( $post ) {
  wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
  $accessories = get_post_meta( $post->ID, 'accessories', true );  
  $specification = get_post_meta( $post->ID, 'specification', true ); 
  echo '<table>';
  echo '<tr>';
  echo '<td><label for="myplugin_new_field"><strong>';
       _e( "Accessories", 'myplugin_textdomain' );
  echo '</strong></label> </td>';
  echo '<td>';
  wp_editor( $accessories, 'accessories' );
  echo '</td>';
  echo '</tr><tr>';
  echo '<td><label for="myplugin_new_field"><strong>';
       _e( "Specification", 'myplugin_textdomain' );
  echo '</strong></label> </td>';
  echo '<td>';
  wp_editor( $specification, 'specification' );
  echo '</td>';
  echo '</tr>';
  echo '</table>';
}
function myplugin_save_postdata( $post_id ) {
  if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) )
    return $post_id;
  $nonce = $_POST['myplugin_inner_custom_box_nonce'];
  if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) )
      return $post_id;
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return $post_id;
  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;
  }
  if (!empty($_POST['accessories'])) {
        update_post_meta( $post_id, 'accessories', $_POST['accessories'] );
        }
  if (!empty($_POST['specification'])) {
        update_post_meta( $post_id, 'specification', $_POST['specification'] );
        }
}
add_action( 'save_post', 'myplugin_save_postdata' );

/*-------- Add Meta Box Wordpress Editor for Post Code END ----------*/
?>

No comments:

Post a Comment