通过AJAX从帖子列表中更改帖子元数据

时间:2013-05-04 作者:Oliver Whysall

我已经找了一段时间了,但还没有找到任何东西。

我有一个自定义的帖子类型——“books”,这个帖子类型有一个自定义的元字段“availability”。

我想为帖子类型设置一列,您可以单击可用性链接,它会将自定义元值从“是”更改为“否”,并更新文本链接,以显示值为“是”的“是”或值为“否”的“否”。

我已经制作了post列,我已经得到了显示值的列,只需要让它与ajax一起工作,我不知道从哪里开始!

我研究了不久前在这里找到的一个插件,该插件涉及使用ajax更改专栏的帖子状态,但我看不到如何将其更改为使用自定义元数据-Custom column for changing post status via ajax

我希望能得到任何帮助,甚至能指出正确的方向,因为这已经困扰了我好几天了!

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

链接答案中的解决方案存在一些问题。但这是一个很好的起点。

基本上你只需要改变方法ajax_change_status(). 此方法会更改帖子状态,但您希望更改帖子元。

简而言之,这是最后一堂课,有一些固定内容

/**
 * Plugin Name: Change Book Availability On Post List
 * Plugin URI:  http://wordpress.stackexchange.com/questions/98295/change-post-meta-via-ajax-from-the-posts-list-table
 * Description: Changing the status of availability in post list
 * Version:     0.1
 * Author:      Ralf Albert
 * Author URI:  http://yoda.neun12.de
 * License:     GPLv3
 */


if ( ! class_exists( \'Ajax_Book_Status\' ) ) {

class Ajax_Book_Status
{

    public $metakey = \'book_status\';

    //constarctor
    public function __construct() {

        global $pagenow,$typenow; //&& $typenow ==\'page\'

        if ( is_admin()  && $pagenow==\'edit.php\' )
            add_filter(\'admin_footer\',array( $this,\'insert_ajax_status_script\' ) );

        add_filter( \'manage_edit-post_columns\', array( $this,\'add_new_columns\' ) );
        add_action( \'manage_post_posts_custom_column\', array( $this, \'manage_columns\' ), 10, 2 );

        //manage columns
        add_filter( \'manage_pages_columns\', array( $this,\'add_new_columns\' ) );
        add_action( \'manage_pages_custom_column\', array( $this, \'manage_columns\' ), 10, 2 );

        //ajax function
        add_action( \'wp_ajax_change_book_status\', array( $this,\'ajax_change_book_status\' ) );
    }

    /**
     * the function that will actually change the post status
     *
     * $post_id - The ID of the post you\'d like to change.
     * $status -  The post status yes|no
     */
    public function ajax_change_book_status(){

        $result = \'\';
        $new_postmeta = \'\';
        $postid = filter_input( INPUT_GET, \'post_id\', FILTER_SANITIZE_NUMBER_INT );

        if ( ! isset( $postid ) )
            $result = \'something went wrong ...\';

        $old_postmeta = (string) get_post_meta( $postid, $this->metakey, true );

        // if no status was saved, predefine the new status to no.
        if ( ! in_array( $old_postmeta, array( \'yes\', \'no\' ) ) ) {
            $old_postmeta = \'no\';
        }

        $new_postmeta = ( $old_postmeta === \'yes\' ) ?
            \'no\' : \'yes\';


        if ( empty( $result ) )
            $result = \'change status to \' . $new_postmeta;

        update_post_meta( $postid, $this->metakey, $new_postmeta, $old_postmeta );

        header( \'Content-type: application/json\' );
        die( json_encode( array( \'data\' => $result, \'text\' => ucfirst( $new_postmeta ) ) ) );
    }

    /*
     ****************************
    * manage columns functions *
    ****************************
    */

    //add new columns function
    public function add_new_columns( $columns ){
        $columns[\'status\']= __(\'Status\');
        return $columns;
    }

    //render columns function
    public function manage_columns( $column_name, $id ) {

        // bail if it is not our column
        if ( \'status\' !== $column_name )
            return;

        $status = get_post_meta( $id, $this->metakey, true );

        if ( ! in_array( $status, array( \'yes\', \'no\' ) ) )
            $status = \'no\';

        printf( \'<div class="book-status"><a href="#" class="book-availability" pid="%d">%s</a></div>\', $id, ucfirst( $status ) );

    }


    //js/jquery code to call ajax
    public function insert_ajax_status_script(){
        ?>
            <script type="text/javascript">

            jQuery(document).ready(
                function($){
                    $( \'.book-availability\' ).click(
                        function(){
                            link = $(this);
                            $.get(
                                ajaxurl,
                                {
                                    \'action\'  : \'change_book_status\',
                                    \'post_id\' : $(this).attr( \'pid\' )
                                },
                                function(result){
                                    if( \'\' !== result.text )
                                        link.text( result.text );
                                    else
                                        alert( result.data );
                                }
                            );
                        }
                    );
                }
            );

            </script>
            <?php
        }

}
}

add_action( \'plugins_loaded\', \'bookstatuscolumn\' );

function bookstatuscolumn(){
    new Ajax_Book_Status();
}
如您所见,插入新列的部分几乎未被触及。只是一些调整。

基本更改是注册方法ajax_change_book_status() 作为ajax回调。该方法请求Posteta,在yesno 并存储新状态。仅此而已。

您必须调整metakey 计算开始时的值到您在自己的Posteta中使用的键。

结束