X

Créer des messages de statut de publication personnalisés dans l'administration

Extraits par IsItWP

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.

Commentaires   Laisser une réponse

  1. Edson Santoro June 6, 2012 at 3:17 am

    SUPER!!!!!!!!!!!!! Ça m'a sauvé la vie !

  2. salut, y a-t-il une chance de cacher cette fonctionnalité aux non-administrateurs dans wordpress ?

    1.  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,

  3. en bas, pas en bas. 🙂

    1. WHooops 🙂 merci

  4. 29 Wordpress Tweaks to Improve Posts and Pages October 18, 2011 at 9:03 am

    [...] [Source : WPSNIPP] [...]

  5. Wordpress Update: Create custom post status mesasges in admin – 400+ Wordpress code snippets for your blog July 19, 2011 at 9:01 am

    […] 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 […]

  6. 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 ». 

    1. 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.

      1. Gabriel Merovingi July 16, 2011 at 1:35 am

        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”] );

        }

        1. 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.

        2. Alexander Janus October 19, 2011 at 6:41 pm

          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”] );

          }

          1. hookedonweb_usa June 27, 2013 at 12:33 am

            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

          2. 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.

  7. How to create custom status icons for custom post | WpCode.net June 10, 2011 at 11:37 pm

    […] 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 […]

Ajouter un commentaire

Nous sommes ravis que vous ayez choisi de laisser un commentaire. Veuillez garder à l'esprit que tous les commentaires sont modérés conformément à notre politique de confidentialité, et tous les liens sont nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Ayons une conversation personnelle et significative.

Liste de contrôle de lancement WordPress

La checklist ultime pour lancer un WordPress

Nous avons compilé tous les éléments essentiels de la liste de contrôle pour le lancement de votre prochain site Web WordPress dans un ebook pratique.
Oui, envoyez-moi l'eBook gratuit !