Approve wordpress comments via email

Asked

Viewed 67 times

0

I am developing a site in Wordpress and I need the approval of the comments in the posts is done through email. I looked for plugins and tutorials on the subject but found nothing.

The intention is that the customer receive the comment via email on his smarthphone and has 2 links, one to APPROVE and the other to REJECT and this operation is done without the need to login to the site, only by clicking on the link.

How can I do this?

1 answer

1

You can add hook in wp_insert_comment to be notified each time a new commentary is added to the system and send the email to the site administrator with the links "approve", "disapprove" and "spam".

To check if the current link is the "approve" comment, just add a hook in init and then check the link and update the comment status.

To get the comment by ID you can use the function get_comment() and to update the function wp_update_comment().

Example:

<?php

add_action('wp_insert_comment','comment_inserted');
function comment_inserted($comment_id, $comment_object) {
    // aqui você monta os links e envia o email para o admin (usando as informações em comment_object)
    // ex.: 
    // http://site.com?comment_id=123&approved=1
    // $approved = [0 = reprovar, 1 = aprovar, spam = marcar como spam]
}

add_action('init', '_update_comment');
function _update_comment() {
    if(!isset($_GET['comment_id'] && !isset($_GET['approved'])) {
        // nenhum dos parametros na URL significa que não é o link esperado
        return;
    }

    if(!is_user_logged_in() || !is_admin()) {
        auth_redirect();
        return;
    }

    $comment_id = (int)$_GET['comment_id'];
    $approved = (int)$_GET['approved'];

    $comment = get_comment($comment_id, ARRAY_A);

    // comentário existe e o usuário é admin?
    if($comment !== null is_admin()) {
        // modifica o status de aprovado do comentário dependendo do valor passado
        switch($approved) {
            case '0':
                $comment['comment_approved'] = 0; // 0 = reprovado
                break;
            case '1':
                $comment['comment_approved'] = 1; // 1 = aprovado
                break;

            case 'spam':
                $comment['comment_approved'] = 'spam'; // spam = spam
                break;

            default: // opção inválida. você pode adicionar alugma mensagem de erro aqui;
                return;
                break;
        }

        wp_update_comment($comment); // atualiza o comentário
    }

    // se chegou aqui é porque o comentário foi atualizado.
    // aqui você pode redirecionar o usuário para uma página de sucesso ou fazer qualquer outra coisa.
}

Note: I just typed all this code and not tested, but it serves as a basis for you to build a plugin with these features.

  • Thanks for the help André I will study the code today and then put the result, but now I already have a north, a place to start and to follow, which makes the solution much easier. Again, thank you!

  • the code suggested by @André-ribeiro worked?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.