bulk_edit_custom_box

最后更新于:2021-11-25 22:30:58

do_action( ‘bulk_edit_custom_box’, string $column_name, string $post_type )

Fires once for each column in Bulk Edit mode.

参数

$column_name

(string)
Name of the column to edit.

$post_type

(string)
The post type slug.

源文件

文件: gc-admin/includes/class-gc-posts-list-table.php

View on Trac

add_action( 'gc_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();
}

(function($) {
	// we create a copy of the GC inline edit post function
	var $gc_inline_edit = inlineEditPost.edit;
	// and then we overwrite the function with our own code
	inlineEditPost.edit = function( id ) {
		// "call" the original GC edit function
		// we don't want to leave GeChiUI hanging
		$gc_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 GeChiUI has already defined for us
			type: 'POST',
			async: false,
			cache: false,
			data: {
				action: 'save_bulk_edit_book', // this is the name of our GC 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);