钩子文档

bulk_edit_custom_box

💡 云策文档标注

概述

bulk_edit_custom_box 是一个 WordPress 动作钩子,用于在批量编辑模式下为自定义列输出输入字段。它允许插件开发者为每个自定义列添加表单控件,以便批量更新文章元数据。

关键要点

  • bulk_edit_custom_box 是一个动作钩子,在批量编辑模式下为每个自定义列触发一次。
  • 自定义列通过 manage_edit-${post_type}_columns 过滤器添加,数据保存通常通过 save_post 钩子或 Ajax 调用处理。
  • 该钩子接收两个参数:$column_name(列名)和 $post_type(文章类型 slug),但不传递文章 ID 或现有列值。
  • 与 quick_edit_custom_box 类似,但批量编辑的数据保存可能需要 Ajax 实现,以处理多篇文章的更新。

代码示例

// 添加自定义输入字段到批量编辑界面
add_action( 'bulk_edit_custom_box', 'display_custom_quickedit_book', 10, 2 );

// 通过 Ajax 保存批量编辑数据
add_action( 'wp_ajax_save_bulk_edit_book', 'save_bulk_edit_book' );
function save_bulk_edit_book() {
    $post_ids = ( ! empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : array();
    $book_author = ( ! empty( $_POST[ 'book_author' ] ) ) ? $_POST[ 'book_author' ] : null;
    $inprint = !! empty( $_POST[ 'inprint' ] );

    if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
        foreach( $post_ids as $post_id ) {
            update_post_meta( $post_id, 'book_author', $book_author );
            update_post_meta( $post_id, 'inprint', $inprint );
        }
    }
    die();
}

注意事项

  • bulk_edit_custom_box 钩子不提供文章 ID 或现有列值,开发者需自行处理数据获取和保存逻辑。
  • 批量编辑的数据保存通常使用 Ajax 调用,以避免页面刷新并提高用户体验,但也可结合 save_post 钩子实现。
  • 确保在 JavaScript 中正确处理批量编辑行的数据填充和 Ajax 请求,如示例中的 admin_edit.js 所示。

📄 原文内容

Fires once for each column in Bulk Edit mode.

Parameters

$column_namestring
Name of the column to edit.
$post_typestring
The post type slug.

More Information

bulk_edit_custom_box is an action that lets plugin print inputs for custom columns when bulk 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.

Note that the action function is passed neither the post ID nor any existing value for the column.

See quick_edit_custom_box for basis.

Source

do_action( 'bulk_edit_custom_box', $column_name, $screen->post_type );

Changelog

Version Description
2.7.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    (From Codex)
    Saving Data

    Unlike quick edit’s data saving process, we’ll save via Ajax calls.

    add_action( 'wp_ajax_save_bulk_edit_book', 'save_bulk_edit_book' );
    function save_bulk_edit_book() {
    	// TODO perform nonce checking
    	// get our variables
    	$post_ids           = ( ! empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : array();
    	$book_author  = ( ! empty( $_POST[ 'book_author' ] ) ) ? $_POST[ 'book_author' ] : null;
    	$inprint = !! empty( $_POST[ 'inprint' ] );
    
    	// if everything is in order
    	if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
    		foreach( $post_ids as $post_id ) {
    			update_post_meta( $post_id, 'book_author', $book_author );
    			update_post_meta( $post_id, 'inprint', $inprint );
    		}
    	}
    
    	die();
    }

  2. Skip to note 6 content

    (From Codex)
    Calling Ajax

    Our updated scripts/admin_edit.js from quick_edit_custom_box.

    (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 ).html();
    			var $inprint = $( '.column-inprint', $post_row ).attr('checked');
    
    			// populate the data
    			$( ':input[name="book_author"]', $edit_row ).val( $book_author );
    			$( ':input[name="inprint"]', $edit_row ).attr('checked', $inprint );
    		}
    	};
    
    	$( document ).on( 'click', '#bulk_edit', function() {
    		// define the bulk edit row
    		var $bulk_row = $( '#bulk-edit' );
    
    		// get the selected post ids that are being edited
    		var $post_ids = new Array();
    		$bulk_row.find( '#bulk-titles' ).children().each( function() {
    			$post_ids.push( $( this ).attr( 'id' ).replace( /^(ttle)/i, '' ) );
    		});
    
    		// get the data
    		var $book_author = $bulk_row.find( 'textarea[name="book_author"]' ).val();
    		var $inprint = $bulk_row.find( 'input[name="inprint"]' ).attr('checked') ? 1 : 0;
    
    		// save the data
    		$.ajax({
    			url: ajaxurl, // this is a variable that WordPress has already defined for us
    			type: 'POST',
    			async: false,
    			cache: false,
    			data: {
    				action: 'save_bulk_edit_book', // this is the name of our WP AJAX function that we'll set up next
    				post_ids: $post_ids, // and these are the 2 parameters we're passing to our function
    				book_author: $book_author,
    				inprint: $inprint
    			}
    		});
    	});
    })(jQuery);