/**
* PostStatusExtender
*
* @author Hyyan Abo Fakher<[email protected].com>
*/
class PostStatusExtender
{
/**
* Extend
*
* Extend the current status list for the given post type
*
* @global \WP_POST $post
*
* @param string $postType the post type name , ex: product
* @param array $states array of states where key is the id(state id) and value
* is the state array
*/
public static function extend($postType, $states)
{
foreach ($states as $id => $state) {
register_post_status($id, $state);
}
add_action('admin_footer-post.php', function() use($postType, $states) {
global $post;
if (!$post || $post->post_type !== $postType) {
return false;
}
foreach ($states as $id => $state) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. ' $("select#post_status").append("<option value=\"%s\" %s>%s</option>");'
. ' $("a.save-post-status").on("click",function(e){'
. ' e.preventDefault();'
. ' var value = $("select#post_status").val();'
. ' $("select#post_status").value = value;'
. ' $("select#post_status option").removeAttr("selected", true);'
. ' $("select#post_status option[value=\'"+value+"\']").attr("selected", true)'
. ' });'
. '});'
. '</script>'
, $id
, $post->post_status !== $id ? '' : 'selected=\"selected\"'
, $state['label']
);
if ($post->post_status === $id) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. ' $(".misc-pub-section #post-status-display").text("%s");'
. '});'
. '</script>'
, $state['label']
);
}
}
});
add_action('admin_footer-edit.php', function() use($states, $postType) {
global $post;
if (!$post || $post->post_type !== $postType) {
return false;
}
foreach ($states as $id => $state) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. " $('select[name=\"_status\"]').append('<option value=\"%s\">%s</option>');"
. '});'
. '</script>'
, $id
, $state['label']
);
}
});
add_filter('display_post_states', function($states, $post) use($states, $postType) {
foreach ($states as $id => $state) {
if ($post->post_type == $postType && $post->post_status === $id) {
return array($state['label']);
} else {
if (array_key_exists($id, $states)) {
unset($states[$id]);
}
}
}
return $states;
}, 10, 2);
}
}
Et ici comment utiliser il
add_action('init', function() {
PostStatusExtender::extend(self::NAME, array(
'sold' => array(
'label' => __('Sold', 'viasit'),
'public' => true,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Sold <span class="count">(%s)</span>', 'Sold <span class="count">(%s)</span>'),
)
));
});
Edit: Correction d'une faute dans le code.
Ces statuts ont des implications profondes sur la façon dont les messages sont traités et affichés, n'est-ce pas? Je ne pense pas que vous pouvez simplement ajouter un nouveau à une liste. Quels nouveaux statuts souhaiteriez-vous ajouter? –
Je souhaite uniquement ajouter de nouveaux statuts à mes types de publications personnalisés et à ceux qui gèrent déjà l'affichage de la publication via des requêtes personnalisées. Avec wordpress, vous pouvez effectuer une requête pour afficher les messages en fonction de leur statut. Donc, l'ajout de certains statuts tels que Vendu et Supprimé ne devrait pas être un problème majeur pour le système? – Brady
@Brady Je vois. Je ne sais pas si c'est facilement possible. Qu'en est-il de l'utilisation du système de marquage/catégories pour cela? –