Sto cercando di creare un messaggio personalizzato invece del messaggio predefinito quando salvo un post, qualcuno sa come farlo!
Sto cercando di creare un messaggio personalizzato invece del messaggio predefinito quando salvo un post, qualcuno sa come farlo!
Risposte:
http://codex.wordpress.org/Function_Reference/register_post_type esempio:
//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;
}
La 'message'parte $_GETdell'array è responsabile del mantenimento del valore del messaggio effettivo come integer. Ciò significa che tutto ciò che viene passato lì verrà impostato come messaggio effettivo. I messaggi stessi sono archiviati in un array globale nei modelli di interfaccia utente dell'amministratore. Ha un nome $messagese ha tre chiavi per impostazione predefinita:
pagepostattachmentI messaggi sono memorizzati come sotto-array $messagesdell'array principale .
Alcune cose da tenere a mente (WP core v4.0.1):
0è in uso.attachmentmessaggi al momento sono un hack e hanno solo la stringa 'Media attachment updated.'su ogni tasto.Usa il post_updated_messagesfiltro:
add_filter( 'post_updated_messages', function( $messages )
{
$messages['post'][2] = 'My awesome custom field just updated. Congratulations!';
return $messages;
} );
Guarda ~/wp-admin/edit-form-advanced.phpquale messaggio viene utilizzato per cosa.
Il fallback, se non viene utilizzato alcun tipo di post, è l'
postarray di messaggi del tipo di post.
È possibile aggiungere in sicurezza il proprio set di messaggi tramite una richiamata sul filtro personalizzato. Assicurati solo di utilizzare il nome del tuo post personalizzato come chiave per l'array dei messaggi:
add_filter( 'post_updated_messages', function( $messages )
{
$messages['my_custom_post_type'][2] = 'Go, buy some milk!';
return $messages;
} );
Il callback stesso probabilmente è meglio agganciato
do_action( "load-{$pagenow}" )
Ho pensato che questo potesse aiutare alcuni.
Dopo aver esaminato in lungo e in largo vari siti, ho potuto visualizzare solo un messaggio personalizzato con l'aiuto di questo.
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');