'Add new custom post status via register_post_status in WordPress

I want to add a custom post status to my blog post.

I have added below code in my functions.php file but I am not able to see that post status in quick edit, in post new page and post edit page. Any advice?

  function my_register_post_status() {
    $my_status_args = array(
        'label' => __('Activated', 'my'),
        'label_count' => __('Activated', 'my'),
        'exclude_from_search' => false,
        'public' => true,
        'publicly_queryable' => true,
        'show_in_admin_status_list' => true,
        'show_in_admin_all_list' => true,
    );
    register_post_status( 'status', $my_status_args );
}
add_action( 'init', 'my_register_post_status' );

Attached screenshots:

enter image description here

enter image description here



Solution 1:[1]

Here is code to add new custom status in Quick edit and in post new page and post edit page.

   function my_custom_status_creation(){
        register_post_status( 'approved', array(
            'label'                     => _x( 'Approved', 'post' ),
            'label_count'               => _n_noop( 'Approved <span class="count">(%s)</span>', 'Approved <span class="count">(%s)</span>'),
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true
        ));
    }
    add_action( 'init', 'my_custom_status_creation' );

    function my_custom_status_add_in_quick_edit() {
        echo "<script>
        jQuery(document).ready( function() {
            jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"approved\">Approved</option>' );      
        }); 
        </script>";
    }
    add_action('admin_footer-edit.php','my_custom_status_add_in_quick_edit');
    function my_custom_status_add_in_post_page() {
        echo "<script>
        jQuery(document).ready( function() {        
            jQuery( 'select[name=\"post_status\"]' ).append( '<option value=\"approved\">Approved</option>' );
        });
        </script>";
    }
    add_action('admin_footer-post.php', 'my_custom_status_add_in_post_page');
    add_action('admin_footer-post-new.php', 'my_custom_status_add_in_post_page');

Solution 2:[2]

You have to add some javascript to have the status as an option in the select. Try something like the below:

function append_post_status_list() {
        global $post;
        $label    = " Activate";
        $complete = "<option value='activated'>$label</option>";
        if ( $post->post_status == 'activated' ) {
            $label    = " Activated";
            $complete = "<option value='activated' selected='selected'>$label</option>";
        }

        ob_start();
        ?>
        <script>
            jQuery(document).ready(function ($) {
                var label = "<?= $label ?>";
                $("select#post_status").append("<?= $complete ?>");
                if (' Activated' == label){
                    $(".misc-pub-section #post-status-display").html(label);
                }
            });
        </script>
        <?php
        echo ob_get_clean();
}
add_action( 'admin_footer-post.php', 'append_post_status_list' );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jigar
Solution 2