Je souhaite ajouter un simple message d'alerte Javascript lorsqu'un produit est ajouté par le client pour examen. Cependant, à l'aide des crochets de transition post-statut, une page blanche est créée après la fenêtre contextuelle. Y a-t-il un crochet qui se déclenche après le rechargement de la page? Voici mon code actuel:
//JS alert on submit product
function notify_me_for_pending() {
$notice = __('Thank you for your submission. Your product will be available for purchase as soon as we have approved it!', 'pressive-child');
echo 'alert("'.$notice.'");';
}
add_action( 'draft_to_pending', 'notify_me_for_pending' );
add_action( 'auto-draft_to_pending', 'notify_me_for_pending' );
add_action('new_to_pending', 'notify_me_for_pending');
Notez que lorsque post est mise à jour, WordPress vous redirige de /wp-admin/post.php
à /wp-admin/post.php?post=41&action=edit
, ce qui signifie qu'il se charge deux fois. Ainsi, ce que vous ferez sur les crochets d’état de transition qui seront ignorés avant l’impression du contenu.
Solution:-
admin_head
Exemple:-
//Update post meta to display alert
function add_js_for_pending($post) {
update_post_meta($post->ID, 'trigger_notice', TRUE);
}
add_action( 'draft_to_pending', 'add_js_for_pending' );
add_action( 'auto-draft_to_pending', 'add_js_for_pending' );
add_action('new_to_pending', 'add_js_for_pending');
//JS alert on submit product
function notify_me_for_pending() {
global $post;
$current_screen = get_current_screen();
//Check if we need to display alert
if ($current_screen->base == 'post' && get_post_meta($post->ID, 'trigger_notice', TRUE)) {
$notice = __('Thank you for your submission. Your product will be available for purchase as soon as we have approved it!', 'pressive-child'); ?>
<script type="text/javascript">
<?php echo 'alert("'.$notice.'");'; ?>
</script><?php
delete_post_meta($post->ID, 'trigger_notice'); //Alert is done now remove it.
}
}
add_action('admin_head', 'notify_me_for_pending');