管理员快速编辑帖子中的现有栏目

时间:2020-10-02 作者:Nath

我一直在搜索这个,我只找到了要创建的新列。。。。所以

我有一个名为Views的列,它显示了帖子的视图数。admin中的列名为tie\\u post\\u views,post meta字段为tie\\u views

我想通过快速编辑帖子选项编辑并保存该字段中的值。

我感谢你的帮助。

编辑:

我发现了一个新代码,它是;“工程”;但这是添加了一个新的专栏,我只编辑了meta,但不完全是我需要的,因为我已经有了这个专栏




     /*
 * New columns
 */
add_filter(\'manage_post_posts_columns\', \'misha_price_and_featured_columns\');
// the above hook will add columns only for default \'post\' post type, for CPT:
// manage_{POST TYPE NAME}_posts_columns
function misha_price_and_featured_columns( $column_array ) {
 
    $column_array[\'price\'] = \'Price\';
    
    // the above code will add columns at the end of the array
    // if you want columns to be added in another place, use array_slice()
 
    return $column_array;
}
 
/*
 * Populate our new columns with data
 */
add_action(\'manage_posts_custom_column\', \'misha_populate_both_columns\', 10, 2);
function misha_populate_both_columns( $column_name, $id ) {
 
    // if you have to populate more that one columns, use switch()
    switch( $column_name ) :
        case \'price\': {
            echo \'$\'.get_post_meta( $id, \'tie_views\', true );
            break;
        }
        
    endswitch;
 
}
/*
 * quick_edit_custom_box allows to add HTML in Quick Edit
 * Please note: it files for EACH column, so it is similar to manage_posts_custom_column
 */
add_action(\'quick_edit_custom_box\',  \'misha_quick_edit_fields\', 10, 2);
 
function misha_quick_edit_fields( $column_name, $post_type ) {
 
    // you can check post type as well but is seems not required because your columns are added for specific CPT anyway
 
    switch( $column_name ) :
        case \'price\': {
 
            // you can also print Nonce here, do not do it ouside the switch() because it will be printed many times
            wp_nonce_field( \'misha_q_edit_nonce\', \'misha_nonce\' );
 
            // please note: the  classes could be:
            // inline-edit-col-left, inline-edit-col-center, inline-edit-col-right
            // each class for each column, all columns are float:left,
            // so, if you want a left column, use clear:both element before
            // the best way to use classes here is to look in browser "inspect element" at the other fields
 
            // for the FIRST column only, it opens  element, all our fields will be there
            echo \'
                
                    \';
 
            echo \'
                    Price
                    
                \';
 
            break;
 
        }
        
 
    endswitch;
 
}

/*
 * Quick Edit Save
 */
add_action( \'save_post\', \'misha_quick_edit_save\' );
 
function misha_quick_edit_save( $post_id ){
 
    // check user capabilities
    if ( !current_user_can( \'edit_post\', $post_id ) ) {
        return;
    }
 
    // check nonce
    if ( !wp_verify_nonce( $_POST[\'misha_nonce\'], \'misha_q_edit_nonce\' ) ) {
        return;
    }
 
    // update the price
    if ( isset( $_POST[\'price\'] ) ) {
        update_post_meta( $post_id, \'tie_views\', $_POST[\'price\'] );
    }
 
    
 
}


1 个回复
SO网友:Max Yudin

这是我的meccano,它是用周围的一些碎片做成的。这段代码来自我的一个未完成和废弃的项目,但它起到了作用(在我个人记忆中)。

注意,我在元名称前面加了下划线(_tie_post_views) 避免其出现在编辑后页面的自定义字段元框中。

您将被迫使用字段定位玩一段时间(可能使用JavaScript?)因为快速编辑和批量编辑实现存在缺陷。

很抱歉,我现在无法描述和评论该代码,但有一天我会尝试回答相关问题。而且,它看起来几乎不言自明。

<?php

///////////////////////// Quick Edit

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


// Empty form field, data will be populated by JavaScript
function tie_display_bulk_quick_edit_postviews( $column ) {

    if ( \'tie_post_views\' === $column ) {

        ?>
        <fieldset class="inline-edit-col-right">
            <div class="inline-edit-col">
                <div class="inline-edit-group wp-clearfix">
                    <label class="alignleft" for="tie_post_views"><span class="title"><?php _e( \'Tie Post Views\', \'textdomain\' ); ?></span></label>
                    <input type="text" name="_tie_post_views" id="tie_post_views" value="" />
                </div>
            </div>
        </fieldset>
        <?php
    }
}


add_action( \'admin_enqueue_scripts\', \'tie_bulk_quick_edit_script\' );

function tie_bulk_quick_edit_script() {
    wp_enqueue_script(
        \'tie-bulk-quick-edit\',
        plugins_url( \'js/tie-bulk-quick-edit.js\', __FILE__ ),
        array(
            \'jquery\',
            \'inline-edit-post\',
        )
    );
}


add_action( \'save_post\', \'tie_save_quick_edit_meta_post_views\', 10, 2 );

function tie_save_quick_edit_meta_post_views( $post_id, $post ) {

    // pointless if $_POST is empty (this happens on Bulk Edit)
    if ( empty( $_POST ) ) {
        return $post_id;
    }

    // verify Quick Edit nonce
    if ( isset( $_POST[\'_inline_edit\'] ) && ! wp_verify_nonce( $_POST[\'_inline_edit\'], \'inlineeditnonce\' ) ) {
        return $post_id;
    }

    // Regular post meta update goes here

}

///////////////////////// Bulk Edit
// https://developer.wordpress.org/reference/hooks/wp_ajax_action/
add_action( \'wp_ajax_tie_post_views_bulk_save\', \'tie_post_views_bulk_save\' );

function tie_post_views_bulk_save() {

    $post_ids = ( isset( $_POST[\'post_ids\'] ) && ! empty( $_POST[\'post_ids\'] ) ) ? $_POST[\'post_ids\'] : null;

    if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {

        if ( isset( $_POST[\'_tie_post_views\'] ) && ! empty( $_POST[\'_tie_post_views\'] ) ) {

            foreach ( $post_ids as $post_id ) {
                update_post_meta( $post_id, \'_tie_post_views\', $_POST[\'_tie_post_views\'] );
            }
        }
    }

}
以及tie-bulk-quick-edit.js (应完全重写,但它将继续):

jQuery(document).ready(function($){

    //Prepopulating quick-edit
    var $inline_editor = inlineEditPost.edit;
    inlineEditPost.edit = function(id){

        $inline_editor.apply(this, arguments);

        var post_id = 0;
        if( typeof(id) == \'object\'){
            post_id = parseInt(this.getId(id));
        }

        if(post_id != 0){
            $row = $(\'#edit-\' + post_id);

            tie_post_views = $(\'#post-\' + post_id + \' .tie_post_views\').text();
            $row.find(\'#tie_post_views\').val(tie_post_views);
        }
    }

    // Detect \'Enter\' key press inside \'Bulk Edit\' and click \'Update\' button
    // https://stackoverflow.com/a/18160418/
    $(\'#bulk-edit\').keypress(function (e) {
        var key = e.which;
        if(key == 13) {
            $(\'#bulk_edit\').click();
            return false;
        }
    });

    // Live() is obsolete
    // This is workaround to avoid refactoring
    // Think about a better solution
    // https://stackoverflow.com/questions/14354040/jquery-1-9-live-is-not-a-function
    if (typeof jQuery.fn.live == \'undefined\' || !(jQuery.isFunction(jQuery.fn.live))) {
        jQuery.fn.extend({
            live: function (event, callback) {
                if (this.selector) {
                    jQuery(document).on(event, this.selector, callback);
                }
            }
       });
    }

    $( \'#bulk_edit\' ).live( \'click\', function(e) {

        var $bulk_row = $( \'#bulk-edit\' );

        var $post_ids = new Array();
        $bulk_row.find( \'#bulk-titles\' ).children().each( function() {
            $post_ids.push( $( this ).attr( \'id\' ).replace( /^(ttle)/i, \'\' ) );
        });

        var $tie_post_views = $bulk_row.find( \'input[name="_tie_post_views"]\' ).val();

        $.ajax({
            url: ajaxurl,
            type: \'POST\',
            async: false,
            cache: false,
            data: {
                action: \'tie_post_views_bulk_save\',
                _tie_post_views: $tie_post_views
            }
        });
    });
});

相关推荐