Voulez-vous ajouter un message de statut personnalisé pour chaque article qu'un auteur crée ? Bien qu'il existe probablement un plugin pour cela, nous avons créé un extrait de code rapide que vous pouvez utiliser pour créer un message de statut d'article personnalisé dans l'administration de WordPress.
Instructions :
Tout ce que vous avez à faire est d'ajouter ce code au fichier functions.php de votre thème ou dans un plugin spécifique au site.
Seuls les utilisateurs ayant la capacité publish_posts peuvent modifier le statut, tandis que tout le monde voit quel est le statut (s'il est défini). Vous pouvez supprimer la notification de statut en sélectionnant aucune.
add_filter( 'display_post_states', 'custom_post_state' );
function custom_post_state( $states ) {
global $post;
$show_custom_state = get_post_meta( $post->ID, '_status' );
// We are using "None" as a way to disable this feature for the current post.
if ( $show_custom_state && $show_custom_state[0] != 'None' ) $states[] = '<span class="custom_state ' . strtolower( $show_custom_state[0] ) . '">' . $show_custom_state[0] . '</span>';
return $states;
}
add_action( 'admin_head', 'status_css' );
function status_css()
{
echo '
<!-- Styling of Custom Statuses -->
<style type="text/css">
.custom{border-top:solid 1px #e5e5e5;}
.custom_state{
font-size:9px;
color:#666;
background:#e5e5e5;
padding:3px 6px 3px 6px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
border-radius:3px;
}
.spelling{background:#4BC8EB;color:#fff;}
.review{background:#CB4BEB;color:#fff;}
.errors{background:#FF0000;color:#fff;}
.source{background:#D7E01F;color:#333;}
.rejected{background:#000000;color:#fff;}
.final{background:#DE9414;color:#333;}
</style>';
}
// Only those with the capability should be able to change things.
if ( current_user_can( 'publish_posts' ) ) {
// Insert our "Custom Status" into the Post Publish Box
add_action( 'post_submitbox_misc_actions', 'custom_status_metabox' );
function custom_status_metabox() {
global $post;
$custom = get_post_custom( $post->ID );
$status = $custom["_status"][0];
$i = 0;
// Available Statuses
$custom_status = array( 'None', 'Spelling', 'Review', 'Errors', 'Source', 'Rejected', 'Final' );
echo '
<div class="misc-pub-section custom">Custom status:
<select name="ourstatus">';
for ( $i = 0; $i < count( $custom_status ); $i++ ) {
echo '<option value="' . $custom_status[$i] . '"';
if ( $status == $custom_status[$i] ) echo ' selected="selected"';
echo '>' . $custom_status[$i] . '</option>';
}
echo '</select></div>';
}
// Save
add_action( 'save_post', 'save_status' );
function save_status( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
update_post_meta( $post_id, "_status", $_POST["ourstatus"] );
}
}
Note : Si c'est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez consulter notre guide sur comment ajouter correctement des extraits de code dans WordPress, afin de ne pas casser accidentellement votre site.
Si vous avez aimé cet extrait de code, veuillez envisager de consulter nos autres articles sur le site comme : Comment créer de superbes formulaires d'opt-in WordPress et 10 meilleurs plugins de témoignages WordPress.
SUPER!!!!!!!!!!!!! Ça m'a sauvé la vie !
salut, y a-t-il une chance de cacher cette fonctionnalité aux non-administrateurs dans wordpress ?
Si vous voulez supprimer tous les aspects des utilisateurs réguliers, vous pourriez simplement faire quelque chose comme ceci avec les hooks,
if(is_admin()){
add_action( ‘post_submitbox_misc_actions’, ‘custom_status_metabox’ );
add_filter( ‘display_post_states’,’custom_post_state’);
}
Si vous voulez afficher le statut mais ne pas donner aux utilisateurs non administrateurs la possibilité de le modifier, vous pouvez faire la même chose mais juste avec la fonction qui affiche les options d'interface.
if(is_admin()){
add_action( ‘post_submitbox_misc_actions’, ‘custom_status_metabox’ );
}
Je n'ai pas testé cela mais cela devrait fonctionner,
en bas, pas en bas. 🙂
WHooops 🙂 merci
[...] [Source : WPSNIPP] [...]
[…] le thème WordPress vous permettra de créer des messages de statut de publication personnalisés. Il s'agit d'une version mise à jour de (Créer des messages de statut de publication personnalisés dans l'administration) ajoutant quelques fonctionnalités supplémentaires. Seuls les utilisateurs ayant la capacité « publish_posts » peuvent […]
Super addon 😉 Je me demandais, je l'ai essayé sur mon WP 3.1.3 et ça marche très bien. La seule chose que je voulais demander est comment puis-je supprimer le statut, après que l'article a été téléversé. J'ai essayé d'utiliser le « Personnalisé » ou le « —— » mais alors l'un de ceux-ci s'affiche également dans la liste « Publication ».
La façon la plus simple serait de définir display:none; sur l'un des statuts que vous créez, par exemple : .final{display:none;} cela créerait toujours le statut mais ne l'afficherait pas.
Merci pour ça ! J'ai ajusté votre code pour supprimer le statut s'il est défini sur « Aucun ». J'ai également inclus que seuls ceux qui peuvent « publish_posts » peuvent changer le statut, tandis que tout le monde voit les résultats enregistrés, bien sûr.
add_filter( ‘display_post_states’, ‘custom_post_state’ );
function custom_post_state( $states ) {
global $post;
$show_custom_state = get_post_meta( $post->ID, ‘_status’ );
if ( $show_custom_state ) $states[] = ” . $show_custom_state[0] . ”;
return $states;
}
add_action( ‘admin_head’, ‘status_css’ );
function status_css() {
echo ‘
.default{font-weight:bold;}
.custom{border-top:solid 1px #e5e5e5;}
.custom_state{font-size:9px; color:#666; background:#e5e5e5; padding:3px 6px 3px 6px; -moz-border-radius:3px; border-radius:3px;}
.spelling{background:#4BC8EB;color:#fff;}
.review{background:#CB4BEB;color:#fff;}
.errors{background:#FF0000;color:#fff;}
.source{background:#D7E01F;color:#333;}
.rejected{background:#000000;color:#fff;}
.final{background:#DE9414;color:#333;}
‘;
}
if ( current_user_can( ‘publish_posts’ ) ) {
add_action( ‘post_submitbox_misc_actions’, ‘custom_status_metabox’ );
add_action( ‘save_post’, ‘save_status’ );
function custom_status_metabox() {
global $post;
$custom = get_post_custom( $post->ID );
$status = $custom[“_status”][0];
$i = 0;
// Available Statuses
$custom_status = array( ‘None’, ‘Spelling’, ‘Review’, ‘Errors’, ‘Source’, ‘Rejected’, ‘Final’ );
echo ‘Statut personnalisé : ‘;
foreach ( $i = 0; $i < count( $custom_status ); $i++ ) {
echo '’ . $custom_status[$i] . ”;
}
echo ”;
}
function save_status( $post_id ) {
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) return $post_id;
// Si le statut est défini sur « Aucun », nous voulons supprimer ce paramètre.
if ( $_POST[“ourstatus”] == ‘None’ ) delete_post_meta( $post_id, “_status”, $_POST[“ourstatus”] );
else update_post_meta( $post_id, “_status”, $_POST[“ourstatus”] );
}
Super Gabriel, belle mise à jour, tu devrais me contacter via http://wpsnipp.com/contact/So je pourrai publier la mise à jour en t'attribuant la paternité de l'article.
J'ai apporté une légère modification au code, le vôtre contenait des erreurs de syntaxe et vous avez utilisé « foreach » au lieu de « for »
add_filter( ‘display_post_states’, ‘custom_post_state’ );
function custom_post_state( $states ) {
global $post;
$show_custom_state = get_post_meta( $post->ID, ‘_status’ );
if ( $show_custom_state ) $states[] = ” . $show_custom_state[0] . ”;
return $states;
}
add_action( ‘admin_head’, ‘status_css’ );
function status_css() {
echo ‘
.default{font-weight:bold;}
.custom{border-top:solid 1px #e5e5e5;}
.custom_state{font-size:9px; color:#666; background:#e5e5e5; padding:3px 6px 3px 6px; -moz-border-radius:3px; border-radius:3px;}
.spelling{background:#4BC8EB;color:#fff;}
.review{background:#CB4BEB;color:#fff;}
.errors{background:#FF0000;color:#fff;}
.source{background:#D7E01F;color:#333;}
.rejected{background:#000000;color:#fff;}
.final{background:#DE9414;color:#333;}
‘;
}
if ( current_user_can( ‘publish_posts’ ) ) {
add_action( ‘post_submitbox_misc_actions’, ‘custom_status_metabox’ );
add_action( ‘save_post’, ‘save_status’ );
function custom_status_metabox() {
global $post;
$custom = get_post_custom( $post->ID );
$status = $custom[“_status”][0];
$i = 0;
// Available Statuses
$custom_status = array( ‘None’, ‘Spelling’, ‘Review’, ‘Errors’, ‘Source’, ‘Rejected’, ‘Final’ );
echo ‘Statut personnalisé : ‘;
for ( $i = 0; $i < count( $custom_status ); $i++ ) {
echo '’ . $custom_status[$i] . ”;
}
echo ”;
}
function save_status( $post_id ) {
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) return $post_id;
// Si le statut est défini sur « Aucun », nous voulons supprimer ce paramètre.
if ( $_POST[“ourstatus”] == ‘None’ ) delete_post_meta( $post_id, “_status”, $_POST[“ourstatus”] );
else update_post_meta( $post_id, “_status”, $_POST[“ourstatus”] );
}
if ( $_POST[“ourstatus”] == ‘None’ ) delete_post_meta( $post_id, “_status”, $_POST[“ourstatus”] );
génère une erreur : Indice non défini : notrestatut
Alors j'ai fait ceci :
if ( isset($_POST[“ourstatus”]) == ‘None’ ) delete_post_meta( $post_id, “_status”, $_POST[“ourstatus”] );
else update_post_meta( $post_id, “_status”, $_POST[“ourstatus”] );
génère la même erreur : Indice non défini : notrestatut
donc j'ai fait la même chose :
if ( isset($_POST[“ourstatus”]) == ‘None’ ) delete_post_meta( $post_id, “_status”, isset($_POST[“ourstatus”] ));
Mais je n'arrive pas à corriger cette dernière erreur :
Indice indéfini : ps_right_now dans . . . wp-content/plugins/post-status-menu-items/cms_post_status_menu.php
Salut Antonin,
C'est une réponse très tardive, mais je voulais vous faire savoir que je pense que la dernière erreur que vous avez mentionnée a été corrigée ce matin dans la version 1.3.3 de Post Status Menu Items.
[…] est un excellent extrait écrit par Kevin Chard et en appliquant cet extrait à votre function.php, vous pouvez créer une icône personnalisée pour les types de publication personnalisés ; mais […]