Empêcher la publication du message avant de définir une image sélectionnée?
2 réponses
- votes
-
- 2013-08-30
Le
has_post_thumbnail()
fonctionnepourmoi,dans les versions WP 3.4.1et autresplus récemment. Mais dans cette logique,car le WPpubliera lepostmême avecexit
ouwp_die()
ou quoi que ce soitpourterminer le script PHP. Pour éviter que lemessage reste avec le statutpublié,vous devrez lemettre àjour avant de leterminer. Regardez le code ci-dessous:add_action('save_post', 'prevent_post_publishing', -1); function prevent_post_publishing($post_id) { $post = get_post($post_id); // You also add a post type verification here, // like $post->post_type == 'your_custom_post_type' if($post->post_status == 'publish' && !has_post_thumbnail($post_id)) { $post->post_status = 'draft'; wp_update_post($post); $message = '<p>Please, add a thumbnail!</p>' . '<p><a href="' . admin_url('post.php?post=' . $post_id . '&action=edit') . '">Go back and edit the post</a></p>'; wp_die($message, 'Error - Missing thumbnail!'); } }
The
has_post_thumbnail()
works for me, in WP versions 3.4.1 and other most recently. But in this logic, because the WP will publish the post even withexit
orwp_die()
or anything to terminate the PHP script. For prevent that the post stay with published status, you will need to update the post before terminate. Look the code below:add_action('save_post', 'prevent_post_publishing', -1); function prevent_post_publishing($post_id) { $post = get_post($post_id); // You also add a post type verification here, // like $post->post_type == 'your_custom_post_type' if($post->post_status == 'publish' && !has_post_thumbnail($post_id)) { $post->post_status = 'draft'; wp_update_post($post); $message = '<p>Please, add a thumbnail!</p>' . '<p><a href="' . admin_url('post.php?post=' . $post_id . '&action=edit') . '">Go back and edit the post</a></p>'; wp_die($message, 'Error - Missing thumbnail!'); } }
-
- 2011-05-04
<?php // Something like that should help, but you'll have to play with it to get it working: // inside your functions.php file function wpse16372_prevent_publish() { if ( ! is_admin() ) return; // This should be ok, but should be tested: $post_id = $GLOBALS['post']->ID; echo '<pre>Test for post ID: '; print_r( $post_id ); echo '</pre>';// the actual test // has_post_thumbnail() doesn't work/exist on/for admin screens (see your error msg). You need to find another way to test if the post has a thumbnail. Maybe some Javascript? //if ( ! has_post_thumbnail( $post_id ); if ( ! has_post_thumbnail( $post_id ) ) { ?> <!-- // <script language="javascript" type="text/javascript"> alert( 'you have to use a featured image' ); </script> // --> <?php exit; // abort } } add_action( 'save_post', 'wpse16372_prevent_publish', 100 ); ?>
<?php // Something like that should help, but you'll have to play with it to get it working: // inside your functions.php file function wpse16372_prevent_publish() { if ( ! is_admin() ) return; // This should be ok, but should be tested: $post_id = $GLOBALS['post']->ID; echo '<pre>Test for post ID: '; print_r( $post_id ); echo '</pre>';// the actual test // has_post_thumbnail() doesn't work/exist on/for admin screens (see your error msg). You need to find another way to test if the post has a thumbnail. Maybe some Javascript? //if ( ! has_post_thumbnail( $post_id ); if ( ! has_post_thumbnail( $post_id ) ) { ?> <!-- // <script language="javascript" type="text/javascript"> alert( 'you have to use a featured image' ); </script> // --> <?php exit; // abort } } add_action( 'save_post', 'wpse16372_prevent_publish', 100 ); ?>
-
Maisil sepeut que le `has_post_thumbnail`ne fonctionnepas s'iln'en apas déjà un.Je suppose que `save_post`ne se déclenchepas sur le hook`publish_post`.But it could be that the `has_post_thumbnail` won't work if it hasn't already got one. I asume that `save_post` doesn't get fired on `publish_post` hook.
- 0
- 2011-05-04
- kaiser
-
C'est uneidée assezintéressante.Pourrait-il êtremodifiépour simplement "Alerter" ou abandonner oune paspublier lemessage?J'aimerais que quelque chose arrive si le client oublie,maisje ne veuxpasnécessairementempêcher quoi que ce soit d'être sauvé.That is a pretty neat idea. Could it be modified to simply "Alert" vs. aborting or not-publishing the post? I'd like something to come up if the client forgets, but don't necessarily want to stop anything from being saved.
- 0
- 2011-05-05
- RodeoRamsey
-
Oh,il alerte déjàpuis abandonne (au cas oùjsne seraitpas activé).Oh, it already alerts and then aborts (in case js is not activated).
- 0
- 2011-05-05
- kaiser
-
Mercibeaucoup Kaiserpour votre aide,mais chaquefois quej'essaie de coller le codeet deme connecter aupanneau d'administration,cela donne cetteerreur Erreur d'analyse:erreur de syntaxe pour la ligne if (! has_post_thumbnail ($ GLOBALS ['post'] -> ID); Pouvez-vousm'aider?Thanks a lot Kaiser for your help, but whenever I try to paste the code and log in to admin panel it gives this error Parse error: syntax error for the line if ( ! has_post_thumbnail( $GLOBALS['post']->ID ); Can you help me ?
- 0
- 2011-05-05
- BIALY
-
voirmise àjour/édition de A. Btw:j'ai déjàmentionné dansmon commentaire,que "il sepourrait que` has_post_thumbnail`ne fonctionnepas ".Vous devrez rechercher une alternative oupeut-êtreintercepter sur `$ _POST`et vérifier ce que vous y êtes.Ensuite,décidez s'ilfaut alerteret abandonner ounon.Recherchez les crochets.Je suis désolé,maisje nepeuxpas êtreplus utile.see update/edit of A. Btw: I already mentioned in my comment, that "it could be that `has_post_thumbnail` won't work". You will have to search for an alternative or maybe intercept on `$_POST` and check what you got there. Then decide whether to alert & abort or not. Search for the hooks. I'm sorry, but i can't be of more help.
- 0
- 2011-05-05
- kaiser
Comme letitre l'indique,je veux unplugin/unefonctionpourempêcher/informer l'utilisateur lorsqu'ilessaie depublier lemessage sans définir l'image sélectionnée.
TOUTE AIDE???