Définir des messages personnalisés pour la mise à jour / l'enregistrement de la publication
-
-
Cen’estpasencore correct.Veuillezpublier votre solution comme réponse.It’s not yet okay. Please post your solution as an answer.
- 7
- 2011-05-21
- fuxia
-
3 réponses
- votes
-
- 2011-09-23
http://codex.wordpress.org/Function_Reference/register_post_type exemple:
//add filter to ensure the text Book, or book, is displayed when user updates a book add_filter('post_updated_messages', 'codex_book_updated_messages'); function codex_book_updated_messages( $messages ) { global $post, $post_ID; $messages['book'] = array( 0 => '', // Unused. Messages start at index 1. 1 => sprintf( __('Book updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Book updated.'), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('Book published. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ), 7 => __('Book saved.'), 8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'), // translators: Publish box date format, see http://php.net/date date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; }
http://codex.wordpress.org/Function_Reference/register_post_type example:
//add filter to ensure the text Book, or book, is displayed when user updates a book add_filter('post_updated_messages', 'codex_book_updated_messages'); function codex_book_updated_messages( $messages ) { global $post, $post_ID; $messages['book'] = array( 0 => '', // Unused. Messages start at index 1. 1 => sprintf( __('Book updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Book updated.'), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('Book published. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ), 7 => __('Book saved.'), 8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'), // translators: Publish box date format, see http://php.net/date date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; }
-
- 2014-11-28
Stockage desmessages
Lapartie
'message'
dutableau$_GET
est responsable dumaintien de la valeur réelle dumessage sousforme deinteger
. Cela signifie quetout ce qui yesttransmis sera défini commemessage réel. Lesmessageseux-mêmes sont stockés dans untableauglobal dans lesmodèles de l'interface utilisateur d'administration. Il s'appelle$messages
et possèdetrois cléspar défaut:page
post
attachment
Lesmessages sont stockés sousforme de sous-tableaux dutableauprincipal
$messages
.Remarques:
Quelquespoints àgarder à l'esprit (WP core v4.0.1):
- Le
0
n'estpas utilisé. - Lesmessages
attachment
sont actuellement unpiratageet ontjuste la chaîne'Media attachment updated.'
sur chaque clé. - Tous les sous-tableaux demessages ont une longueur de 10 clés
Comment ajouter desmessagespersonnalisés
Utilisez lefiltre
post_updated_messages
:add_filter( 'post_updated_messages', function( $messages ) { $messages['post'][2] = 'My awesome custom field just updated. Congratulations!'; return $messages; } );
Regardez
~/wp-admin/edit-form-advanced.php
pour quelmessageest utilisépour quoi.La solution de secours,si aucuntype depublicationn'est utilisé,est letableau demessages detype
post
.Types depublicationpersonnalisés
Vouspouvez ajouteren toute sécurité votrepropreensemble demessages via un rappel sur lefiltrepersonnalisé. Assurez-vous simplement d'utiliser lenom de votretype demessagepersonnalisé comme clépour letableau demessages:
add_filter( 'post_updated_messages', function( $messages ) { $messages['my_custom_post_type'][2] = 'Go, buy some milk!'; return $messages; } );
Le rappel lui-mêmeestprobablement lemieux accroché
do_action( "load-{$pagenow}" )
Message Storage
The
'message'
part of the$_GET
array is what is responsible for holding the actual message value asinteger
. This means that everything that gets passed in there, will be set as actual message. The messages itself are stored inside a global array in the Admin UI templates. It's named$messages
and has three keys per default:page
post
attachment
The messages are stored as sub-arrays of the main
$messages
array.Notes:
Some things to keep in mind (WP core v4.0.1):
- The
0
is not in use. - The
attachment
messages currently are a hack and just have the string'Media attachment updated.'
on every key. - All message sub-arrays are 10 keys long
How to add custom messages
Use the
post_updated_messages
filter:add_filter( 'post_updated_messages', function( $messages ) { $messages['post'][2] = 'My awesome custom field just updated. Congratulations!'; return $messages; } );
Look at
~/wp-admin/edit-form-advanced.php
for which message is used for what.The fallback, if no post type is used, is the
post
post type messages array.Custom Post Types
You can safely add your own set of messages via a callback on the bespoken filter. Just make sure that you use your custom post types name as key for the messages array:
add_filter( 'post_updated_messages', function( $messages ) { $messages['my_custom_post_type'][2] = 'Go, buy some milk!'; return $messages; } );
The callback itself probably is best hooked on
do_action( "load-{$pagenow}" )
-
- 2018-10-24
J'aipensé que celapourrait aider certains.
Après avoirparcouru la longueuret la largeur de divers sites,je n'aipu obtenir qu'unmessagepersonnalisé affiché avec l'aide de cela.
function frl_on_save_post($post_id, $post) {/* add warning filter when saving post */ if($post->post_type == 'post') //test for something real here add_filter('redirect_post_location', 'frl_custom_warning_filter'); } add_action('save_post', 'frl_on_save_post', 2, 2); function frl_custom_warning_filter($location) { /* filter redirect location to add warning parameter*/ $location = add_query_arg(array('warning'=>'my_warning'), $location); return $location; } function frl_warning_in_notice() { /* print warning message */ if(!isset($_REQUEST['warning']) || empty($_REQUEST['warning'])) return; $warnum = trim($_REQUEST['warning']); /* possible warnings codes and messages */ $warnings = array( 'my_warning' => __('This is my truly custom warning!', 'frl') ); if(!isset($warnings[$warnum])) return; echo '<div class="error message"><p><strong>'; echo $warnings[$warnum]; echo '</strong></p></div>'; } add_action('admin_notices', 'frl_warning_in_notice');
Thought this might help some.
After going through length and breadth of various sites I could only get a custom message displayed with help from this.
https://onextrapixel.com/10-tips-for-a-deeply-customised-wordpress-admin-area/
function frl_on_save_post($post_id, $post) {/* add warning filter when saving post */ if($post->post_type == 'post') //test for something real here add_filter('redirect_post_location', 'frl_custom_warning_filter'); } add_action('save_post', 'frl_on_save_post', 2, 2); function frl_custom_warning_filter($location) { /* filter redirect location to add warning parameter*/ $location = add_query_arg(array('warning'=>'my_warning'), $location); return $location; } function frl_warning_in_notice() { /* print warning message */ if(!isset($_REQUEST['warning']) || empty($_REQUEST['warning'])) return; $warnum = trim($_REQUEST['warning']); /* possible warnings codes and messages */ $warnings = array( 'my_warning' => __('This is my truly custom warning!', 'frl') ); if(!isset($warnings[$warnum])) return; echo '<div class="error message"><p><strong>'; echo $warnings[$warnum]; echo '</strong></p></div>'; } add_action('admin_notices', 'frl_warning_in_notice');
J'essaie de créer unmessagepersonnalisé au lieu dumessagepar défaut lorsquej'enregistre unmessage,est-ce que quelqu'un sait comment lefaire!