Skip to content

Instantly share code, notes, and snippets.

@pixelbart
Last active May 7, 2019 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelbart/607fbbc332943273575ce38e5c895360 to your computer and use it in GitHub Desktop.
Save pixelbart/607fbbc332943273575ce38e5c895360 to your computer and use it in GitHub Desktop.
Class for custom post status in WordPress
<?php
/**
* Class for custom post statuses
*/
class Custom_Status
{
protected $status, $labels, $post_type;
/**
* Set variables
* @param string $status
* @param array $labels
* @param string $post_type
*/
public function __construct($status, $labels, $post_type) {
$this->status = $status;
$this->labels = $labels;
$this->post_type = $post_type;
}
/**
* Install custom status
*/
public function install() {
add_action( 'init', [$this, 'registerPostStatus'], 1 );
add_action( 'admin_footer-edit.php', [$this, 'registerOverviewStatusSelect'], PHP_INT_MAX );
add_filter( 'display_post_states', [$this,'displayOverviewStatus'] );
add_action( 'admin_footer', [$this, 'displayEditStatus'], PHP_INT_MAX );
}
/**
* Register custom post states
*/
public function registerPostStatus() {
$args = [
'public' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'exclude_from_search' => false,
'label' => $this->labels['name'],
'label_count' => $this->labels['counter'],
];
register_post_status( $this->status, $args );
}
/**
* Register custom status into the post edit overview
*/
public function registerOverviewStatusSelect() {
?>
<script id="status-<?=$this->status?>" type="text/javascript">
(function($) {
$("select[name=\"_status\"]").append( "<option value=\"<?=$this->status?>\"><?=$this->labels['name']?></option>" );
})(jQuery);
</script><?php
}
/**
* Display custom states into the post edit overview
* @param array $wp_states default wordpress states
*/
public function displayOverviewStatus($wp_statuses) {
if( $this->status !== get_query_var('post_status') ) {
if( $this->status == $post->post_status ) {
return [ $this->labels['name'] ];
}
}
return $wp_statuses;
}
/**
* Display custom states into post edit screen (single)
*/
public function displayEditStatus() {
global $post;
if( $this->post_type !== $post->post_type )
return;
$html = '<script>(function($) {';
$html .= '$(function() { $("#post-status-display").text("'.$this->labels['name'].'"); });';
if( $this->status == $post->post_status) {
$html .= '$("#post_status").append("<option value=\"'.$this->status.'\" selected>'.$this->labels['name'].'</option>");';
$html .= '$(".misc-pub-section.misc-pub-post-status").find("label").append("<span id=\"post-status-display\"> '.$this->labels['name'].'</span>");';
} else {
$html .= '$("#post_status").append("<option value=\"'.$this->status.'\">'.$this->labels['name'].'</option>");';
}
$html .= '})(jQuery)</script>';
echo $html;
}
}
<?php
include_once('class-custom-status.php');
$post_status = 'closed';
$post_type = 'my_cpt';
$labels = [
'name' => esc_html_x( 'Closed', 'Status General Name', 'textdomain' ),
'counter' => _n_noop( 'Closed (%s)', 'Closed (%s)', 'textdomain' ),
];
$status = new Custom_Status($post_status, $labels, $post_type);
$status->install();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment