WordPress

Administrator – Pulpit nawigacyjny – Anuluj ustawienia ostatnich komentarzy

  • 5 stycznia, 2016
  • 4 min read
Administrator – Pulpit nawigacyjny – Anuluj ustawienia ostatnich komentarzy


Lista ostatnich komentarzy jest częścią Widżet pulpitu nawigacyjnego aktywności.

Podejście nr 1

Moglibyśmy usunąć ten widżet pulpitu nawigacyjnego, a następnie dodać naszą zmodyfikowaną wersję:

/**
 * Remove the latest comments from the Activity dashboard widget (approach #1)
 */
add_action( 'wp_dashboard_setup', function()
{
    // Remove the Activity widget
    remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );

    // Add the modified Activity widget
    if ( is_blog_admin() ) 
       wp_add_dashboard_widget( 
           'wpse_dashboard_activity', 
           __( 'Activity' ), 
           'wpse_dashboard_site_activity' 
       );

} );

gdzie nasze niestandardowe wywołanie zwrotne (bez komentarzy) to:

/**
 * Custom Activity Widget callback
 *
 * @see wp_dashboard_site_activity()
 */
function wpse_dashboard_site_activity() {

        echo '<div id="activity-widget">';

        $future_posts = wp_dashboard_recent_posts( array(
                'max'     => 5,
                'status'  => 'future',
                'order'   => 'ASC',
                'title'   => __( 'Publishing Soon' ),
                'id'      => 'future-posts',
        ) );
        $recent_posts = wp_dashboard_recent_posts( array(
                'max'     => 5,
                'status'  => 'publish',
                'order'   => 'DESC',
                'title'   => __( 'Recently Published' ),
                'id'      => 'published-posts',
        ) );

        // No comments!    
        $recent_comments = false; // wp_dashboard_recent_comments();

        if ( !$future_posts && !$recent_posts && !$recent_comments ) {
                echo '<div class="no-activity">';
                echo '<p class="smiley"></p>';
                echo '<p>' . __( 'No activity yet!' ) . '</p>';
                echo '</div>';
        }

        echo '</div>';
}

Mogliśmy użyć zamiast tego:

// No comments!    
$recent_comments = wp_dashboard_recent_comments( $total_items = 0 );

Podejście nr 2

Innym krótszym podejściem byłoby zaczepienie do dashboard_recent_posts_query_args odfiltruj na stronie pulpitu nawigacyjnego i usuń komentarze:

/**
 * Remove the latest comments from the Activity Dashboard widget (approach #2)
 */
add_action( 'load-index.php', function(){
    add_filter( 'dashboard_recent_posts_query_args', function( $args )
    {
        add_filter( 'the_comments', '__return_false' );
        return $args;
    } );    
} );

Podejście nr 3

Moglibyśmy wtedy dodać dalsze ograniczenia, na przykład opróżnić komentarze tylko raz:

/**
 * Remove the latest comments from the Activity Dashboard widget (approach #3)
 */
add_action( 'load-index.php', function(){
    add_filter( 'dashboard_recent_posts_query_args', function( $args )
    {
        add_filter( 'the_comments', function( $comments )
        {
            static $instance = 0;
            $comments = ( 1 === ++$instance ) ? false : $comments;
            return $comments;
        } );
        return $args;
    } );    
} );

Podejście nr 4

Jeśli chcemy zredukować zapytanie do bazy danych komentarzy, możemy użyć metody pre_get_comments zamiast tego hak.

Warto przeczytać!  Jak utworzyć post Roundup w WordPress (łatwy sposób)

Oto jeden z takich pomysłów:

/**
 * Remove the latest comments from the Activity Dashboard widget (approach #4)
 */
add_action( 'load-index.php', function()
{
    add_filter( 'dashboard_recent_posts_query_args', function( $args )
    {
        // Let's exit the WP_Comment_Query from the get_comments_ids() method:
        add_action( 'pre_get_comments', function( \WP_Comment_Query $q )
        {
            if( 1 === did_action( 'pre_get_comments' ) )
                $q->set( 'count', true );

            // Override the WHERE part
            add_filter( 'comments_clauses', function( $clauses )
            {
                if( 1 === did_action( 'pre_get_comments' ) )
                    $clauses['where'] = ' 0=1 ';
                return $clauses;
            }, PHP_INT_MAX );

        } );
        return $args;
    } );    
} );

gdzie używamy tzw count zmienna zapytania z WP_Comment_Query aby zminimalizować zapytanie do bazy danych.

Podejście nr 5

Oto jeszcze bardziej drastyczne podejście:

/**
 * Remove latest comments from the Activity Dashboard widget (approach #5)
 */
add_action( 'load-index.php', function()
{
    add_filter( 'dashboard_recent_posts_query_args', function( $args )
    {
        // Let's exit the WP_Comment_Query from the get_comments_ids() method:
        add_action( 'pre_get_comments', function( \WP_Comment_Query $q )
        {
            if( 1 === did_action( 'pre_get_comments' ) )
                $q->set( 'count', true );

            // Eliminate the next query         
            add_filter( 'query', function( $query )
            {
                static $instance = 0;
                if( 1 === ++$instance )
                    $query = false;
                return $query;
            }, PHP_INT_MAX );

        } );
        return $args;
    } );    
} );

gdzie po prostu eliminujemy to zapytanie o komentarz.

Warto przeczytać!  php — Problem z domyślną wartością i pobieraniem niestandardowych pól użytkownika

Podejście nr 6

Wreszcie oto wersja CSS:

/**
 * Hide the latest comments from the Activity Dashboard widget (approach #6)
 */
add_action( 'admin_head-index.php', function()
{
    print '<style>#latest-comments{ display:none; }</style>';
} );


Źródło