Skip to content

Instantly share code, notes, and snippets.

@cyberwani
Created March 16, 2018 10:07
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 cyberwani/b04f43a36de67070f6999c94ad36a4a0 to your computer and use it in GitHub Desktop.
Save cyberwani/b04f43a36de67070f6999c94ad36a4a0 to your computer and use it in GitHub Desktop.
WordPress Post Quick Edit. Reference https://generatewp.com/managing-content-easily-quick-edit/
<?php
// Add a Custom Column
function pqe_quickedit_custom_posts_columns( $posts_columns ) {
$posts_columns['pqe_edit_time'] = __( 'Edit Time', 'pqe' );
return $posts_columns;
}
add_filter( 'manage_post_posts_columns', 'pqe_quickedit_custom_posts_columns' );
// Display content for new Custom Column
function pqe_quickedit_custom_column_display( $column_name, $post_id ) {
if ( 'pqe_edit_time' == $column_name ) {
$time_recorded = get_post_meta( $post_id, 'pqe_edit_time', true );
if ( $time_recorded ) {
echo esc_html( $time_recorded );
} else {
esc_html_e( 'N/A', 'generatewp' );
}
}
}
add_action( 'manage_post_posts_custom_column', 'pqe_quickedit_custom_column_display', 10, 2 );
// Add a Quick Edit controller field
function pqe_quickedit_fields( $column_name, $post_type ) {
if ( 'pqe_edit_time' != $column_name )
return;
$time_recorded = get_post_meta( $post_id, 'pqe_edit_time', true );
?>
<fieldset class="inline-edit-col-right">
<div class="inline-edit-col">
<label>
<span class="title"><?php esc_html_e( 'Time', 'generatewp' ); ?></span>
<span class="input-text-wrap">
<input type="text" name="pqe_edit_time" class="pqe_edittime" value="">
</span>
</label>
</div>
</fieldset>
<?php
}
add_action( 'quick_edit_custom_box', 'pqe_quickedit_fields', 10, 2 );
// Save the Quick Edit field data
function pqe_quickedit_save_post( $post_id, $post ) {
// if called by autosave, then bail here
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// if this "post" post type?
if ( $post->post_type != 'post' )
return;
// does this user have permissions?
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// update!
if ( isset( $_POST['pqe_edit_time'] ) ) {
update_post_meta( $post_id, 'pqe_edit_time', $_POST['pqe_edit_time'] );
}
}
add_action( 'save_post', 'pqe_quickedit_save_post', 10, 2 );
// Populate field with live data with JavaScript
function pqe_quickedit_javascript() {
$current_screen = get_current_screen();
if ( $current_screen->id != 'edit-post' || $current_screen->post_type != 'post' )
return;
// Ensure jQuery library loads
wp_enqueue_script( 'jquery' );
?>
<script type="text/javascript">
jQuery( function( $ ) {
$( '#the-list' ).on( 'click', 'a.editinline', function( e ) {
e.preventDefault();
var editTime = $(this).data( 'edit-time' );
inlineEditPost.revert();
$( '.pqe_edittime' ).val( editTime ? editTime : '' );
});
});
</script>
<?php
}
add_action( 'admin_print_footer_scripts-edit.php', 'pqe_quickedit_javascript' );
function pqe_quickedit_set_data( $actions, $post ) {
$found_value = get_post_meta( $post->ID, 'pqe_edit_time', true );
if ( $found_value ) {
if ( isset( $actions['inline hide-if-no-js'] ) ) {
$new_attribute = sprintf( 'data-edit-time="%s"', esc_attr( $found_value ) );
$actions['inline hide-if-no-js'] = str_replace( 'class=', "$new_attribute class=", $actions['inline hide-if-no-js'] );
}
}
return $actions;
}
add_filter('post_row_actions', 'pqe_quickedit_set_data', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment