Created
November 9, 2015 14:36
-
-
Save mafsdisseny/a542f4dcf2c9f2ec5b97 to your computer and use it in GitHub Desktop.
Creation of a custom post status for WordPress
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 --------------------------- | |
// ---------------------------------------------------------------------- | |
// "premsa" es el post status | |
// "publicacio" el cpt asociado. | |
// Create a custom post status | |
function mafs_new_archive_post_status() { | |
register_post_status( 'premsa', array( | |
'label' => _x( 'En premsa', 'publicacio' ), | |
'public' => true, | |
'exclude_from_search' => true, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => true, | |
'label_count' => _n_noop( 'En premsa <span class="count">(%s)</span>', 'En premsa <span class="count">(%s)</span>' ), | |
) ); | |
} | |
add_action( 'init', 'mafs_new_archive_post_status' ); | |
// Adding custom post status to WordPress status dropdown | |
function mafs_add_to_post_status_dropdown() { | |
global $post; | |
$complete = ''; | |
$label = ''; | |
if($post->post_type == 'publicacio'){ | |
if($post->post_status == 'premsa'){ | |
$complete = ' selected="selected"'; | |
$label = '<span id=\"post-status-display\"> En premsa</span>'; | |
} | |
?> | |
<script> | |
jQuery(document).ready(function($){ | |
$("select#post_status").append("<option value=\"premsa\" <?php selected('premsa', $post->post_status); ?>>En premsa</option>"); | |
$(".misc-pub-section label").append("<?php echo $label ?>"); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action( 'post_submitbox_misc_actions', 'mafs_add_to_post_status_dropdown'); | |
// Adding custom post status to post type index | |
function mafs_display_archive_state( $states ) { | |
global $post; | |
$arg = get_query_var( 'post_status' ); | |
if($arg != 'premsa'){ | |
if($post->post_status == 'premsa'){ | |
return array('En premsa'); | |
} | |
} | |
return $states; | |
} | |
add_filter( 'display_post_states', 'mafs_display_archive_state' ); | |
// Adding custom post status to quick edit screen | |
function mafs_add_status_into_quick_edit() { | |
global $post; | |
// no posts | |
if (!$post) return; | |
if ( 'publicacio' === $post->post_type ) { | |
echo "<script> | |
jQuery(document).ready( function() { | |
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"premsa\">En premsa</option>' ); | |
}); | |
</script>"; | |
} | |
} | |
add_action('admin_footer-edit.php','mafs_add_status_into_quick_edit'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment