PHP Developer

Thursday, 25 August 2016

How wp_editor and date picker use in Posts Meta Fields(Custom Fields)

How wp_editor and date picker use in Posts Meta Fields(Custom Fields) :


<?php
/*----------- Create Metabox in Custom Post vacancies Code Start ---------------*/

function myplugin_add_custom_box() {
    $screens = array( 'vacancies' );  // Post Type
    foreach ( $screens as $screen ) {
        add_meta_box(
            'myplugin_sectionid',
            __( 'Other 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' );
 
  $job_title = get_post_meta( $post->ID, 'job_title', true );
  $opening_date = get_post_meta( $post->ID, 'opening_date', true );
  $last_date = get_post_meta( $post->ID, 'last_date', true );
 
  $pay_scale_desc = get_post_meta( $post->ID, 'pay_scale_desc', false );
 
  ?>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
  <script>
  $( function() {
    $( ".datepicker" ).datepicker();
  } );
  </script>
  <?php
  echo '<table>';
  echo '<tr>';
  echo '<td><label for="myplugin_new_field">';
       _e( "Job Title", 'myplugin_textdomain' );
  echo '</label> </td>';
  echo '<td><input type="text" id="job_title" name="job_title" value="' . esc_attr( $job_title ) . '" size="25" /></td>';
  echo '</tr><tr>';
  echo '<td><label for="myplugin_new_field">';
       _e( "Opening Date", 'myplugin_textdomain' );
  echo '</label> </td>';
  echo '<td><input type="text" class="datepicker" id="opening_date" name="opening_date" value="' . esc_attr( $opening_date ) . '" size="25" /></td>';
  echo '</tr><tr>';
  echo '<td><label for="myplugin_new_field">';
       _e( "Closing Date", 'myplugin_textdomain' );
  echo '</label> </td>';
  echo '<td><input type="text" class="datepicker" id="last_date" name="last_date" value="' . esc_attr( $last_date ) . '" size="25" /></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td><label for="myplugin_new_field">';
       _e( "Pay Scale Description", 'myplugin_textdomain' );
  echo '</label> </td>';
  echo '<td>';
  wp_editor( $pay_scale_desc[0], 'pay_scale_desc' );
  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;
  }
 
  $job_title = sanitize_text_field( $_POST['job_title'] );
  $opening_date = sanitize_text_field( $_POST['opening_date'] );
  $last_date = sanitize_text_field( $_POST['last_date'] );
 
  update_post_meta( $post_id, 'job_title', $job_title );
  update_post_meta( $post_id, 'opening_date', $opening_date );
  update_post_meta( $post_id, 'last_date', $last_date );
 
  if ( isset ( $_POST['pay_scale_desc'] ) ) {
    update_post_meta( $post_id, 'pay_scale_desc', $_POST['pay_scale_desc'] );
  }
}
add_action( 'save_post', 'myplugin_save_postdata' );

/*----------- Create Metabox in Custom Post vacancies Code END ---------------*/
?>

 =========================================================

And How wp_editor value get in Frontend in same Format :


<?php 
$pay_scale_desc = get_post_meta( $post->ID, 'pay_scale_desc', false );
echo "<pre>";
print_r($pay_scale_desc[0]);
echo "</pre>";
?>

No comments:

Post a Comment