quick_edit_custom_box

最后更新于:2021-11-27 21:38:31

do_action( ‘quick_edit_custom_box’, string $column_name, string $post_type, string $taxonomy )

Fires once for each column in Quick Edit mode.

参数

$column_name

(string)
Name of the column to edit.

$post_type

(string)
The post type slug, or current screen name if this is a taxonomy list table.

$taxonomy

(string)
The taxonomy name, if any.

源文件

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

View on Trac

/* load script in the footer */
if ( ! function_exists('gc_my_admin_enqueue_scripts') ):
function gc_my_admin_enqueue_scripts( $hook ) {

	if ( 'edit.php' === $hook &&
		isset( $_GET['post_type'] ) &&
		'book' === $_GET['post_type'] ) {

		gc_enqueue_script( 'my_custom_script', plugins_url('scripts/admin_edit.js', __FILE__),
			false, null, true );

	}

}
endif;
add_action( 'admin_enqueue_scripts', 'gc_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':
        # ...
    }
}

(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 ).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);

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;
        gc_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">
        <?php 
         switch ( $column_name ) {
         case 'book_author':
             ?><span class="title">Author</span><input name="book_author" /><?php
             break;
         case 'inprint':
             ?><span class="title">In Print</span><input name="inprint" type="checkbox" /><?php
             break;
         }
        ?>
        </label>
      </div>
    </fieldset>
    <?php
}

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