0

I try to add publish/draft button in Wordpress Admin Post Lists but it doesn’t work well in a certain situation.

Does Not Work

New post -> Save as Draft -> Publish using new button in Adomin Post Lists

Does Work

New post -> Publish -> Draft -> Publish using new button in Adomin Post Lists

Thank You all in advance for help or any other ways :D

Reference : Publish pending article from front end with a button? (this doesnt work)

Here’s functions.php code

function show_publish_button($post_id){
$pub_form = <<<EOF
        <form name="make_it_publish" method="post" action="">
                <input type="hidden" name="pid" id="pid" value="{$post_id}">
                <input type="hidden" name="mi_publish" id="mi_publish" value="mi_publish">
                <input type="submit" name="submit" id="submit" value="publish">
         </form>
EOF;
echo $pub_form;
}

function show_draft_button($post_id){
$pri_form = <<<EOF
        <form name="make_it_draft" method="post" action="">
                <input type="hidden" name="pid" id="pid" value="{$post_id}">
                <input type="hidden" name="mi_draft" id="mi_draft" value="mi_draft">
                <input type="submit" name="submit" id="submit" value="draft">
         </form>
EOF;
echo $pri_form;
}

function change_post_status($post_id,$status){

    $time = current_time('mysql');
    wp_update_post(
        array(
           'ID'                         =>  $post_id,
           'post_status'        =>  $status,
           'post_name'          =>  $post_id,
           'post_date_gmt'  =>  get_gmt_from_date( $time )
        )
    );
}

add_action( 'admin_init', function() {
    if (isset($_POST['mi_publish']) && $_POST['mi_publish'] == 'mi_publish'){
        if (isset($_POST['pid']) && !empty($_POST['pid'])){
            change_post_status((int)$_POST['pid'],'publish');
        }
    }
    if (isset($_POST['mi_draft']) && $_POST['mi_draft'] == 'mi_draft'){
        if (isset($_POST['pid']) && !empty($_POST['pid'])){
            change_post_status((int)$_POST['pid'],'draft');
        }
    }
});


function manage_posts_columns($columns) {
    $columns['publish'] = "publish";
    $columns['draft'] = "draft";
    return $columns;
}

function add_column($column_name, $post_id) {

    if($column_name == 'publish'){
        $pub = "publish";
    }
    if($column_name == 'draft'){
        $pri = "draft";
    }


    if ( isset($pub) && $pub ){
        show_publish_button($post_id);
    } else if ( isset($pri) && $pri ){
        show_draft_button($post_id);
    } else {
        echo __('None');
    }
}
add_filter( 'manage_posts_columns', 'manage_posts_columns' );
add_action( 'manage_posts_custom_column', 'add_column', 10, 2 );

1 Answer 1

0

The reason is nest form tag.

Admin Post Lists is huge form and I added form tag. That's why it didn't work.

Simple way is using ajax and wp_update_post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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