How to add a custom field in a post table quick edit box in wordpress

Below is working code for add a custom field into quick edit box

// Below code will add a custom field.
add_action( 'quick_edit_custom_box', 'rj_display_display_order_box_in_quick_edit', 10, 2 );

function rj_display_display_order_box_in_quick_edit( $column_name, $post_type ) {
    if(!in_array( $post_type,array('people','watch-us') ))
    {
        return;
    }
    static $printNonce = TRUE;
    if ( $printNonce ) {
        $printNonce = FALSE;
        wp_nonce_field( 'display_order_edit_field', 'display_order_edit_nonce' );
    }

    ?>
    <fieldset class="inline-edit-col-right inline-edit-book">
      <div class="inline-edit-col column-<?php echo $column_name; ?>">
        <label class="inline-edit-group">
        <?php
         switch ( $column_name ) {
         case 'display_order':
             ?><span class="title">Display Order</span><span class="input-text-wrap"><input name="display_order" /></span><?php
             break;
         }
        ?>
        </label>
      </div>
    </fieldset>
    <?php
}

// below filter will add a custom field column in table
add_filter( 'manage_posts_columns', 'rj_manage_display_order_columns', 10, 2 );
function rj_manage_display_order_columns( $columns, $post_type ) {
   if ( !in_array( $post_type,array('people','watch-us') ) )
   {
    return $columns;
   }
   $new_columns = array();
         foreach( $columns as $key => $value ) {
            $new_columns[ $key ] = $value;
            if ( $key == 'author' )
               $new_columns[ 'display_order' ] = 'Display Order';
         }
         return $new_columns;
}
// below action will display value of custom field
add_action( 'manage_posts_custom_column', 'rj_display_order_column_value', 10, 2 );
function rj_display_order_column_value( $column_name, $post_id ) {
   switch( $column_name ) {
      case 'display_order':
         echo '<div id="display_order-' . $post_id . '">' . get_post_meta( $post_id, 'display_order', true ) . '</div>';
         break;
   }
}

// below action for add a custom js code file into admin sde
add_action( 'admin_print_scripts-edit.php', 'rj_enqueue_edit_display_order_script' );
function rj_enqueue_edit_display_order_script() {
   wp_enqueue_script( 'rj-admin-edit', get_template_directory_uri() . '/assets/js/quick_edit.js', array( 'jquery', 'inline-edit-post' ), '', true );
  
}

// my quick_edit.js file

(function($) {
    var $wp_inline_edit = inlineEditPost.edit;
   inlineEditPost.edit = function( id ) {
   $wp_inline_edit.apply( this, arguments );
      var $post_id = 0;
      if ( typeof( id ) == 'object' )
         $post_id = parseInt( this.getId( id ) );

      if ( $post_id > 0 ) {

         var $edit_row = $( '#edit-' + $post_id );
     var $display_order = $( '#display_order-' + $post_id ).text();
     $edit_row.find( 'input[name="display_order"]' ).val( $display_order );

      }

   };

})(jQuery);
// my quick_edit.js file end
// this will save custom field value in quick edit
add_action( 'save_post','rj_save_quick_edit_display_order' );
function rj_save_quick_edit_display_order( $post_id ) {

global $wpdb;

   if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return $post_id;
   if ( !in_array(  $_POST['post_type'],array('people','watch-us') ) ) {
        return;
    }
   if ( isset( $post->post_type ) && $post->post_type == 'revision' )
        return $post_id;
     if ( array_key_exists( 'display_order', $_POST ) )
        update_post_meta( $post_id, 'display_order', $_POST['display_order'] );
}
// below script for save quick_edit bulk action values
add_action( 'wp_ajax_manage_wp_posts_using_bulk_quick_save_bulk_edit', 'manage_wp_posts_using_bulk_quick_save_bulk_edit' );
function manage_wp_posts_using_bulk_quick_save_bulk_edit() {

    $post_ids = ( isset( $_POST[ 'post_ids' ] ) && !empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : NULL;
        
    if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
    
        $custom_fields = array( 'display_order' );
        foreach( $custom_fields as $field ) {
            if ( isset( $_POST[ $field ] ) && !empty( $_POST[ $field ] ) ) {
                foreach( $post_ids as $post_id ) {
                    update_post_meta( $post_id, $field, $_POST[ $field ] );
                }
            }
        }
    }
}

Comments

Popular posts from this blog

How to add image option in nav menu in wordpress

How to add a custom email functionality in woocommerce,wordpress for example new email template for shipping order

How to change user id on checkout page for assign order to different user in woocommerce wordpress