Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Forked from daronspence/quick-edit.php
Last active April 15, 2020 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/1397000964c21998c83a0bc133160f71 to your computer and use it in GitHub Desktop.
Save nfsarmento/1397000964c21998c83a0bc133160f71 to your computer and use it in GitHub Desktop.
Quick Edit Plugin
<?php
/**
* Plugin Name: NS inline edit
* Plugin URL: https://www.nuno-sarmento.com
* Description: Add excerpt to WordPress Posts Quick Edit
* Requires at least: 5.0
* Tested up to: 5.4
* Version: 0.1
* Plugin Author: Nuno Sarmento
* Author URI: https://www.nuno-sarmento.com
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
add_action( 'quick_edit_custom_box', function( $column_name, $post_type ){
if ( 'portfolio' === $post_type && 'post_excerpt' === $column_name ){ ?>
<fieldset class="inline-edit-col-left inline-edit-custom" style="clear: left;">
<div class="inline-edit-col inline-edit-<?php echo $column_name ?>">
<label><?php _e( 'Excerpt', 'quick-edit-pro' ); ?>
<textarea name="<?php echo $column_name; ?>"></textarea>
</label>
</div>
</fieldset>
<?php }
}, 10, 2 );
add_filter( 'manage_posts_columns', function( $columns ) {
return array_merge( $columns,
array( 'post_excerpt' => __( 'Excerpt' ) ) );
}, 10, 2 );
add_action( 'manage_posts_custom_column' , function( $column, $post_id ){
if ( 'post_excerpt' === $column ){
$post = get_post( $post_id );
echo $post->post_excerpt;
}
}, 10, 2 );
add_action( 'save_post', 'ns_update_post' );
function ns_update_post( $post_id ) {
$slug = 'portfolio';
if ( $slug !== $_POST['post_type'] ) {
return;
}
// verify if this is an auto save routine. If it is our form has not been submitted,
// so we dont want to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
// checkboxes are submitted if checked, absent if not
if ( isset( $_REQUEST['post_excerpt'] ) ) {
remove_action( 'save_post', 'ns_update_post' );
$args = array(
'ID' => $post_id,
'post_excerpt' => esc_html( $_REQUEST['post_excerpt'] )
);
wp_update_post( $args );
// Now hook the action
add_action( 'save_post', 'ns_update_post' );
}
return;
}
add_action( 'admin_print_footer_scripts', function(){ ?>
<script type="text/javascript">
(function($) {
// we create a copy of the WP inline edit post function
var $wp_inline_edit = inlineEditPost.edit;
// and then we overwrite the function with our own code
inlineEditPost.edit = function( id ) {
// "call" the original WP edit function
// we don't want to leave WordPress hanging
$wp_inline_edit.apply( this, arguments );
// now we take care of our business
// get the post ID
var $post_id = 0;
if ( typeof( id ) == 'object' ) {
$post_id = parseInt( this.getId( id ) );
}
if ( $post_id > 0 ) {
// define the edit row
var $edit_row = $( '#edit-' + $post_id );
var $post_row = $( '#post-' + $post_id );
// get the data
var $post_excerpt = $( '.column-post_excerpt', $post_row ).text();
// populate the data
$( ':input[name="post_excerpt"]', $edit_row ).val( $post_excerpt );
}
};
})(jQuery);
</script>
<?php
}, 1000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment