用于通过AJAX更改帖子状态的自定义列

时间:2011-11-10 作者:INT

我目前正在尝试在页面管理屏幕上实现一个新的自定义列,它将允许我通过一个简单的切换链接更改不同页面的状态(已发布/挂起)。

根据我收集的信息,我需要使用$post->post\\u status,“status”,在某种程度上,可能需要使用jQuery或其他工具进行切换。但我不知道如何将其与ajax结合在一起。我想在编辑帖子屏幕中查看默认的帖子状态修饰符会得到一些帮助,但我意识到它不是基于ajax的,我会在按下“更新”时更新状态(很明显,duh)。

以下是我迄今为止的糟糕尝试:

function wp_change_page_status_link() {
    $status = get_post_status($ID);
    if($status == \'publish\'){
        $status = \'<span>On</span>\';
        //$post->post_status, \'pending\'
    }
    elseif($status == \'pending\'){
        $status = \'<span>Off</span>\';
    }
    return $status;
}
如果有人能给我指出正确的方向,我将不胜感激。。

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

here you go:

<?php
/*
Plugin Name: ajaxed-status
Plugin URI: http://en.bainternet.info
Description: answer to : Custom column for changing post status via ajax
http://wordpress.stackexchange.com/questions/33442/custom-column-for-changing-post-status-via-ajax
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/



if ( !class_exists(\'ajaxed_status\')){
    class ajaxed_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_status\', array($this,\'ajax_change_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 publish|pending|draft|private|static|object|attachment|inherit|future|trash.
        */
        public function change_post_status($post_id,$status){
            $current_post = get_post( $post_id, \'ARRAY_A\' );
            $current_post[\'post_status\'] = $status;
            wp_update_post($current_post);
        }


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

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

        //rander columns function 
        public function manage_columns($column_name, $id) {
            global $wpdb,$post;
            if ("status" == $column_name){
                echo \'<div id="psatus">\';
                switch ($post->post_status) {
                    case \'publish\':
                        echo \'<a href="#" class="pb" change_to="pending" pid="\'.$id.\'">Published</a>\';
                        break;
                    case \'draft\':
                        echo \'<a href="#" class="pb" change_to="publish" pid="\'.$id.\'">Draft</a>\';
                        break;
                    case \'pending\':
                        echo \'<a href="#" class="pb" change_to="publish" pid="\'.$id.\'">Pending</a>\';
                        break;
                    default:
                        echo \'unknown\';
                        break;
                } // end switch
                echo \'</div>\';
            }
        }


        //js/jquery code to call ajax
        public function insert_ajax_status_script(){
            ?>
            <div id="status_update_working" style="background-color: green; color: #fff; font-wieght: bolder;   font-size: 22px;   height: 33px;   left: 40%;   padding: 35px;   position: fixed;   top: 100px;   width: 350px; display:none !important; ">Changing status...</div>
            <script type="text/javascript">

            function ajax_change_status(p){
                jQuery("#status_update_working").show(\'fast\');
                jQuery.getJSON(ajaxurl,
                    {   post_id: p.attr("pid"),
                        action: "change_status",
                        change_to: p.attr("change_to")
                    },
                    function(data) {
                        if (data.error){
                            alert(data.error);                      
                        }else{
                            p.text(data.text);
                            p.attr("change_to",data.change_to);
                        }
                    }
                );
                jQuery("#status_update_working").hide(\'9500\');
            }
            jQuery(document).ready(function(){
                jQuery(".pb").click(function(){
                    ajax_change_status(jQuery(this));
                });
            });
            </script>
            <?php
        }

        //ajax callback function
        public function ajax_change_status(){
            if (!isset($_GET[\'post_id\'])){
                $re[\'data\'] = \'something went wrong ...\';
                echo json_encode($re);
                die();
            }
            if (isset($_GET[\'change_to\'])){
                $this->change_post_status($_GET[\'post_id\'],$_GET[\'change_to\']);
                if ($_GET[\'change_to\'] == "pending"){
                    $re[\'text\'] = "Pending";
                    $re[\'change_to\'] = "publish";
                }else{
                    $re[\'text\'] = "Published";
                    $re[\'change_to\'] = "pending";
                }
            }else{
                $re[\'data\'] = \'something went wrong ...\';
            }
            echo json_encode($re);
            die();
        }
    }
}

new ajaxed_status();
结束

相关推荐

Post will not load via ajax

我继承了一个WP 3.2.1网站,该网站使用prettyPhoto 通过ajax加载内容。在我在站点上启用permalinks之前,这是可行的。已安装自定义Post类型UI插件。我可以在mysite浏览到我的自定义帖子类型。com/story/story的名称,但通过ajax加载会导致404。触发$的链接。在Prettypo中获取如下代码:<a href=\"<?php echo get_permalink(); ?>&ajax=true&count=<?php e