quick_edit_custom_box
云策文档标注
概述
quick_edit_custom_box 是一个 WordPress 动作钩子,用于在快速编辑模式下为自定义列输出输入字段。它允许插件为每个自定义列添加表单元素,并需配合其他钩子实现数据保存。
关键要点
- quick_edit_custom_box 是一个动作钩子,在快速编辑模式下为每个自定义列触发一次。
- 自定义列通过 manage_{$post_type}_posts_columns 过滤器添加,数据保存需挂钩 save_post 动作。
- 参数包括 $column_name(列名)、$post_type(文章类型或屏幕名称)和 $taxonomy(分类法名称,可选)。
- 设置现有值需注意 $post 全局变量限制,通常通过 JavaScript 和隐藏元素实现。
代码示例
// 创建输入字段示例
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">
<div class="inline-edit-col">
<label>
<span class="title">Author</span>
<span class="input-text-wrap">
<input type="text" name="book_author" class="ptitle" value="" />
</span>
</label>
<label>
<span class="title">In Print</span>
<span class="input-text-wrap">
<input type="checkbox" name="inprint" />
</span>
</label>
</div>
</fieldset>
<?php
}注意事项
在快速编辑中填充现有值时,不能直接使用 $post 全局变量,因为 $post 仅引用最后一篇文章。建议通过 JavaScript 钩子 inlineEditPost.edit 和隐藏元素来设置输入值。
原文内容
Fires once for each column in Quick Edit mode.
Parameters
$column_namestring-
Name of the column to edit.
$post_typestring-
The post type slug, or current screen name if this is a taxonomy list table.
$taxonomystring-
The taxonomy name, if any.
Source
do_action( 'quick_edit_custom_box', $column_name, $screen->post_type, '' );
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
Skip to note 4 content
Steven Lin
Example migrated from Codex:
Setting Existing Values
Populating inputs with existing values takes a bit of trickery. One can’t simply access the
$postglobal 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 hookinginlineEditPost.editto 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.editmust be done in a JS script, loaded after js/_enqueues/admin/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.PHP:
/* load script in the footer */ if ( ! function_exists('wp_my_admin_enqueue_scripts') ): function wp_my_admin_enqueue_scripts( $hook ) { if ( 'edit.php' === $hook && isset( $_GET['post_type'] ) && 'book' === $_GET['post_type'] ) { wp_enqueue_script( 'my_custom_script', plugins_url('scripts/admin_edit.js', __FILE__), false, null, true ); } } endif; add_action( 'admin_enqueue_scripts', 'wp_my_admin_enqueue_scripts' ); /* 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': // the !! means translate the following item to a boolean value if ( !!get_post_meta( $post_id , 'inprint' , true ) ) { $checked = 'checked'; } else { $checked = ''; } echo "<input type='checkbox' readonly $checked/>"; break; case 'book_author': # ... } }scripts/admin_edit.js:
(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 $book_author = $( '.column-book_author', $post_row ).text(); var $inprint = !! $('.column-inprint>*', $post_row ).prop('checked'); // populate the data $( ':input[name="book_author"]', $edit_row ).val( $book_author ); $( ':input[name="inprint"]', $edit_row ).prop('checked', $inprint ); } }; })(jQuery);Skip to note 5 content
Steven Lin
Example migrated from Codex:
Creating Inputs
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 column-<?php echo $column_name; ?>"> <label class="inline-edit-group"> <span class="title">Author</span><input name="book_author" /><span class="title">In Print</span><input name="inprint" type="checkbox" /> </label> </div> </fieldset> </pre> </div><!-- .comment-content --> <section id='feedback-4394' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'> </section><!-- .feedback --> <footer class='feedback-links wporg-dot-link-list' > <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fquick_edit_custom_box%2F%3Freplytocom%3D4394%23feedback-editor-4394" rel="nofollow">Log in to add feedback</a></footer> </article><!-- .comment-body --> </li> <li id="comment-4395" data-comment-id="4395" class="comment byuser comment-author-stevenlinx odd alt thread-odd thread-alt depth-1"> <article id="div-comment-4395" class="comment-body"> <a href="#comment-content-4395" class="screen-reader-text">Skip to note 6 content</a> <header class="comment-meta"> <div class="comment-author vcard"> <span class="comment-author-attribution"> <a href="https://profiles.wordpress.org/stevenlinx/" rel="external nofollow" class="url">Steven Lin</a> </span> <a class="comment-date" href="https://developer.wordpress.org/reference/hooks/quick_edit_custom_box/#comment-4395"> <time datetime="2020-10-15T13:25:15+00:00"> 6 years ago </time> </a> </div> <div class="user-note-voting" data-nonce="0608ee92d6" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="4395" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fquick_edit_custom_box%2F%23comment-4395"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="100% like this"><span class="screen-reader-text">Vote results for this note: </span>1</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="4395" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fquick_edit_custom_box%2F%23comment-4395"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div> </header> <!-- .comment-metadata --> <div class="wporg-has-embedded-code comment-content" id="comment-content-4395"> <p>Example migrated from Codex:</p> <p><strong>Saving Data</strong></p> <p>Data entered in custom inputs can be saved by hooking the <a href="https://developer.wordpress.org/reference/hooks/save_post/">save_post</a> action.</p> <pre class="wp-block-code"><code lang="php" class="language-php line-numbers">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); } }