Advertisement
downloadtaky

quick edit custom meta

May 6th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | None | 0 0
  1. <?php
  2. /**
  3.      *
  4.      * Add 'Disponibilità' in Quick Edit
  5.      */
  6.  
  7.     function etdispo_quickedit_custom_posts_columns( $posts_columns ) {
  8.         $posts_columns['et2018-quantita_birra'] = __( 'Disponibilità', 'etdispo' );
  9.         return $posts_columns;
  10.     }
  11.     add_filter( 'manage_post_posts_columns', 'etdispo_quickedit_custom_posts_columns' );
  12.  
  13.     function etdispo_quickedit_custom_column_display( $column_name, $post_id ) {
  14.         if ( 'et2018-quantita_birra' == $column_name ) {
  15.             $etdispo_regi = get_post_meta( $post_id, 'et2018-quantita_birra', true );
  16.  
  17.             if ( $etdispo_regi ) {
  18.                 echo esc_html( $etdispo_regi );
  19.             } else {
  20.                 esc_html_e( 'N/A', 'etdispo' );
  21.             }
  22.         }
  23.     }
  24.     add_action( 'manage_post_posts_custom_column', 'etdispo_quickedit_custom_column_display', 10, 2 );
  25.  
  26.     function etdispo_quickedit_fields( $column_name, $post_type, $post_id ) {
  27.         if ( 'et2018-quantita_birra' != $column_name )
  28.             return;
  29.         $etdispo_regi = get_post_meta( $post_id, 'et2018-quantita_birra', true );
  30.         ?>
  31.         <fieldset class="inline-edit-col-right">
  32.             <div class="inline-edit-col">
  33.                 <label>
  34.                     <span class="title"><?php esc_html_e( 'Disponibilità', 'etdispo' ); ?></span>
  35.                     <span class="input-text-wrap">
  36.                     <input type="text" name="et2018-quantita_birra" class="etdispoedit" value="">
  37.                 </span>
  38.                 </label>
  39.             </div>
  40.         </fieldset>
  41.         <?php
  42.     }
  43.     add_action( 'quick_edit_custom_box', 'etdispo_quickedit_fields', 10, 3 );
  44.     function etdispo_quickedit_save_post( $post_id, $post ) {
  45.         // if called by autosave, then bail here
  46.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  47.             return;
  48.  
  49.         // if this "post" post type?
  50.         if ( $post->post_type != 'post' )
  51.             return;
  52.  
  53.         // does this user have permissions?
  54.         if ( ! current_user_can( 'edit_post', $post_id ) )
  55.             return;
  56.  
  57.         // update!
  58.         if ( isset( $_POST['et2018-quantita_birra'] ) ) {
  59.             update_post_meta( $post_id, 'et2018-quantita_birra', $_POST['et2018-quantita_birra'] );
  60.         }
  61.     }
  62.     add_action( 'save_post', 'etdispo_quickedit_save_post', 10, 2 );
  63.  
  64.     function etdispo_quickedit_javascript() {
  65.         $current_screen = get_current_screen();
  66.         if ( $current_screen->id != 'edit-post' || $current_screen->post_type != 'post' )
  67.             return;
  68.  
  69.         // Ensure jQuery library loads
  70.         wp_enqueue_script( 'jquery' );
  71.         ?>
  72.         <script type="text/javascript">
  73.             jQuery( function( $ ) {
  74.                 $( '#the-list' ).on( 'click', 'a.editinline', function( e ) {
  75.                     e.preventDefault();
  76.                     var editDispo = $(this).data( 'edit-dispo' );
  77.                     inlineEditPost.revert();
  78.                     $( '.etdispoedit' ).val( editDispo ? editDispo : '' );
  79.                 });
  80.             });
  81.         </script>
  82.         <?php
  83.     }
  84.     add_action( 'admin_print_footer_scripts-edit.php', 'etdispo_quickedit_javascript' );
  85.     /* Qui */
  86.     function etdispo_quickedit_set_data( $actions, $post ) {
  87.         $found_value = get_post_meta( $post->ID, 'et2018-quantita_birra', true );
  88.  
  89.         if ( $found_value ) {
  90.             if ( isset( $actions['inline hide-if-no-js'] ) ) {
  91.                 $new_attribute = sprintf( 'data-edit-dispo="%s"', esc_attr( $found_value ) );
  92.                 $actions['inline hide-if-no-js'] = str_replace( 'class=', "$new_attribute class=", $actions['inline hide-if-no-js'] );
  93.             }
  94.         }
  95.  
  96.         return $actions;
  97.     }
  98.     add_filter('post_row_actions', 'etdispo_quickedit_set_data', 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement