Last active
January 31, 2016 18:06
-
-
Save topdown/9036d462af465d38c29b to your computer and use it in GitHub Desktop.
Custom post status workaround
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 | |
/** | |
* Register Custom Status | |
* | |
*/ | |
function custom_archived_post_status() { | |
$args = array( | |
'label' => _x( 'Archived', 'Status General Name', 'text_domain' ), | |
'label_count' => _n_noop( 'Archived (%s)', 'Archived (%s)', 'text_domain' ), | |
'public' => true, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => true, | |
'exclude_from_search' => false, | |
); | |
register_post_status( 'archived', $args ); | |
} | |
add_action( 'init', 'custom_archived_post_status', 0 ); | |
/** | |
* Inject it into the edit forms | |
* | |
*/ | |
function custom_status_into_inline_edit() { // ultra-simple example | |
echo "<script> | |
jQuery(document).ready( function() { | |
// quick edit | |
jQuery( 'select[name="_status"]' ).append( '<option value="archived">Archived</option>' ); | |
// post page | |
jQuery( 'select[name="post_status"]' ).append( '<option value="archived">Archived</option>' ); | |
}); | |
</script>"; | |
} | |
add_action( 'admin_footer-edit.php', 'custom_status_into_inline_edit' ); | |
add_action( 'admin_footer-post.php', 'custom_status_into_inline_edit' ); | |
/** | |
* Display the status in the dropdown | |
*/ | |
function custom_display_status_label( $statuses ) { | |
global $post; // we need it to check current post status | |
if ( get_query_var( 'post_status' ) != 'archived' ) { // not for pages with all posts of this status | |
if ( $post->post_status == 'archived' ) { | |
return array( 'Archived' ); // returning our status label | |
} | |
} | |
return $statuses; // returning the array with default statuses | |
} | |
add_filter( 'display_post_states', 'custom_display_status_label' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment