Collection of useful code snippets for WordPress

Most of these code snippets dedicated to WordPress websites are super useful because you don’t have to use third party plugins. Fewer plugins means fewer files, fewer java scripts and css files and sometime fewer ads in your dashboard.

All code snippets should go to your function.php file in your theme folder.

Snippets in your plugin

If you don’t like fiddling with your theme folder, you can place it into your own plugin which can be edited with the built-in WordPress simple editor, so snippets will stay active, also if you will update (overwrite) your theme folder with the new version.

Please note, sometime you have to use !important property in CSS, sometime not.

OK, so here are my favourite code snippets:

Disable comments

No need to use 3rd party plugin.

// This function will deactivate all features related to comments and conceal them from the dashboard
function disable_comments_and_post_via_email() {
    global $pagenow;

    // Redirect any user attempting to access the comments page
    if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url(), 301);
        exit;
    }

    // Eliminate the meta box for recent comments
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

    // Withdraw 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);

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

    // Remove comments page from the 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 the admin bar
    add_action('init', function () {
        if (is_admin_bar_showing()) {
            remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
        }
    });

    // Deactivate post via email
    update_option('mailserver_url', '');
}

add_action('admin_init', 'disable_comments_and_post_via_email');

// Unregister the widget for recent comments
function unregister_comment_widget() {
    unregister_widget('WP_Widget_Recent_Comments');
}

add_action('widgets_init', 'unregister_comment_widget', 1);

// Disable Comments START
// Withdraw support for comments and trackbacks in post types
function disable_comments_post_types_support() {
    $post_types = get_post_types();
    foreach ($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');
        }
    }
}
add_action('admin_init', 'disable_comments_post_types_support');

// Close comments on the front-end
function disable_comments_status() {
    return false;
}
add_filter('comments_open', 'disable_comments_status', 20, 2);
add_filter('pings_open', 'disable_comments_status', 20, 2);

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

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

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

// Remove comments metabox from the dashboard
function disable_comments_dashboard() {
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'disable_comments_dashboard');

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

// Remove Discussion settings page
function remove_menus(){
  remove_submenu_page('options-general.php', 'options-discussion.php');
}
add_action('admin_init', 'hide_post_via_email_in_settings');
function hide_post_via_email_in_settings() {
    remove_action('add_option_form_fields', 'post_via_email_option');
}

// Disable post via email
add_action('init', 'disable_post_via_email');
function disable_post_via_email() {
    remove_action('wp-mail.php', 'wp-mail');
}

// Hide post via email in dashboard
add_action('admin_menu', 'hide_post_via_email_in_dashboard');
function hide_post_via_email_in_dashboard() {
    remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
}

Clean header

Basically a ‘must have’ snippet.

//------------------------
//      CLEAN HEADER
//------------------------

add_action('init', 'clean_up_header');
function clean_up_header()
{
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wp_generator');
    remove_action('wp_head', 'index_rel_link');
    remove_action('wp_head', 'wlwmanifest_link');
    remove_action('wp_head', 'feed_links', 2);
    remove_action('wp_head', 'feed_links_extra', 3);
    remove_action('wp_head', 'parent_post_rel_link', 10, 0);
    remove_action('wp_head', 'start_post_rel_link', 10, 0);
    remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
    remove_action('wp_head', 'wp_shortlink_header', 10, 0);
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
}
add_filter('xmlrpc_enabled', '__return_false');

Remove archive

Presentation website should not have this blog feature activated. This does SEO friendly redirect 301.

//------------------------
//    REMOVE ARCHIVE
//------------------------

function redirect_to_home($query)
{
    if (is_date()) {
        wp_redirect(home_url(), $status = 301);
        exit;
    }
}
add_action('parse_query', 'redirect_to_home');

Customised login page

Feel free to have fun with changing CSS.

//------------------------
//      LOGIN PAGE
//------------------------

function customloginpage()
{
    echo '<style type="text/css">
h1 a {
	background-image: url(https://s.w.org/style/images/about/WordPress-logotype-simplified.png) !important;
	height: 100px !important;
	width: 100px !important;
	background-size: 100px 100px !important;
	background-repeat: no-repeat;
	padding-bottom: 30px;
}
#loginform {
	box-shadow: 0 2px 4px rgb(0 0 0 / 7%);
	border: 1px solid #E6E7E8 !important;
}
#loginform:hover {
	box-shadow: rgba(50, 50, 93, 0.25) 0px 50px 100px -20px, rgba(0, 0, 0, 0.3) 0px 30px 60px -30px;
}
body {
	background: #FFF !important;
}
.button-primary {
	background: #FFF !important;
	border-color: #000000 !important;
	color: #000 !important;
}
.button-primary:hover {
	background: #000 !important;
	color: #FFF !important;
}
#loginform, .button-primary {
	-webkit-transition: all .3s ease;
	-moz-transition: all .3s ease-in-out;
	transition: all .3s ease-in-out;
}
.login .message, .login .success {
	border-left: 4px solid #555555 !important;
}
.wp-core-ui .button, .wp-core-ui .button-secondary {
	color: #555!important;
	border-color: #555 !important;
	background: #fff !important;
}
#login {
	width: 400px !important;
	padding: 10% 0 0 !important;
}
.privacy-policy-page-link, #backtoblog {
	visibility: hidden;
	display: none;
}

</style>';
}
add_action('login_head', 'customloginpage');
// Error message
function no_wordpress_errors()
{
    return 'Wrong credentials. Sit back, relax and think about your login information for a minute and when you are sure, try again.';
}
add_filter('login_errors', 'no_wordpress_errors');

Soon I will add more …