5

I'm currently trying to implent a new custom column at the page manage screen that will let me change the status (published/pending) of the different pages via an easy toggle link.

From what I've gathered I need to use $post->post_status, 'status'in someway, probably toggle with jQuery or something. But I can't figure out how I tie it together with ajax. Thought I'd get some help looking at the default post status modifier in the edit post screen, but I realized that it's not ajax based, I the status updates when you press "update" (obviously, duh).

Here's my lousy try for an attempt so far:

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

Would be really grateful if someone could point me in the right direction..

13

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();
  • Wow, thanks so much for this! Really appreciate that you took your time to do this. I added this to my dev site but it only seems to work with posts and not pages. How do I enable it for both types? (and possibly custom post types). – INT Nov 11 '11 at 14:44
  • I just realized that it does work with pages, sorry. BUT the thing is, it only work with the topmost post/page. If you try to change status on the other ones, nothing happens (ajax doesn't fire, check this vid: youtu.be/Q1APmCL6DtE) – INT Nov 16 '11 at 19:15
  • try it know, i've updated the code. – Bainternet Nov 16 '11 at 23:22
  • Btw: ajax_change_status() is redundant in it's internals. First ! isset than isset than else... :) +1 anyway. – kaiser Dec 20 '11 at 19:33
  • @Bainternet , the plugin works like a charm! However, if I load the scripts on upload.php, it's working half way-- the column get added by "manage_upload_columns", and the ajax response to the click is correct, text got changed, BUT, the change is not saved. Would you please help on this? – Jenny Jun 17 '12 at 11:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.