为什么此快速编辑复选框仅在取消选中时才保存值?

时间:2017-01-08 作者:BlueDogRanch

我正在使用下面的一组函数在帖子列表中的每个帖子的快速编辑区域中创建一个复选框wp-admin/edit.php 将值保存到名为headline_news.

问题是,当选中复选框时,“快速编辑”会保存值,但当我取消选中复选框时,它不会再次保存。通过PHPMyAdmin,在保存复选框时,数据库中的自定义字段值会更改,但在取消复选框并保存时不会更改。

前三个功能对于添加列、回显列内容和打印复选框来说是相当标准的。我猜不保存未选中框的问题一定是在Javascripts中的saving函数中。

// Add column to posts listing

add_filter( \'manage_post_posts_columns\', \'add_columns\' );
function add_columns( $columns ) {

    $columns[\'headline_news\'] = \'Headline news\';
    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 ) {

    if( $column_name == \'headline_news\' ) {
        $headline_news = get_post_meta( $post_id, \'headline_news\', true );
        echo $headline_news ;
}
}



// Print checkbox in Quick Edit

add_action( \'quick_edit_custom_box\', \'quick_edit_add\', 10, 2 );
function quick_edit_add( $column_name, $post_type ) {

    printf( \'
        <input type="checkbox" name="headline_news" class="headline_news"> %s\',
        \'Headline news position\'
    );
}



// 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;

    // verify quick edit nonce
    if ( isset( $_POST[ \'_inline_edit\' ] ) && ! 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 ) && $post->post_type == \'revision\' )
        return $post_id;

     // Save only the custom field

            $custom_fields = array( \'headline_news\' );

            foreach( $custom_fields as $field ) {

                if ( array_key_exists( $field, $_POST ) )
                    update_post_meta( $post_id, $field, $_POST[ $field ] );

            }
}





//  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" title="\';
    $actions[\'inline hide-if-no-js\']    .= esc_attr( \'Edit this item inline\' ) . \'"\';
    $actions[\'inline hide-if-no-js\']    .= " onclick=\\"checked_headline_news(\'{$data}\')\\" >";
    $actions[\'inline hide-if-no-js\']    .= \'Quick Edit\';
    $actions[\'inline hide-if-no-js\']    .= \'</a>\';

    return $actions;
}

Edit 1/23/17

将“保存”功能的一部分更改为现在可以使用:

    // Save only the custom field

   if (isset($_POST[\'headline_news\'])) {

                    update_post_meta( $post_id, \'headline_news\', \'yes\' );

                        } else {

                    delete_post_meta( $post_id, \'headline_news\' );

                }
它允许我从快速编辑中保存和删除自定义字段。但是,当我在完整帖子编辑器中编辑帖子时,编辑文本、更改类别等,自定义字段将被删除。那为什么会这样呢?如何在完整的帖子编辑器中保存,以避免更改此元字段?

和:“快速编辑”中为什么有两个复选框字段

enter image description here

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

为什么在编辑帖子屏幕上删除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_namequick_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;
}

相关推荐

400错误请求-JavaScript应用程序调用自定义wp-json终结点

我已成功创建了新的端点/路由,可以通过wp-json. 不仅如此,我还可以使用Restlet客户端从浏览器外部成功地向该端点发出post请求。我的端点句柄json 在请求主体内,转换和编辑数据,然后进行外部api调用,然后从外部api将新数据发送回客户端。然而,当我在wordpress站点上运行调用此api的应用程序时,我得到400 Bad Request. 我处理函数中许多地方的错误,并根据数据中可能出现的错误类型发回特定的错误消息,但我似乎找不到这些错误的来源。我已设置WP_DEBUG 到true,