Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress Disable Comments (add to function.php)
<?php
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
// Remove comments metabox from dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable support for comments and trackbacks in post types
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
@4ADSTUDIO

This comment has been minimized.

Copy link

@4ADSTUDIO 4ADSTUDIO commented May 22, 2017

No description provided.

@etom-project

This comment has been minimized.

Copy link

@etom-project etom-project commented Jul 10, 2017

Great and Thank you :)

@LeonBeilis

This comment has been minimized.

Copy link

@LeonBeilis LeonBeilis commented Sep 12, 2017

thanks!

@paperbonsai

This comment has been minimized.

Copy link

@paperbonsai paperbonsai commented Sep 19, 2017

Don't forget to delete all existing comments ;]
TRUNCATE wp_commentmeta; TRUNCATE wp_comments;

@loorlab

This comment has been minimized.

Copy link

@loorlab loorlab commented Nov 21, 2017

Great !

@pennestri

This comment has been minimized.

Copy link

@pennestri pennestri commented Jan 1, 2018

Great!

@monowm4

This comment has been minimized.

Copy link

@monowm4 monowm4 commented Jan 11, 2018

Works like a charm, nice job.

@duncanmcclean

This comment has been minimized.

Copy link

@duncanmcclean duncanmcclean commented Feb 28, 2018

Works a treat 👍

@hectorplus

This comment has been minimized.

Copy link

@hectorplus hectorplus commented Mar 28, 2018

Beautiful, there is a function for every plugin, indeed! Thank you this one!

@FeliceAntonio

This comment has been minimized.

Copy link

@FeliceAntonio FeliceAntonio commented May 13, 2018

Great work, very useful, thanks a lot.

@flunder

This comment has been minimized.

Copy link

@flunder flunder commented Jun 14, 2018

Thanks!

@TheBeneficent

This comment has been minimized.

Copy link

@TheBeneficent TheBeneficent commented Jun 16, 2018

Useful, thanks.

@lucasrafaelleite

This comment has been minimized.

Copy link

@lucasrafaelleite lucasrafaelleite commented Jul 27, 2018

Thanks!

@alexwoollam

This comment has been minimized.

Copy link

@alexwoollam alexwoollam commented Sep 19, 2018

Good work!
I forked it to make a WPCS complient version: https://gist.github.com/alexwoollam/2f4bcd4eb4740eb49562131290248f26

@elhajdi

This comment has been minimized.

Copy link

@elhajdi elhajdi commented Nov 19, 2018

Thank you

@danielpost

This comment has been minimized.

Copy link

@danielpost danielpost commented Dec 10, 2018

For anyone interested in this, I cleaned it up a little bit:

<?php

add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;
    
    if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url());
        exit;
    }

    // Remove comments metabox from dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

    // Disable support for comments and trackbacks in post types
    foreach (get_post_types() as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
});

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page in menu
add_action('admin_menu', function () {
    remove_menu_page('edit-comments.php');
});

// Remove comments links from admin bar
add_action('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});
@VerdiH

This comment has been minimized.

Copy link

@VerdiH VerdiH commented Jan 9, 2019

Thank you @mattclements and @danielpost !

@nr1q

This comment has been minimized.

Copy link

@nr1q nr1q commented Jan 16, 2019

Great, thanks! This worked for me to remove the comments icon in admin bar

add_action('wp_before_admin_bar_render', function() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
});
@Afofaifo

This comment has been minimized.

Copy link

@Afofaifo Afofaifo commented May 16, 2019

Great, Thanks!!
How can I hide comments (module,row or section) if the user is logged out

@creativeartbd

This comment has been minimized.

Copy link

@creativeartbd creativeartbd commented Aug 1, 2019

Thanks, @mattclements and, @danielpost too.

@creativeartbd

This comment has been minimized.

Copy link

@creativeartbd creativeartbd commented Aug 6, 2019

Can anyone tell me why the following line is not removed or hide existing comments?

// Hide existing comments
function df_disable_comments_hide_existing_comments($comments) {
$comments = array();
return $comments;
}
add_filter('comments_array', 'df_disable_comments_hide_existing_comments', 10, 2);

@lindenism

This comment has been minimized.

Copy link

@lindenism lindenism commented Nov 3, 2019

Your code has a flaw! It does not remove existing comment count in admin bar.

function df_disable_comments_admin_menu() {
remove_menu_page('edit-comments.php');
}
add_action('init', 'df_disable_comments_admin_menu');

This code is not working and is broken. init should be admin-init.

See working code...

function df_disable_comments_admin_bar() {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
}
add_action('admin_init', 'df_disable_comments_admin_bar');

@mattclements

This comment has been minimized.

Copy link
Owner Author

@mattclements mattclements commented Nov 11, 2019

Thanks @danielpost!

I have updated the very old version that I had to yours, much cleaner and I think it covers all the other flaws that others have commented on.

@thiendo

This comment has been minimized.

Copy link

@thiendo thiendo commented Nov 13, 2019

  1. Change setting in Settings > Discussion.
  2. Disable Comments section in Admin by adding this code to functions.php
function remove_menus() {
    remove_menu_page( 'edit-comments.php' );
}
add_action( 'admin_menu', 'remove_menus' );
@EarthlingDavey

This comment has been minimized.

Copy link

@EarthlingDavey EarthlingDavey commented Jan 30, 2020

Thank you.

For a class that can be included then initialised with \x\Comments::initialise(); try this gist:
https://gist.github.com/EarthlingDavey/bc72015b23449a10e4476c2fe6a63fec

@ChrisChiera

This comment has been minimized.

Copy link

@ChrisChiera ChrisChiera commented Feb 29, 2020

Still shows in the admin bar. Updating that portion to this and appears to work: https://www.isitwp.com/remove-comments-link-from-admin-bar/.

@ramsesdelr

This comment has been minimized.

Copy link

@ramsesdelr ramsesdelr commented Jun 16, 2020

Works perfectly, thanks!

@craigiswayne

This comment has been minimized.

Copy link

@craigiswayne craigiswayne commented Oct 24, 2020

Thank you 👍

@manuki76

This comment has been minimized.

Copy link

@manuki76 manuki76 commented Dec 9, 2020

Thank you works great!

@svsdesign

This comment has been minimized.

Copy link

@svsdesign svsdesign commented Jan 12, 2021

Nice one - 👍

@flyingwebie

This comment has been minimized.

Copy link

@flyingwebie flyingwebie commented Feb 2, 2021

Thanks million :D

@dexterski

This comment has been minimized.

Copy link

@dexterski dexterski commented Apr 8, 2021

It works!

@SteaveRayvon

This comment has been minimized.

Copy link

@SteaveRayvon SteaveRayvon commented Apr 19, 2021

thanks!

@robbiegod

This comment has been minimized.

Copy link

@robbiegod robbiegod commented Apr 23, 2021

Does this function prevent comment spam as well?

@SteaveRayvon

This comment has been minimized.

Copy link

@SteaveRayvon SteaveRayvon commented May 17, 2021

Does this function prevent comment spam as well?
yes

@jadamec

This comment has been minimized.

Copy link

@jadamec jadamec commented Jun 2, 2021

Thanks, but the last part Remove comments links from admin bar doesn't work for me. Comment icon is still showing in Admin Bar.

Wordpress 5.7.2

@glafarge

This comment has been minimized.

Copy link

@glafarge glafarge commented Jun 6, 2021

You could also remove the submenu page Settings > Discussion from admin as no longer needed.

I updated @danielpost clean code, reflecting those changes :

<?php

/**
 * For example, copy the following to "wp-content/mu-plugins/disable-comments.php"
 * to use it as a "must-use plugin" (always-on and before "normals" plugins)
 */ 

add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;
    
    if ($pagenow === 'edit-comments.php' || $pagenow === 'options-discussion.php') {
        wp_redirect(admin_url());
        exit;
    }

    // Remove comments metabox from dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

    // Disable support for comments and trackbacks in post types
    foreach (get_post_types() as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
});

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page and option page in menu 
add_action('admin_menu', function () {
    remove_menu_page('edit-comments.php');
    remove_submenu_page('options-general.php', 'options-discussion.php');
});

// Remove comments links from admin bar
add_action('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment