Skip to content

Instantly share code, notes, and snippets.

@hannit
Created November 16, 2016 04:15
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 hannit/71919aa6c736882cd1b80e9837ca9652 to your computer and use it in GitHub Desktop.
Save hannit/71919aa6c736882cd1b80e9837ca9652 to your computer and use it in GitHub Desktop.
Add meta fields to quick edit
add_action( 'quick_edit_custom_box', 'display_custom_quickedit_featured', 10, 2 );
function display_custom_quickedit_featured( $column_name, $post_type ) {
static $printNonce = TRUE;
if ( $printNonce ) {
$printNonce = FALSE;
wp_nonce_field( plugin_basename( __FILE__ ), 'post_edit_nonce' );
}
?>
<fieldset class="inline-edit-col-right inline-edit-featured">
<div class="inline-edit-col column-<?php echo $column_name; ?>">
<label class="inline-edit-group">
<?php
switch ( $column_name ) {
case 'featured':
?><span class="title">Featured</span><input name="featured_position" /><?php
break;
}
?>
</label>
</div>
</fieldset>
<?php
}
add_action( 'save_post', 'save_post_featured' );
function save_post_featured( $post_id ) {
$slug = 'post';
if ( $slug !== $_POST['post_type'] ) {
return;
}
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
$_POST += array("{$slug}_edit_nonce" => '');
if ( !wp_verify_nonce( $_POST["{$slug}_edit_nonce"], plugin_basename( __FILE__ ) ) ) {
return;
}
if ( isset( $_REQUEST['featured_position'] ) ) {
update_post_meta( $post_id, 'featured_position', $_REQUEST['featured_position'] );
} else {
delete_post_meta($post_id, 'featured_position');
}
# checkboxes are submitted if checked, absent if not
}
add_action('admin_footer', 'grafa_quick_edit_javascript');
/* load script in the footer */
if ( ! function_exists('wp_my_admin_enqueue_scripts') ):
function wp_my_admin_enqueue_scripts( $hook ) {
if ( 'edit.php' === $hook ) {
wp_enqueue_script( 'my_custom_script', get_stylesheet_directory_uri().'/assets/js/admin_edit.js', false, null, true );
}
}
endif;
add_action( 'get_footer', 'wp_my_admin_enqueue_scripts' );
(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 $featured = $( '.column-featured', $post_row ).text();
// populate the data
$( ':input[name="featured_position"]', $edit_row ).val( $featured );
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment