Are you trying to find some WordPress code snippets to enhance your website?
Using code snippets on your WordPress site allows you to create unique designs and functionalities that themes and plugins might not offer. These snippets can also boost security and make the admin dashboard easier to use.
In this article, we’ll show you a list of the best WordPress code snippets for beginners.
Why Use Code Snippets in WordPress?
Adding code snippets to your WordPress site can help you customize your website in many ways. For example, you can change the color of text selection with a simple CSS snippet. Snippets can also improve your site’s performance and speed by reducing the need for multiple plugins. They also help you learn coding and use the vast number of free code snippets shared by the WordPress community.
Here are some useful WordPress code snippets for beginners:
- Allow SVG File Upload
SVG (Scalable Vector Graphics) files are smaller and lighter than JPEG or PNG files, improving your site’s speed. However, WordPress doesn’t allow SVG uploads by default due to security concerns. To upload SVG files, add this code snippet to your site:
if (!current_user_can(‘administrator’)) {
return $upload_mimes;
}
$upload_mimes[‘svg’] = ‘image/svg+xml’;
$upload_mimes[‘svgz’] = ‘image/svg+xml’;
return $upload_mimes;
});
add_filter(‘wp_check_filetype_and_ext’, function ($wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime) {
if (!$wp_check_filetype_and_ext[‘type’]) {
$check_filetype = wp_check_filetype($filename, $mimes);
$ext = $check_filetype[‘ext’];
$type = $check_filetype[‘type’];
$proper_filename = $filename;
if ($type && 0 === strpos($type, ‘image/’) && ‘svg’ !== $ext) {
$ext = false;
$type = false;
}
$wp_check_filetype_and_ext = compact(‘ext’, ‘type’, ‘proper_filename’);
}
return $wp_check_filetype_and_ext;
}, 10, 5);
Add this code to your theme’s functions.php file or use a code snippets plugin like WPCode.
- Disable the WP Admin Bar
The WP admin bar appears at the top of the screen for all logged-in users. To disable it, add this snippet:
add_filter(‘show_admin_bar’, ‘__return_false’);
o keep the admin bar for administrators only, check our tutorial on disabling it for other users.
- Remove WordPress Version Number
To prevent security issues, you can hide the WordPress version number with this snippet:
add_filter(‘the_generator’, ‘__return_empty_string’);
- Add Featured Images to RSS Feeds
To make your RSS feed more appealing by adding featured images, use this code:
function wpcode_snippet_rss_post_thumbnail($content) {
global $post;
if (has_post_thumbnail($post->ID)) {
$content = ‘<p>’ . get_the_post_thumbnail($post->ID) . ‘</p>’ . $content;
}
return $content;
}
add_filter(‘the_excerpt_rss’, ‘wpcode_snippet_rss_post_thumbnail’);
add_filter(‘the_content_feed’, ‘wpcode_snippet_rss_post_thumbnail’);
- Disable Automatic Update Emails
To stop receiving emails for automatic updates, add this code:
add_filter(‘auto_core_update_send_email’, ‘__return_false’);
add_filter(‘auto_plugin_update_send_email’, ‘__return_false’);
add_filter(‘auto_theme_update_send_email’, ‘__return_false’);
- Change ‘Howdy, Admin’ in the Admin Bar
To change the ‘Howdy, Admin’ greeting, use this snippet:
function wpcode_snippet_replace_howdy($wp_admin_bar) {
$new_howdy = ‘Welcome,’;
$my_account = $wp_admin_bar->get_node(‘my-account’);
$wp_admin_bar->add_node(array(
‘id’ => ‘my-account’,
‘title’ => str_replace(‘Howdy,’, $new_howdy, $my_account->title),
));
}
add_filter(‘admin_bar_menu’, ‘wpcode_snippet_replace_howdy’, 25);
- Disable XML-RPC
To disable XML-RPC and improve security, use this code:
add_filter(‘xmlrpc_enabled’, ‘__return_false’);
- Disable Automatic Trash Emptying
To stop WordPress from automatically emptying the trash, use this snippet:
add_action(‘init’, function() {
remove_action(‘wp_scheduled_delete’, ‘wp_scheduled_delete’);
});
- Change Excerpt Length
To change the length of post excerpts, use this code:
add_filter(‘excerpt_length’, function($length) {
return 40;
}, 500);
- Disable Site Admin Email Verification
To disable the admin email verification notice, add this snippet:
add_filter(‘admin_email_check_interval’, ‘__return_false’);
- Disable Automatic Updates
To disable automatic updates for WordPress, plugins, and themes, use this code:
- How to Add Code Snippets in WordPress (Easy Method)
You can add these code snippets to your theme’s functions.php file or use a plugin like WPCode. WPCode is a safe and easy way to add custom code to your site. It also includes a library of over 900 code snippets.
To add a code snippet with WPCode:
- Install and activate the WPCode plugin.
- Go to the Code Snippets » + Add Snippet page.
- Choose a custom or premade snippet.
- Paste your code in the ‘Code Preview’ box.
- Select the ‘Auto Insert’ mode in the ‘Insertion’ section.
- Activate the snippet and click ‘Update’ to save.
Frequently Asked Questions About WordPress Code Snippets
- How do I display code on my WordPress site? Add a Code block to your page/post and paste your code.
- How do I create a WordPress website from scratch without coding? Use SeedProd, a WordPress page builder with premade templates and a drag-and-drop builder.
- Where can I get WordPress code snippets? Use the WPCode library for over 900 code snippets.