Created
March 8, 2023 01:48
-
-
Save codearachnid/3d8efcc25b0057d433baa541282ced5e to your computer and use it in GitHub Desktop.
WordPress Custom Post Status OOP
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 | |
// sourced from https://www.ibenic.com/create-custom-wordpress-post-status/ and https://stackoverflow.com/a/49569592 | |
class WordPress_Custom_Post_Status { | |
/** | |
* Post Types for this status | |
* @var array | |
*/ | |
protected $post_type = []; | |
/** | |
* Status slug | |
* @var string | |
*/ | |
protected $slug = ''; | |
/** | |
* Enable the button | |
* 'true', 'publish' mixed enables publishing from this status | |
* 'update' string enables updating from this status | |
* 'false' boolean disabled/removes the button | |
* @var boolean | |
*/ | |
protected $enable_action= false; | |
/** | |
* Default definitons | |
* @var array | |
*/ | |
protected $defaults = [ | |
'label' => '', | |
'public' => null, | |
'protected' => null, | |
'private' => null, | |
'publicly_queryable' => null, | |
'exclude_from_search' => false, | |
'internal' => null, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => false, | |
'label_count' => '' | |
]; | |
/** | |
* Status settings | |
* @var array | |
*/ | |
protected $settings = []; | |
// ... | |
/** | |
* Register the post status if everything that is required is set | |
*/ | |
public function __construct( $args = [] ) { | |
if( empty( $args ) ) { | |
return; | |
} | |
if( ! isset( $args['post_type'] ) || empty( $args['post_type'] ) ) { | |
return; | |
} | |
if( ! isset( $args['slug'] ) || $args['slug'] == '' ) { | |
return; | |
} | |
$this->post_type = $args['post_type']; | |
$this->slug = $args['slug']; | |
if( isset( $args['action'] ) && in_array( $args['action'], [ false, true, 'publish', 'update' ] ) ) { | |
$this->enable_action = $args['action']; | |
} | |
if( ! isset( $args['label'] ) || $args['label'] == '' ) { | |
$args['label'] = ucfirst( $args['slug'] ); | |
} | |
if( ! isset( $args['label_count'] ) || $args['label_count'] == '' ) { | |
$args['label_count'] = _n_noop( $args['label'] . ' <span class="count">(%s)</span>', $args['label'] . ' <span class="count">(%s)</span>'); | |
} | |
unset( $args['slug'] ); | |
unset( $args['post_type'] ); | |
unset( $args['action'] ); | |
$this->settings = wp_parse_args( $args, $this->defaults ); | |
add_action( 'init', [ $this, 'register_status' ] ); | |
add_action( 'admin_footer', [ $this, 'set_status' ] ); | |
} | |
/** | |
* Register Post Status using WordPress API | |
*/ | |
public function register_status() { | |
register_post_status( $this->slug, $this->settings ); | |
} | |
public function set_status() { | |
$set_status = apply_filters( 'ibenic_custom_post_status_' . $this->slug, true ); | |
if( ! $set_status ) { | |
return; | |
} | |
global $post; | |
if( ! in_array( $post->post_type, $this->post_type ) ) { | |
return; | |
} | |
$complete = ''; | |
$label = ''; | |
if( $post->post_status == $this->slug ) { | |
$complete = ' selected=\"selected\"'; | |
$label = '<span id=\"post-status-display\">' . $this->settings['label'] . '</span>'; | |
} | |
?> | |
<script> | |
( function($){ | |
$(document).ready(function(){ | |
$('select#post_status').append( "<option value='<?php echo $this->slug; ?>' <?php echo $complete; ?>><?php echo $this->settings['label']; ?></option>"); | |
$('.misc-pub-section label').append( "<?php echo $label; ?>"); | |
<?php if( $complete != '' ) { | |
// If the post has this status check the preferred action | |
// If true or 'publish', we leave it as default | |
if( ! $this->enable_action ) { | |
echo '$("#publish").remove();'; | |
} elseif( $this->enable_action === 'update' ) { | |
echo '$("#publish").val("Update");$("#publish").attr("name","save");$("#original_publish").val("Update");'; | |
} | |
} ?> | |
}); | |
})( jQuery ); | |
</script> | |
<?php | |
} | |
} | |
/**** EXAMPLE *****/ | |
new WordPress_Custom_Post_Status( [ | |
'post_type' => [ 'post', 'page' ], | |
'slug' => 'custom_status', | |
'label' => _x( 'Custom Status', 'ibenic' ), | |
'action' => 'update', | |
'label_count' => _n_noop( 'Custom <span class="count">(%s)</span>', 'Custom <span class="count">(%s)</span>' ), | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment