Codex tools: Log in
Contents |
quick_edit_custom_box is an action that lets a plugin print inputs for custom columns when quick editing. This action is called one time for each custom column. Custom columns are added with the manage_edit-${post_type}_columns filter. To save the data from the custom inputs, hook the save_post action.
A registered action function is passed the following parameters.
Note that the action function is passed neither the post ID nor any existing value for the column.
add_action( 'quick_edit_custom_box', 'display_custom_quickedit_book', 10, 2 );
function display_custom_quickedit_book( $column_name, $post_type ) {
static $printNonce = TRUE;
if ( $printNonce ) {
$printNonce = FALSE;
wp_nonce_field( plugin_basename( __FILE__ ), 'book_edit_nonce' );
}
?>
<fieldset class="inline-edit-col-right inline-edit-book">
<div class="inline-edit-col inline-edit-<?php echo $column_name ?>">
<?php
switch ( $column_name ) {
case 'book_author':
?><legend>Author</legend><input name="book_author" /><?php
break;
case 'inprint':
?><legend>In Print</legend><input name="inprint" type="checkbox" /><?php
break;
}
?>
</div>
</fieldset>
<?php
}
Data entered in custom inputs can be saved by hooking the save_post action.
add_action( 'save_post', 'save_book_meta' );
function save_book_meta( $post_id ) {
/* in production code, $slug should be set only once in the plugin,
preferably as a class property, rather than in each function that needs it.
*/
$slug = 'book';
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['book_author'] ) ) {
update_post_meta( $post_id, 'author', $_REQUEST['book_author'] );
}
# checkboxes are submitted if checked, absent if not
if ( isset( $_REQUEST['inprint'] ) ) {
update_post_meta($post_id, 'inprint', TRUE);
} else {
update_post_meta($post_id, 'inprint', FALSE);
}
}
Populating inputs with existing values takes a bit of trickery. One can't simply access the $post global because when this action is run, $post refers only to the last post (the quick edit inputs are created only once, and cloned as needed when quick editing a column). There are two parts: storing the data on the page and hooking inlineEditPost.edit to set the input values. If the quick-edit columns are also displayed as custom columns, the data is already in the table. Otherwise, it can be added as hidden elements to an existing custom column.
Hooking inlineEditPost.edit must be done in a JS script, loaded after wp-admin/js/inline-edit-post.js. The original method is saved and replaced with a new one which calls the original. This particular technique is a simple example of aspect-oriented programming.
add_action('admin_footer-edit.php', 'admin_edit_book_foot', 11);
/* load scripts in the footer */
function admin_edit_foot() {
$slug = 'book';
# load only when editing a book
if ( (isset($_GET['page']) && $_GET['page'] == $slug)
|| (isset($_GET['post_type']) && $_GET['post_type'] == $slug))
{
echo '<script type="text/javascript" src="', plugins_url('scripts/admin_edit.js', __FILE__), '"></script>';
}
}
/* example of how an existing value can be stored in the table */
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function custom_book_column( $column, $post_id ) {
switch ( $column ) {
case 'inprint':
if ( !!get_post_meta( $post_id , 'inprint' , true ) ) {
$checked = 'checked';
} else {
$checked = '';
}
echo "<input type='checkbox' readonly $checked/>";
break;
case 'book_author':
# ...
}
}
function quickEditBook() {
var $ = jQuery;
var _edit = inlineEditPost.edit;
inlineEditPost.edit = function(id) {
var args = [].slice.call(arguments);
_edit.apply(this, args);
if (typeof(id) == 'object') {
id = this.getId(id);
}
if (this.type == 'post') {
var
// editRow is the quick-edit row, containing the inputs that need to be updated
editRow = $('#edit-' + id),
// postRow is the row shown when a book isn't being edited, which also holds the existing values.
postRow = $('#post-'+id),
// get the existing values
// the class ".column-book_author" is set in display_custom_quickedit_book
author = $('.column-book_author', postRow).text(),
inprint = !! $('.column-inprint>*', postRow).attr('checked');
// set the values in the quick-editor
$(':input[name="book_author"]', editRow).val(author);
$(':input[name="inprint"]', editRow).attr('checked', inprint);
}
};
}
// Another way of ensuring inlineEditPost.edit isn't patched until it's defined
if (inlineEditPost) {
quickEditBook();
} else {
jQuery(quickEditBook);
}
wp-admin/includes/class-wp-terms-list-table.php in 3.1
wp-admin/includes/template.php
wp-admin/includes/class-wp-posts-list-table.php, and by WP_Terms_List_Table->inline_edit in wp-admin/includes/class-wp-terms-list-table.php.