Filtro dell'elenco dei commenti dell'amministratore per mostrare solo i commenti dell'utente corrente?


10

Nella pagina Commenti ( /wp-admin/edit-comments.php), ogni utente che ha effettuato l'accesso può vedere tutti i commenti sul sito.
pagina dei commenti


Vorrei che gli utenti vedessero solo i propri commenti e i commenti lasciati ai propri post.

Come posso filtrare questo?

Risposte:


9

Ognuno di questi 3 ti aiuterà:

//Before getting the comments, on the WP_Comment_Query object for each comment
add_action('pre_get_comments', 'wpse56652_filt_comm');

//Applied on the comments SQL Query, you can modify the 'Where' part of the query
add_filter('comments_clauses', 'wpse56652_filt_comm');

//After the comments are fetched, you can modify the comments array
add_filter('the_comments', 'wpse56652_filt_comm');

function wpse56652_filt_comm($param) {
    //access the current user
    global $current_user;
    get_currentuserinfo();

    //current users id = $current_user->ID;

    //Current users posts, check get_posts params to change as per your need
    $user_posts = get_posts(array('author' => $current_user->ID, 'posts_per_page' => -1));

    echo '<pre>';
    print_r($param);
    echo '</pre>';

    return $param;
}

Inoltre, è possibile utilizzare global $pagenowper assicurarsi che il codice venga eseguito solo su questa pagina.

Mi dispiace di essere un po 'malato oggi, quindi non ho potuto scrivere un esempio! ;)

Modificare:

/**
 * Show only the Comments MADE BY the current logged user
 * and the Comments MADE TO his/hers posts.
 * Runs only for the Author role.
 */

add_filter('the_comments', 'wpse56652_filter_comments');

function wpse56652_filter_comments($comments){
    global $pagenow;
    global $user_ID;
    get_currentuserinfo();
    if($pagenow == 'edit-comments.php' && current_user_can('author')){
        foreach($comments as $i => $comment){
            $the_post = get_post($comment->comment_post_ID);
            if($comment->user_id != $user_ID  && $the_post->post_author != $user_ID)
                unset($comments[$i]);
        }
    }
    return $comments;
}

grazie per la risposta - solo poche ore fa ho trovato l'articolo per risolvere questo problema per i post nel tuo blog! Trovo persino i parametri per i commenti, ma non so come impostare l'id utente registrato corrente. se voglio solo mostrare solo i suoi commenti, posso usare il suo ID ma voglio anche mostrare commenti ai suoi post. Come si può fare?
— moonvader,

Prego! Controlla la risposta ora, l'ho aggiornata.
— Rutwick Gangurde,

ora mostra tutti i parametri di commento nella pagina wp-admin / edit-commenti.php - ma posso ancora vedere tutti i commenti (
— moonvader,

Questo perché devi filtrare i commenti! L'ho messo print_r per amor di prova!
— Rutwick Gangurde,

questo filtro deve essere fatto all'interno della funzione wpse56652_filt_comm? puoi mostrarmi un esempio come mostrare solo i commenti dell'utente con id = 4?
— moonvader,
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.