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.

//------------------------
//    DISABLE COMMENTS
//------------------------

// Delete all existing comments first, then apply this snippet.
add_action('admin_init', function ()
{
    global $pagenow; if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url()); exit; }
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); 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'); }
    }
}
);
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
add_filter('comments_array', '__return_empty_array', 10, 2);
add_action('admin_menu', function ()
{
    remove_menu_page('edit-comments.php'); }
);
add_action('init', function ()
{
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60); }
}
);
function unregister_comment_widget()
{
    unregister_widget('WP_Widget_Recent_Comments');
}
add_action('widgets_init', 'unregister_comment_widget', 1);

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 …