X

Ajout d’une colonne pour les messages de l’administrateur avec wp_get_shortlink

Snippets by IsItWP

Vous cherchez un moyen d’ajouter une nouvelle colonne à l’écran des articles avec un champ de saisie contenant le lien court ? Bien qu’il existe probablement un plugin pour cela, nous avons créé un extrait de code rapide que vous pouvez utiliser pour ajouter une colonne d’articles avec wp_get_shortlink dans WordPress.

Instructions:

Tout ce que vous avez à faire est d’ajouter ce code dans le fichier functions.php de votre thème ou dans un plugin spécifique à votre site:

add_filter( 'manage_posts_columns', 'shortlink_add_column' );
add_action( 'manage_posts_custom_column', 'shortlink_add_value', 10, 2 );

function shortlink_add_column($cols) { 
        $cols['shortlink'] = __('Shortlink'); 
        return $cols;
}
function shortlink_add_value($column_name, $post_id) {
    if ( 'shortlink' == $column_name ) {
        echo '<input type="text" value="'.wp_get_shortlink($post_id).'" onclick="this.focus(); this.select();" />';
    }
}

add_filter( 'manage_edit-post_sortable_columns', 'shortlink_sortable_column' );
function shortlink_sortable_column( $columns ) {
	$columns['shortlink'] = 'Shortlink';
	return $columns;
}

Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez consulter notre guide sur la façon d’ajouter correctement des extraits de code dans WordPress, afin de ne pas casser accidentellement votre site.

Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : 9 meilleurs plugins WordPress pour le service d’assistance et comment configurer le suivi des téléchargements sur WordPress avec Google Analytics.

Commentaires   laisser une réponse

  1. I noticed this column works on posts but not on custom post types. Any ideas?

    1. Hi Clifford, you can do the following for example.

      manage_edit-${post_type}_columns

      For a more detailed overview the codex has a working sample.
      http://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column

      1. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      2. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      3. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      4. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      5. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      6. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      7. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      8. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      9. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      10. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      11. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      12. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      13. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      14. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      15. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      16. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      17. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      18. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      19. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      20. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      21. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      22. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      23. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      24. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      25. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      26. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      27. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      28. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      29. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      30. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      31. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      32. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      33. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      34. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      35. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      36. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

        1. let me take a look and post something for you.

  2. Avinash D'Souza août 14, 2011 à 6:20 pm

    Can this also support bit.ly links?

    1. As of right now it just uses the wordpress short url’s although I’m sure it could be setup to work with bitly as well. I take a look into it and see what I can come up with.

Ajouter un commentaire

Nous sommes heureux que vous ayez choisi de laisser un commentaire. N'oubliez pas que tous les commentaires sont modérés conformément à notre privacy policy, et que tous les liens sont en nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Engageons une conversation personnelle et constructive.

WordPress Launch Checklist

L'ultime liste de contrôle pour le lancement de WordPress

Nous avons rassemblé 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 le gratuit !