Last active
August 2, 2021 19:47
-
-
Save yiedpozi/09aeb03ce96ca740b84e4d7dd5daf0d1 to your computer and use it in GitHub Desktop.
Register custom post status and display it in the post status list
This file contains hidden or 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 post status | |
add_action( 'init', function() { | |
register_post_status( 'mysite-accepted', array( | |
'label' => _x( 'Accepted', 'post status label', 'mysite' ), | |
'label_count' => _n_noop( 'Accepted <span class="count">(%s)</span>', 'Accepted <span class="count">(%s)</span>', 'mysite' ), | |
) ); | |
register_post_status( 'mysite-rejected', array( | |
'label' => _x( 'Rejected', 'post status label', 'mysite' ), | |
'label_count' => _n_noop( 'Rejected <span class="count">(%s)</span>', 'Rejected <span class="count">(%s)</span>', 'mysite' ), | |
) ); | |
} ); | |
// List of registered post status | |
function mysite_get_post_statuses() { | |
return array( | |
'mysite-accepted' => __( 'Accepted', 'mysite' ), | |
'mysite-rejected' => __( 'Rejected', 'mysite' ), | |
); | |
} | |
// Default post status | |
function mysite_get_default_post_status() { | |
return 'mysite-accepted'; | |
} | |
// Append registered post status in the dropdown in post editor page | |
add_action( 'post_submitbox_misc_actions', function() { | |
global $post; | |
if ( $post->post_type !== 'custom_post_type' ) { | |
return; | |
} | |
$statuses = mysite_get_post_statuses(); | |
$post_status = $post->post_status; | |
if ( !in_array( $post_status, array_keys( $statuses ) ) ) { | |
$post_status = mysite_get_default_post_status(); | |
} | |
$display = null; | |
$append = array(); | |
// Display the status for the post | |
$display = " | |
$( '#post-status-display' ).text( '" . esc_js( $statuses[ $post_status ] ) . "' ); | |
$( 'select[name=\"post_status\"]' ).val( '" . esc_js( $post_status ) . "' ); | |
$( 'input[name=\"hidden_post_status\"]' ).val( '" . esc_js( $post_status ) . "' ); | |
"; | |
// Append registered status | |
foreach ( $statuses as $key => $label ) { | |
$selected = null; | |
if ( $key === $post_status ) { | |
$selected = ' selected'; | |
} | |
$append[] = '<option value=\"' . esc_js( $key ) . '\"' . $selected . '>' . esc_js( $label ) . '</option>'; | |
} | |
echo " | |
<script type=\"text/javascript\"> | |
jQuery( document ).ready( function( $ ) { | |
" . $display . " | |
$( 'select[name=\"post_status\"]' ).html( '" . implode( '', $append ) . "' ); | |
$( '#publishing-action input[type=\"submit\"]' ).attr( { 'name': 'save', id: 'save' } ).val( 'Save' ); | |
} ); | |
</script> | |
"; | |
} ); | |
// Append registered post status in the dropdown in quick edit | |
add_action( 'admin_footer-edit.php', function() { | |
global $post; | |
if ( $post->post_type !== 'custom_post_type' ) { | |
return; | |
} | |
$statuses = mysite_get_post_statuses(); | |
$append = array(); | |
// Append registered status | |
foreach ( $statuses as $key => $label ) { | |
$selected = null; | |
if ( $key === $post->post_status ) { | |
$selected = ' selected'; | |
} | |
$append[] = '<option value=\"' . esc_js( $key ) . '\"' . $selected . '>' . esc_js( $label ) . '</option>'; | |
} | |
echo " | |
<script type=\"text/javascript\"> | |
jQuery( document ).ready( function( $ ) { | |
$( 'select[name=\"_status\"]' ).html( '" . implode( '', $append ) . "' ); | |
} ); | |
</script> | |
"; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment