Last active
April 13, 2020 20:36
-
-
Save hearvox/57fb6ea4282752373edca2c74ee8fb59 to your computer and use it in GitHub Desktop.
WordPress custom post status function (none for Block editor yet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/******************************* | |
CUSTOM POST STATUS | |
******************************/ | |
/** | |
* Register custom post status. | |
* | |
* @return object | |
*/ | |
function my_custom_post_status(){ | |
register_post_status( 'archive', array( | |
'label' => _x( 'Archive', 'textdomain' ), | |
'public' => true, | |
'show_in_admin_all_list' => false, | |
'show_in_admin_status_list' => true, | |
'label_count' => _n_noop( 'Archive <span class="count">(%s)</span>', 'Archive <span class="count">(%s)</span>' ) | |
) ); | |
} | |
add_action( 'init', 'my_custom_post_status' ); | |
/** | |
* Add post status to Edit Post's Status select options in Publish box (Classic-only). | |
* | |
* WordPress does not do these (yet) by defualt: | |
* @see https://wordpress.org/support/article/post-status/#custom-status | |
* | |
* @return void | |
*/ | |
function my_append_post_status_list(){ | |
global $post; | |
$complete = ''; | |
$label = ''; | |
if( $post->post_type == 'post' ) { | |
if( $post->post_status == 'archive' ) { | |
$complete = ' selected="selected"'; | |
$label = '<span id="post-status-display"> Archived</span>'; | |
} | |
echo ' | |
<script> | |
jQuery(document).ready(function($){ | |
$("select#post_status").append("<option value="archive" '. $complete . '>Archive</option>"); | |
$(".misc-pub-section label").append("'.$label.'"); | |
}); | |
</script> | |
'; | |
} | |
} | |
add_action( 'admin_footer-post.php', 'my_append_post_status_list' ); | |
add_action( 'admin_footer-post-new.php', 'my_append_post_status_list' ); | |
/** | |
* Add post status to Bulk/Quick Edit's Status select in All Posts table. | |
* | |
* WordPress does not do these (yet) by defualt: | |
* @see https://wordpress.org/support/article/post-status/#custom-status | |
* | |
* @return void | |
*/ | |
function my_append_post_status_bulk() { // ultra-simple example | |
echo "<script> | |
jQuery(document).ready( function() { | |
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"archvive\">Archve</option>' ); | |
}); | |
</script>"; | |
} | |
add_action('admin_footer-edit.php','my_append_post_status_bulk'); | |
/** | |
* Add post-status name next to post in All Posts table. | |
* | |
* @see https://developer.wordpress.org/reference/hooks/display_post_states/ | |
* | |
* @return array $states Custom post-status name or current post stati. | |
*/ | |
function my_display_post_status( $states ) { | |
global $post; | |
$arg = get_query_var( 'post_status' ); | |
if( $arg != 'archive' ) { | |
if( $post->post_status == 'archive' ) { | |
return array( 'Archive' ); | |
} | |
} | |
return $states; | |
} | |
add_filter( 'display_post_states', 'my_display_post_status' ); | |
/* | |
Reference: | |
https://wordpress.org/support/article/post-status/ | |
https://developer.wordpress.org/reference/functions/register_post_status/ | |
Based on: | |
https://web.archive.org/web/20181026023402/http://jamescollings.co.uk/blog/wordpress-create-custom-post-status/ | |
List all statuses: get_post_stati() -- get_post_status() lists only WP native. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment