If you’re looking to add more functionalities and enhancements to your WordPress website, incorporating code snippets can help. These small pieces of code can help improve your website’s security and user experience. In this guide, we’ve compiled some practical WordPress code snippets that are easy for beginners. Whether you want to customize your website’s design, improve its performance, or explore new possibilities, these code snippets can be an excellent tool.
Why Embed Code Snippets in WordPress?
Your WordPress site is a blank canvas, and code snippets are the versatile tools you can use to create a masterpiece. Whether you embed custom code into your theme files or use a dedicated code snippets plugin, you’re not just adding unique design elements but also optimizing your site’s functionality and performance.
If you want to tweak the color users see when they select text on your site, a simple CSS snippet can make that happen. For beginners, integrating such snippets elevates your site’s aesthetics and operational efficiency and minimizes reliance on multiple plugins, streamlining your site’s speed.
Exploring code snippets is an excellent chance to enhance your coding skills. The WordPress community generously shares a wealth of free code snippets, each designed to tackle specific challenges or add interesting features to your site.
Disable the WordPress Admin Bar for All Users Except Administrators
The admin bar can be distracting for non-admin users. Keep your site looking clean for subscribers or contributors by turning it off for everyone except administrators.
copy this code:
add_action(‘after_setup_theme’, ‘remove_admin_bar’);
function remove_admin_bar() {
if (!current_user_can(‘administrator’) && !is_admin()) {
show_admin_bar(false);
}
}
Change the Default Gravatar in Comments
Gravatar is a service that displays user profile images in the comments. Personalize your site by changing the default Gravatar to something unique.
copy this code:
add_filter( ‘avatar_defaults’, ‘custom_gravatar’ );
function custom_gravatar ($avatar_defaults) {
$myavatar = ‘URL_TO_YOUR_CUSTOM_GRAVATAR’;
$avatar_defaults[$myavatar] = “Custom Gravatar”;
return $avatar_defaults;
}
Replace URL_TO_YOUR_CUSTOM_GRAVATAR with the actual image URL you wish to use.
Limit Post Revisions
WordPress saves every change you make in the posts, which can quickly bloat your database. Limit the number of revisions WordPress keeps to keep your site running smoothly.
copy this code:
define(‘WP_POST_REVISIONS’, 5);
This snippet limits WordPress to save only the last five revisions.
Increase Memory Limit
Sometimes, especially when using resource-intensive plugins or themes, you might need to increase WordPress’s memory limit.
copy this code:
define(‘WP_MEMORY_LIMIT,’ ‘256M’);
This increases the PHP memory limit to 256MB.
Remove WordPress Version Number
Hiding your WordPress version number can help protect your site from hackers targeting specific vulnerabilities.
copy this code:
remove_action(‘wp_head’, ‘wp_generator’);
Stop Loading wp-emoji-release.min.js and wp-emoji-release.min.css
Emojis are fun, but WordPress loads additional scripts and styles for them. Stop loading these files to speed up your site if you don’t use emojis.
copy this code:
remove_action(‘wp_head’, ‘print_emoji_detection_script’, 7);
remove_action(‘wp_print_styles’, ‘print_emoji_styles’);
Redirect Users to the Homepage After Logout
Improve user experience by redirecting users to your homepage or a custom page after they log out.
copy this code:
add_action(‘wp_logout’,’auto_redirect_after_logout’);
function auto_redirect_after_logout(){
wp_safe_redirect(home_url());
exit;
}
Change the Login Logo URL
By default, the WordPress login page logo links to wordpress.org. Make it link to your homepage instead.
copy this code:
function change_login_logo_url() {
return home_url();
}
add_filter( ‘login_headerurl’, ‘change_login_logo_url’ );
Add a Custom Dashboard Widget
Add a custom widget to the WordPress dashboard to display important information or reminders.
copy this code:
add_action(‘wp_dashboard_setup’, ‘custom_dashboard_widget’);
function custom_dashboard_widget() {
wp_add_dashboard_widget(‘custom_help_widget’, ‘Theme Support’, ‘custom_dashboard_help’);
}
function custom_dashboard_help() {
echo ‘<p>Welcome to YourSiteName! Need help? Contact the support here: <a href=”mailto:support@yoursite.com”>support@yoursite.com</a>.</p>’;
}
These snippets are a great starting point for beginners looking to tweak their WordPress sites. Remember, always back up your site before adding or changing code, especially if you’re new to WordPress. This ensures that you can easily revert changes if anything goes wrong.