Approve wordpress comments via email

0

I'm developing a Wordpress website and I need the approval of the comments in the posts to be made through email. I searched for plugins and tutorials on the subject but found nothing.

The intention is that the customer receives the comment via email on his smarthphone and has 2 links, one to APPROVE and another to REJECT and this operation is done without the need to log in to the site, just by clicking on the link. >

How can I do this?

    
asked by anonymous 02.03.2015 / 19:54

1 answer

1

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

To verify that the current link is the "approve" comment, simply add a hook in init and then check the link and update the status of the comment.

To get the comment by the ID you can use the get_comment() function and to update the 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 did not test, but it serves as the basis for you to build a plugin with these features.     

02.03.2015 / 21:21