Some code snippets that I use from time to time (do adjust the code if necessary)
Genesis – Add Code in head
add_action( 'wp_head', 'pref_google_tag_manager_js' );
function pref_google_tag_manager_js() { ?>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','');</script>
<!-- End Google Tag Manager -->
<meta name="google-site-verification" content="" />
<?php }
Genesis – Add Google Tag Manager code immediately after opening <body> tag
add_action( 'genesis_before', 'pref_google_tag_manager_no_js' );
function pref_google_tag_manager_no_js() { ?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id="
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<?php }
WordPress – Changing the WordPress login page logo
add_action( 'login_enqueue_scripts', 'tpr_login_logo' );
function tpr_login_logo() { ?>
<style type="text/css">
body.login h1 a {
background-image: url( "<?php echo get_stylesheet_directory_uri(); ?>/images/logo.png" );
background-position: top center;
background-repeat: no-repeat;
background-size: contain;
width: 155px;
height: 83px;
margin: 0 auto 15px;
padding: 0;
}
</style>
<?php }
WP Gravity Forms – Filter mail with cyrillic
add_filter('gform_pre_send_email', 'reject_certain_emails_function');
function reject_certain_emails_function($email){
// reject any Cyrillic
if(preg_match('/[\p{Cyrillic}]/u', $email['message'])){
$email['abort_email'] = true;
}
return $email;
}
Disable XML-RPC – XML-RPC is a protocol that allows WordPress to communicate with other systems, but it can be a security vulnerability. Disabling it can help prevent brute force attacks.
add_filter('xmlrpc_enabled', '__return_false');
Remove WordPress Version – Displaying the WordPress version in the page source can give attackers information about potential vulnerabilities.
function wpbeginner_remove_version() {
return '';
}
add_filter('the_generator', 'wpbeginner_remove_version');
Change Login Error Messages – Changing the default login error messages (e.g., “invalid username” or “invalid password”) makes it harder for malicious users to guess login details.
function no_wordpress_errors() {
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );
Change Default Gravatar – This snippet lets you set a custom default image for users who don’t have a Gravatar.
add_filter('avatar_defaults', 'custom_gravatar');
function custom_gravatar($avatar_defaults) {
$myavatar = 'https://example.com/path/to/my-custom-avatar.png';
$avatar_defaults[$myavatar] = 'Custom Gravatar';
return $avatar_defaults;
}
Add a Custom Post Type – This is a very common and powerful snippet for creating a new type of content, like “Portfolios” or “Products,” which are different from standard posts and pages.
function create_posttype_portfolio() {
register_post_type( 'portfolio',
array(
'labels' => array(
'name' => __( 'Portfolios' ),
'singular_name' => __( 'Portfolio' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'portfolio'),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
)
);
}
add_action( 'init', 'create_posttype_portfolio' );
Add SVG Support – WordPress doesn’t natively support SVG uploads due to security concerns. This snippet allows you to enable it safely.
function svg_support_upload_mimes($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'svg_support_upload_mimes');
Customize the Admin Bar – You can use this snippet to remove or add items to the WordPress admin bar for a cleaner user experience.
function remove_admin_bar_items() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
$wp_admin_bar->remove_menu('about');
$wp_admin_bar->remove_menu('wporg');
}
add_action('admin_bar_menu', 'remove_admin_bar_items', 999);
Disable Comments on a Specific Post Type – If you have a custom post type (like “Portfolios”) where comments aren’t relevant, you can disable them entirely.
function disable_comments_on_cpt() {
remove_post_type_support('portfolio', 'comments');
}
add_action('init', 'disable_comments_on_cpt');