为什么在编辑帖子屏幕上删除headline\\u新闻元数据,并修复qedit_save_post()
功能stomps onheadline_news
因为$_POST[\'headline_news\']
使用后期编辑屏幕时未设置。
因为自定义字段编辑器用于headline_news
而且没有涉及自定义的元框,我们将让内置的自定义字段编辑器处理元数据的保存,并且在使用edit Post屏幕时故意不触发快速编辑保存逻辑。E、 g.:
// Handle saving of headline_news via Quick Edit. This code will not fire on the post edit screen.
// The post edit screen will handle the field via the custom field editor.
if ( isset( $_POST[ \'_inline_edit\' ] ) && wp_verify_nonce( $_POST[ \'_inline_edit\' ], \'inlineeditnonce\' ) ) {
if ( isset( $_POST[\'headline_news\'] ) ) {
update_post_meta( $post_id, \'headline_news\', \'yes\' );
} else {
delete_post_meta( $post_id, \'headline_news\' );
}
}
重复的标题新闻位置复选框和修复最初,我无法再现出现重复标题新闻位置复选框的现象,但在进一步查看后,我注意到原始代码没有检查$column_name
在quick_edit_add()
作用这个quick_edit_custom_box
行动is called once for each custom column, 因此,我假设实际上有多个自定义列被添加到您的帖子列表中,而此代码不是原始问题的一部分(这些列可以通过插件或主题添加)。我们可以为适当的列添加检查,以确保每个列只呈现一次输出:
// Print checkbox in Quick Edit for each custom column.
add_action( \'quick_edit_custom_box\', \'quick_edit_add\', 10, 2 );
function quick_edit_add( $column_name, $post_type ) {
// Handle the headline_news checkbox
// Note the added check. This prevents the output from being
// rendered for every custom column & allows us to handle each individual column.
switch ( $column_name ) {
case \'headline_news\' :
printf( \'<input type="checkbox" name="headline_news" class="headline_news"> %s\',
__( \'Headline news position\', \'text-domain\' )
);
break;
// Example of how another column could be incorporated:
//case \'another_column\' :
//printf( \'<input type="checkbox" name="another_column" class="another_column"> %s\',
// __( \'Another Column\', \'text-domain\' )
//);
//break;
}
}
完整的工作解决方案:这是原始代码,上面提到的编辑帖子屏幕的修复,重复的标题新闻位置复选框的修复,以及一些主要与WordPress编码标准相关的小调整。
// Add column to posts listing
add_filter( \'manage_post_posts_columns\', \'add_columns\' );
function add_columns( $columns ) {
// Add the Headline News custom column.
$columns[\'headline_news\'] = __( \'Headline news\', \'text-domain\' );
// Perhaps add other custom columns too.
// $columns[\'another_column\'] = __( \'Another Column\', \'text-domain\' );
return $columns;
}
// Echo contents of custom field in column
add_action( \'manage_posts_custom_column\', \'columns_content\', 10, 2 );
function columns_content( $column_name, $post_id ) {
switch ( $column_name ) {
case \'headline_news\' :
$headline_news = get_post_meta( $post_id, \'headline_news\', true );
echo esc_html( $headline_news );
break;
// Example of how another column could be incorporated:
//case \'another_column\' :
// $another_column = get_post_meta( $post_id, \'another_column\', true );
// echo esc_html( $another_column );
//break;
}
}
// Print checkbox in Quick Edit for each custom column.
add_action( \'quick_edit_custom_box\', \'quick_edit_add\', 10, 2 );
function quick_edit_add( $column_name, $post_type ) {
// Handle the headline_news checkbox
// Note the added check. This prevents the output from being
// rendered for every custom column & allows us to handle each individual column.
switch ( $column_name ) {
case \'headline_news\' :
printf( \'<input type="checkbox" name="headline_news" class="headline_news"> %s\',
__( \'Headline news position\', \'text-domain\' )
);
break;
// Example of how another column could be incorporated:
//case \'another_column\' :
//printf( \'<input type="checkbox" name="another_column" class="another_column"> %s\',
// __( \'Another Column\', \'text-domain\' )
//);
//break;
}
}
// Save checkbox value
add_action( \'save_post\', \'qedit_save_post\', 10, 2 );
function qedit_save_post( $post_id, $post ) {
// pointless if $_POST is empty (this happens on bulk edit)
if ( empty( $_POST ) ) {
return $post_id;
}
// Ensure quick edit nonce is set.
if ( empty( $_POST[ \'_inline_edit\' ] ) ) {
return $post_id;
}
// Verify quick edit nonce
if ( ! wp_verify_nonce( $_POST[ \'_inline_edit\' ], \'inlineeditnonce\' ) ) {
return $post_id;
}
// Don\'t save for autosave
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// dont save for revisions
if ( isset( $post->post_type ) && \'revision\' === $post->post_type ) {
return $post_id;
}
// Handle saving of headline_news via Quick Edit. This code will not fire on the post edit screen.
// The post edit screen will handle the field via the custom field editor.
if ( isset( $_POST[ \'_inline_edit\' ] ) && wp_verify_nonce( $_POST[ \'_inline_edit\' ], \'inlineeditnonce\' ) ) {
if ( isset( $_POST[\'headline_news\'] ) ) {
update_post_meta( $post_id, \'headline_news\', \'yes\' );
} else {
delete_post_meta( $post_id, \'headline_news\' );
}
}
}
// JavaScript functions to set/update checkbox
add_action( \'admin_footer\', \'quick_edit_javascript\' );
function quick_edit_javascript() {
global $current_screen;
if ( \'post\' !== $current_screen->post_type ) {
return;
} ?>
<script type="text/javascript">
function checked_headline_news( fieldValue ) {
inlineEditPost.revert();
jQuery( \'.headline_news\' ).attr( \'checked\', 0 == fieldValue ? false : true );
}
</script><?php
}
add_filter( \'post_row_actions\', \'expand_quick_edit_link\', 10, 2 );
function expand_quick_edit_link( $actions, $post ) {
global $current_screen;
$data = get_post_meta( $post->ID, \'headline_news\', true );
$data = empty( $data ) ? 0 : 1;
$actions[\'inline hide-if-no-js\'] = \'<a href="#" class="editinline"\';
$actions[\'inline hide-if-no-js\'] .= \' title="\' . esc_attr( __( \'Edit this item inline\', \'text-domain\' ) ) . \'"\';
$actions[\'inline hide-if-no-js\'] .= " onclick=\\"checked_headline_news(\'{$data}\')\\" >";
$actions[\'inline hide-if-no-js\'] .= __( \'Quick Edit\', \'text-domain\' );
$actions[\'inline hide-if-no-js\'] .= \'</a>\';
return $actions;
}