这是我的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
}
});
});
});