如何在“快速编辑”屏幕上显示自定义元框?

时间:2010-10-26 作者:Scott B

我已经使用add\\u meta\\u box()在页面和帖子上向WordPress编辑窗口添加了一个自定义meta框。

如何使此元框也显示在“快速编辑”屏幕上?

理想情况下,我希望它只显示在类别选择器的右侧。

2 个回复
最合适的回答,由SO网友:Jan Fabry 整理而成

要做到这一点似乎没有简单的方法,您必须自己添加所有代码。inline_edit_row(), 绘制快速编辑和批量编辑屏幕的功能似乎只有一个可以挂接的操作:quick_edit_custom_box or bulk_edit_custom_box. 所有非核心列都会调用它wp_manage_posts_columns() 返回。例如,可以使用一些过滤器添加列manage_posts_columns. 遗憾的是,此函数定义了post表的列标题,因此您应该在print_column_headers() 打印它们。这可以在get_column_headers() 功能,带the manage_[screen_id]_headers filter. edit-post 是“编辑帖子”屏幕的屏幕id。

总的来说,这提供了一个如下的黑客添加一些代码。找出您可以在哪里处理表单提交(目前)留给读者作为练习。

// Add a dummy column for the `posts` post type    
add_filter(\'manage_posts_columns\', \'add_dummy_column\', 10, 2);
function add_dummy_column($posts_columns, $post_type)
{
    $posts_columns[\'dummy\'] = \'Dummy column\';
    return $posts_columns;
}
// But remove it again on the edit screen (other screens to?)
add_filter(\'manage_edit-post_columns\', \'remove_dummy_column\');
function remove_dummy_column($posts_columns)
{
    unset($posts_columns[\'dummy\']);
    return $posts_columns;
}

// Add our text to the quick edit box
add_action(\'quick_edit_custom_box\', \'on_quick_edit_custom_box\', 10, 2);
function on_quick_edit_custom_box($column_name, $post_type)
{
    if (\'dummy\' == $column_name) {
        echo \'Extra content in the quick edit box\';
    }
}

// Add our text to the bulk edit box
add_action(\'bulk_edit_custom_box\', \'on_bulk_edit_custom_box\', 10, 2);
function on_bulk_edit_custom_box($column_name, $post_type)
{
    if (\'dummy\' == $column_name) {
        echo \'Extra content in the bulk edit box\';
    }
}

SO网友:Michael Cannon

上述答案并没有提供保存数据的明确方法。以下代码来自使用video 在我编写WordPress批量编辑插件之前自定义帖子类型Custom Bulk/Quick Edit.

File quick_edit.js

// @ref http://rachelcarden.com/2012/03/manage-wordpress-posts-using-bulk-edit-and-quick-edit/
(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 $additional_copies = $( \'.column-additional_copies\', $post_row ).html();
            var $main_credits      = $( \'.column-main_credits\', $post_row ).html();

            // populate the data
            $( \':input[name="additional_copies"]\', $edit_row ).val( $additional_copies );
            $( \':input[name="main_credits"]\', $edit_row ).val( $main_credits );
        }
    };

    $( \'#bulk_edit\' ).live( \'click\', 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 $additional_copies = $bulk_row.find( \'textarea[name="additional_copies"]\' ).val();
        var $main_credits      = $bulk_row.find( \'textarea[name="main_credits"]\' ).val();

        // 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_video\', // 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
                additional_copies: $additional_copies,
                main_credits: $main_credits
            }
        });
    });
})(jQuery);

File video-quick-edit.php

<?php
/**
 *  Quick Edit and Bulk Edit helper for Media Burn video records
 *
 *  @author Michael Cannon <[email protected]>
 *  @ref http://rachelcarden.com/2012/03/manage-wordpress-posts-using-bulk-edit-and-quick-edit/
 */

add_action( \'bulk_edit_custom_box\', \'quick_edit_custom_box_video\', 10, 2 );
add_action( \'quick_edit_custom_box\', \'quick_edit_custom_box_video\', 10, 2 );

function quick_edit_custom_box_video( $column_name, $post_type ) {
    $slug = \'video\';
    if ( $slug !== $post_type )
        return;

    if ( ! in_array( $column_name, array( \'additional_copies\', \'main_credits\' ) ) )
        return;

    static $printNonce = true;
    if ( $printNonce ) {
        $printNonce = false;
        wp_nonce_field( plugin_basename( __FILE__ ), \'video_edit_nonce\' );
    }

?>
    <fieldset class="inline-edit-col-right inline-edit-video">
      <div class="inline-edit-col inline-edit-<?php echo $column_name ?>">
        <label class="inline-edit-group">
        <?php
    switch ( $column_name ) {
    case \'additional_copies\':
?>
            <span class="title">Additional Copies</span>
            <textarea cols="22" rows="1" name="additional_copies" autocomplete="off"></textarea>
            <?php
        break;
    case \'main_credits\':
?>
            <span class="title">Main Credits</span>
            <textarea cols="22" rows="1" name="main_credits" autocomplete="off"></textarea>
            <?php
        break;
    }
?>
        </label>
      </div>
    </fieldset>
    <?php
}


add_action( \'save_post\', \'save_video_meta\' );

function save_video_meta( $post_id ) {
    // TODO make $slug static
    $slug = \'video\';
    if ( $slug !== $_POST[\'post_type\'] )
        return;

    if ( !current_user_can( \'edit_post\', $post_id ) )
        return;

    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
        return;

    if ( isset( $post->post_type ) && \'revision\' == $post->post_type )
        return;

    $_POST += array( "{$slug}_edit_nonce" => \'\' );
    if ( !wp_verify_nonce( $_POST["{$slug}_edit_nonce"], plugin_basename( __FILE__ ) ) )
        return;

    if ( isset( $_REQUEST[\'additional_copies\'] ) )
        update_post_meta( $post_id, \'additional_copies\', wp_kses_post( $_REQUEST[\'additional_copies\'] ) );

    if ( isset( $_REQUEST[\'main_credits\'] ) )
        update_post_meta( $post_id, \'main_credits\', wp_kses_post( $_REQUEST[\'main_credits\'] ) );
}


add_action( \'admin_print_scripts-edit.php\', \'admin_edit_video_foot\' );
function admin_edit_video_foot() {
    $slug = \'video\';
    // load only when editing a video
    if ( ( isset( $_GET[\'page\'] ) && $slug == $_GET[\'page\'] )
        || ( isset( $_GET[\'post_type\'] ) && $slug == $_GET[\'post_type\'] ) ) {
        wp_enqueue_script( \'admin-quick-edit-video\', get_template_directory_uri() . \'/functions/user/custom/fitv/quick_edit.js\', array( \'jquery\', \'inline-edit-post\' ), \'\', true );
    }
}


add_action( \'wp_ajax_save_bulk_edit_video\', \'save_bulk_edit_video\' );
function save_bulk_edit_video() {
    $post_ids          = ( ! empty( $_POST[ \'post_ids\' ] ) ) ? $_POST[ \'post_ids\' ] : array();
    $additional_copies = ( ! empty( $_POST[ \'additional_copies\' ] ) ) ? wp_kses_post( $_POST[ \'additional_copies\' ] ) : null;
    $main_credits      = ( ! empty( $_POST[ \'main_credits\' ] ) ) ? wp_kses_post( $_POST[ \'main_credits\' ] ) : null;

    if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
        foreach ( $post_ids as $post_id ) {
            update_post_meta( $post_id, \'additional_copies\', $additional_copies );
            update_post_meta( $post_id, \'main_credits\', $main_credits );
        }
    }

    die();
}


?>

结束

相关推荐

我可以将类别Metabox添加到附件吗?

我在用register_taxonomy_for_object_type() 将类别分类字段添加到媒体上载(附件)。我正在使用此代码执行此操作:add_action(\'init\', \'reg_tax\'); function reg_tax() { register_taxonomy_for_object_type(\'category\', \'attachment\'); } 这可以在查看图像时为媒体页面添加一个简单的分类文本字段。我真正想要的是让它显示实