It’s been a while since I’m tired of not being able to give a reason for rejecting ads and that the only rejection solution is the trash. So here is a code to add 2 custom rejection buttons, the first directly on your ad list and the 2nd in each ad next to the “published” button. This code allows you to give and send the reason for rejection by email and then transfer the ad as a draft so that the advertiser can modify it again.
function ajouter_bouton_refuser($actions, $post) {
if ($post->post_type === 'hp_listing' && $post->post_status !== 'rejected') {
$url = admin_url("admin.php?action=reject_listing&listing_id={$post->ID}");
$actions['reject'] = '<a href="' . esc_url($url) . '" style="color:red; font-weight:bold;">Refuser</a>';
}
return $actions;
}
add_filter('post_row_actions', 'ajouter_bouton_refuser', 10, 2);
function traiter_refus_annonce() {
if (isset($_GET['action']) && $_GET['action'] === 'reject_listing' && isset($_GET['listing_id'])) {
$listing_id = intval($_GET['listing_id']);
if (!current_user_can('edit_post', $listing_id)) {
wp_die(__('Vous n’avez pas la permission de faire cela.', 'hivepress'));
}
?>
<script type="text/javascript">
var raison = prompt("Entrez la raison du refus de cette annonce :");
if (raison !== null) {
window.location.href = "<?php echo admin_url('admin.php?action=confirm_reject_listing&listing_id=' . $listing_id); ?>&raison=" + encodeURIComponent(raison);
} else {
window.location.href = "<?php echo admin_url('edit.php?post_type=hp_listing'); ?>";
}
</script>
<?php
exit;
}
}
add_action('admin_init', 'traiter_refus_annonce');
function confirmer_refus_annonce() {
if (isset($_GET['action']) && $_GET['action'] === 'confirm_reject_listing' && isset($_GET['listing_id']) && isset($_GET['raison'])) {
$listing_id = intval($_GET['listing_id']);
$raison = sanitize_text_field($_GET['raison']);
envoyer_email_refus($listing_id, $raison);
wp_update_post(array(
'ID' => $listing_id,
'post_status' => 'draft', // Déplace l'annonce dans les brouillons
));
wp_redirect(admin_url('edit.php?post_type=hp_listing'));
exit;
}
}
add_action('admin_init', 'confirmer_refus_annonce');
function envoyer_email_refus($listing_id, $raison) {
$annonce = get_post($listing_id);
$auteur_id = $annonce->post_author;
$email_auteur = get_the_author_meta('user_email', $auteur_id);
$sujet = "\"{$annonce->post_title}\" a été refusée";
$message = "Bonjour,\n\nVotre annonce \"{$annonce->post_title}\" a été refusée pour la raison suivante :\n\n\"{$raison}\"\n\n";
$message .= "\nVous pouvez modifier l'annonce directement ici : LINK\n\n";
$message .= "Si vous avez des questions, vous pouvez nous contacter.\n\nCordialement,\nL'équipe NAME";
wp_mail($email_auteur, $sujet, $message);
}
function afficher_annonces_rejetees($query) {
if (is_admin() && $query->is_main_query() && $query->get('post_type') === 'hp_listing') {
$post_status = $query->get('post_status');
if (!$post_status) {
$query->set('post_status', ['publish', 'pending', 'draft', 'rejected']);
}
}
}
add_action('pre_get_posts', 'afficher_annonces_rejetees');
function ajouter_bouton_refus_meta_box() {
global $post;
if ($post->post_type === 'hp_listing') {
?>
<style>
#publishing-action {
display: flex;
align-items: center;
gap: 10px;
}
#refuser-annonce {
background-color: red;
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
border-radius: 3px;
font-size: 13px;
}
</style>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function () {
var publierBtn = document.getElementById("publish");
if (publierBtn) {
var boutonRefus = document.createElement("button");
boutonRefus.id = "refuser-annonce";
boutonRefus.innerText = "Refuser";
publierBtn.parentNode.insertBefore(boutonRefus, publierBtn);
boutonRefus.addEventListener("click", function (event) {
event.preventDefault();
var raison = prompt("Veuillez entrer la raison du refus :");
if (raison) {
window.location.href = "<?php echo admin_url('edit.php?post_type=hp_listing&action=confirm_reject_listing&listing_id=' . $post->ID . '&raison='); ?>" + encodeURIComponent(raison);
}
});
}
});
</script>
<?php
}
}
add_action('post_submitbox_start', 'ajouter_bouton_refus_meta_box');