1

I have many posts in which I want to edit the excerpt. But instead of having to got to the edit page, I would want to do it with the "Quick Edit" feature.

How can I add the excerpt in the Quick Edit ?

PS: I know a plugin exists for that, but I have a custom plugin for my website, so it would be cool if I can add this in it.

EDIT: I don't want to use the plugin because I am not really fan of using plugins when there's a possibility to easily add code somewhere to do what I want to do. Also the plugin "Custom Bulk/Quick Edit" I was talking have many features I won't use.

  • 1
    Please describe your development problem more precise. What plugin is there, how does it solve what you want to do, and what stops you from using that plugin / solving it the same way? – janh Jan 18 '18 at 17:36
  • Just for your information. Themes are for visual display, plugins are for functional performance. There is no (NO) difference between running a plugin or functions.php. – Max Yudin Jan 19 '18 at 3:35
0

I too am looking for this option / feature. I came across this guys explanation which I believe answers most of the question. I am in the process of implementing this so I cannot say how accurate it is being a wordpress noob (not php!), I do realise this is for a custom box but you could populate the column field using the the_excerpt() fn or from the $post object - etc.

So it seems, to have the option we want available in the quick edit section, we need to add it as a "custom" column in the post / page list view first.

/*
 * New columns
 */
add_filter('manage_post_posts_columns', 'misha_featured_columns');
// the above hook will add columns only for default 'post' post type, for CPT:
// manage_{POST TYPE NAME}_posts_columns
function misha_price_and_featured_columns( $column_array ) {
    $column_array['featured'] = 'Featured product';
    return $column_array;
}

/*
 * Populate our new columns with data
 */
add_action('manage_posts_custom_column', 'misha_populate_column', 10, 2);
function misha_populate_column( $column_name, $id ) {
    switch( $column_name ) :

        case 'featured': {
            if( get_post_meta($id,'product_featured',true) == 'on') 
                echo 'Yes';
            break;
        }
    endswitch;

}

Then we can add it to the quick edit section.

/*
 * quick_edit_custom_box allows to add HTML in Quick Edit
 * Please note: it files for EACH column, so it is similar to manage_posts_custom_column
 */
add_action('quick_edit_custom_box',  'misha_quick_edit_fields', 10, 2);

function misha_quick_edit_fields( $column_name, $post_type ) {

    switch( $column_name ) :

        case 'featured': {

            echo '<fieldset><label class="alignleft">
                    <input type="checkbox" name="featured">
                    <span class="checkbox-title">Featured product</span>
                </label></fieldset>';

            break;

        }

    endswitch;

}

We also need some code to hook into save post to allow for updating the meta data.

/*
 * Quick Edit Save
 */
add_action( 'save_post', 'misha_quick_edit_save' );

function misha_quick_edit_save( $post_id ){

    if ( !current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    if ( !wp_verify_nonce( $_POST['misha_nonce'], 'misha_q_edit_nonce' ) ) {
        return;
    }

    if ( isset( $_POST['featured'] ) ) {
        update_post_meta( $post_id, 'product_featured', 'on' );
    } else {
        update_post_meta( $post_id, 'product_featured', '' );
    }

}

Lastly some JavaScript, he uses jQuery, is required to populate the fields data once the page is loaded. This he places in a separate file and loads with wp_enqueue:

add_action( 'admin_enqueue_scripts', 'misha_enqueue_quick_edit_population' );
function misha_enqueue_quick_edit_population( $pagehook ) {

    // do nothing if we are not on the target pages
    if ( 'edit.php' != $pagehook ) {
        return;
    }

    wp_enqueue_script( 'populatequickedit', get_stylesheet_directory_uri() . '/populate.js', array( 'jquery' ) );

}

JavaScript file:

jQuery(function($){

    var wp_inline_edit_function = inlineEditPost.edit;

    inlineEditPost.edit = function( post_id ) {

        wp_inline_edit_function.apply( this, arguments );

        var id = 0;
        if ( typeof( post_id ) == 'object' ) { // if it is object, get the ID number
            id = parseInt( this.getId( post_id ) );
        }

        if ( id > 0 ) {

            // add rows to variables
            var specific_post_edit_row = $( '#edit-' + id ),
                specific_post_row = $( '#post-' + id ),
                featured_product = false; // let's say by default checkbox is unchecked

            if( $( '.column-featured', specific_post_row ).text() == 'Yes' ) featured_product = true;

            $( ':input[name="featured"]', specific_post_edit_row ).prop('checked', featured_product );
        }
    }
});

I've copy, pasted & edited his raw code for reference, the tutorial at the site has comments within in the code explaining its use etc. I don't know this person just came across this post and theirs whilst googling.

Finally one thing I believe may need pointing out is that the save_post hook could interfere with any hooks you already have for save_post and I am unsure if quick edit save_post would recognise the excerpt field as standard or would need the hook above?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.