For beginners exploring WordPress, mastering code snippets can unlock a wealth of customization options, allowing you to modify your website to fit your unique requirements.
Throughout the development of our website, we frequently utilized code snippets for enhancements in both the admin panel and the front-end display.
This guide aims to empower beginners by carefully curating and explaining valuable WordPress code snippets that you can implement immediately.
We have rigorously tested these snippets on actual websites to confirm their functionality and ensure they operate without errors.
In this article, we will present our compilation of the most beneficial WordPress code snippets for those just starting out.
Why Incorporate Code Snippets in WordPress?
If you manage a WordPress website, integrating useful code snippets into your theme files or using a code snippets plugin can help you achieve endless customization and enhance your site’s uniqueness.
Custom code allows you to adjust specific elements on your website. For instance, you can modify the text selection color in WordPress by implementing a straightforward CSS code snippet.
As a newcomer, incorporating helpful code snippets can also boost your site’s performance and speed by minimizing reliance on multiple plugins.
In addition, code snippets can enhance your programming abilities and provide access to a wealth of free code snippets shared by the WordPress community.
Now, let’s explore some of the most valuable WordPress code snippets for beginners. Use the quick links below to navigate to different sections of our tutorial:
- Enable SVG File Uploads
- Hide the WordPress Admin Bar
- Remove the WordPress Version Number
- Include Featured Images in RSS Feeds
- Turn Off Automatic Update Notifications
- Change ‘Howdy, Admin’ in the Admin Bar
- Disable XML-RPC Functionality
- Prevent Automatic Emptying of Trash
- Adjust the Length of Excerpts
- Turn Off Site Admin Email Verification
- Disable Automatic Updates
- How to Easily Add Code Snippets in WordPress
- Common Questions About WordPress Code Snippets
1. Enable SVG File Uploads
SVG (Scalable Vector Graphics) is a file format that uses XML markup to define vector graphics. This format allows you to resize images without any loss of quality.
These files are smaller and more efficient than JPEG or PNG, helping to improve your website’s loading speed.
WordPress restricts SVG file uploads by default due to the potential for malicious code that can jeopardize your site’s security.
If you still wish to upload SVG files to your website, you can use the following code snippet:
/** * Enable SVG file uploads for administrators. * * @param array $upload_mimes Allowed MIME types. * * @return mixed */
add_filter( 'upload_mimes', function ( $upload_mimes ) { // By default, only administrators can upload SVG files. // To allow other user roles, modify or comment the lines below, but be cautious of // security risks associated with allowing any user to upload SVG files. if ( ! current_user_can( 'administrator' ) ) { return $upload_mimes; } $upload_mimes['svg'] = 'image/svg+xml'; $upload_mimes['svgz'] = 'image/svg+xml'; return $upload_mimes; }
);
/** * Validate MIME type for SVG files. * * @param array $wp_check_filetype_and_ext Contains extension, MIME type, and corrected filename. * @param string $file Full file path. * @param string $filename The name of the file (may differ from $file if it's in a temporary directory). * @param string[] $mimes Array of MIME types indexed by their file extension regex. * @param string|false $real_mime The actual MIME type or false if it cannot be determined. */
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
);
You can insert this code into your theme’s functions.php file or utilize a code snippets plugin like WPCode. We will guide you through the process later in this article.
For comprehensive instructions, check out our tutorial on how to add SVG image files in WordPress.
2. Disable the WordPress Admin Bar
By default, WordPress displays an admin bar at the top of your site for all logged-in users, including subscribers, authors, editors, and other roles.
This admin bar can lead users to your WordPress dashboard, allowing them to make changes to your site based on their permissions.
However, it can be somewhat distracting while viewing the front end of your site, as it may overlap with design elements like the header.
To turn off the WordPress admin bar, simply add the following PHP code snippet to your site:
/* Disable WordPress Admin Bar for all users */
add_filter( 'show_admin_bar', '__return_false' );
After executing the code, the admin bar will no longer appear on the front end of the website.
If you prefer to hide the admin bar from all users except administrators, check out our guide on disabling the WordPress admin bar for non-admins.
3. Hide the WordPress Version Number
WordPress shows the current version number on your site for tracking purposes.
However, this information can create security risks by revealing the WordPress version to potential hackers. They can exploit known vulnerabilities in specific versions.
To hide the version number, simply add the following code snippet to your site:
add_filter('the_generator', '__return_empty_string');
By doing this, hackers will be unable to determine your WordPress version through automated scanners and other basic methods.
For comprehensive instructions, refer to our guide on the proper way to hide the WordPress version number.
4. Include Featured Images in RSS Feeds
RSS feeds enable users to receive regular updates about your WordPress blog through feed readers like Feedly.
Enhancing your content with featured images or thumbnails in your RSS feeds can attract more visitors and improve the overall user experience on your site.
You can easily display post thumbnails in your RSS feeds by using the following helpful WordPress code snippet:
/** * Display the post thumbnail, if available, before the content in feeds. * * @param string $content The post content. * * @return string * /
function wpcode_snippet_rss_post_thumbnail( $content ) { global $post; if ( has_post_thumbnail( $post->ID ) ) { $content = '' . 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' );
This feature can enhance your feed’s engagement and encourage visitors to return to your site.
For comprehensive guidance, check out our tutorial on adding post thumbnails to your WordPress RSS feeds.
5. Turn Off Automatic Update Notifications
By default, WordPress notifies you via email whenever it automatically updates any plugins, themes, or the core system.
If you manage multiple WordPress sites, receiving constant update notifications in your email can be quite frustrating.
To stop these automatic update emails, simply add the following PHP code snippet to your website:
// Disable automatic update emails.
add_filter( 'auto_core_update_send_email', '__return_false' );
// Disable automatic update emails for plugins.
add_filter( 'auto_plugin_update_send_email', '__return_false' );
// Disable automatic update emails for themes.
add_filter( 'auto_theme_update_send_email', '__return_false' );
After implementing this code, you will no longer receive notifications for plugin or theme automatic updates.
For comprehensive guidance, refer to our step-by-step tutorial on disabling automatic update email notifications in WordPress.
6. Customize the ‘Howdy, Admin’ Greeting in the Admin Bar
When you access your WordPress dashboard, a greeting saying ‘Howdy’ followed by your display name appears in the top right corner.
This greeting might feel unnatural, seem outdated, or even be a bit bothersome.
You can easily modify the greeting in the admin bar by adding the following code snippet to your WordPress site:
function wpcode_snippet_replace_howdy( $wp_admin_bar ) { // Modify the line below to customize the greeting displayed in the admin bar instead of "Howdy,". $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 );
After adding the code, you should also insert a personalized greeting next to the $new_howdy = line in the script.
For further details, check out our tutorial on how to modify or remove ‘Howdy Admin’ in WordPress.
7. Disable XML-RPC
XML-RPC is an essential WordPress API that enables users to connect their websites with external services.
For example, enabling XML-RPC is necessary if you wish to use automation tools like Uncanny Automator or mobile applications to manage your site.
However, if you do not plan to utilize these features, we suggest disabling XML-RPC to enhance security and prevent unauthorized access.
Hackers may exploit these vulnerabilities to discover your login information or initiate DDoS attacks.
To disable XML-RPC functionality, simply use the following code snippet on your website:
add_filter( 'xmlrpc_enabled', '__return_false' );
For additional guidance, check out our tutorial on disabling XML-RPC in WordPress.
8. Disable Automatic Trash Emptying
By default, WordPress automatically deletes items in the trash after 30 days, including posts, pages, and media files.
However, some users prefer to keep their trash intact to recover deleted files whenever needed.
To prevent automatic trash emptying, you can add the following code snippet to your WordPress site:
add_action( 'init', function() { remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
} );
After implementing this code, you will need to manually empty your trash. For further information, refer to our tutorial on limiting or disabling automatic trash emptying in WordPress.
9. Change Excerpt Length
Excerpts display the initial lines of your blog posts beneath the post titles on your WordPress homepage, blog, or archive pages.
Consider shortening your excerpt to spark curiosity and motivate users to click on the post for more information.
Alternatively, you can lengthen the excerpt to provide additional context and important details to readers without requiring them to click on the post.
To modify the excerpt length, simply add this code snippet to your website:
add_filter( 'excerpt_length', function ( $length ) { // Specify the number of words to display in the excerpt. return 40; }, 500
);
This snippet will, by default, limit the excerpt to 40 words, but you can change the number on Line 5 to suit your blog’s needs.
For further details, check out our beginner’s guide on customizing WordPress excerpts.
10. Disable Site Admin Email Verification
By default, WordPress sends an email to site administrators every few months to verify that their email address is still valid.
However, this notification can sometimes arrive more frequently than necessary, which can be quite bothersome.
Fortunately, you can turn off the admin email verification notice by adding the following code snippet to your WordPress site:
Disable the admin email verification interval.
For step-by-step guidance, refer to our tutorial on disabling the WordPress admin email verification notice.
11. Turn Off Automatic Updates
WordPress automatically updates its core software, plugins, and themes to enhance security, prevent malware infections, protect against website breaches, and safeguard data.
However, these automatic updates can occasionally lead to compatibility problems or even break your website in rare cases.
If that happens, you can use the following code snippet to turn off automatic updates:
// Disable core auto-updates
add_filter( 'auto_update_core', '__return_false' );
// Disable auto-updates for plugins.
add_filter( 'auto_update_plugin', '__return_false' );
// Disable auto-updates for themes.
add_filter( 'auto_update_theme', '__return_false' );
This code will disable all automatic updates for WordPress core software, themes, and plugins. For more detailed information, see our tutorial on disabling automatic updates in WordPress.
How to Easily Add Code Snippets in WordPress
Now that you are familiar with essential WordPress code snippets for beginners, you can easily integrate them into your theme’s stylesheets or the functions.php file.
However, keep in mind that even a minor mistake while entering the code can disrupt your site and render it inaccessible. Additionally, if you change to a different theme, all your custom code will be lost, requiring you to re-enter it.
This is why we always recommend using WPCode.
We utilize WPCode to enhance the functionality of our WordPress sites, and we have discovered that it simplifies the process of safely adding custom code to your website.
Moreover, the plugin includes a library of over 900 code snippets, encompassing all the ones mentioned above. For further details, check out our comprehensive WPCode review.
First, you need to install and activate the WPCode plugin. For step-by-step guidance, refer to our tutorial on how to install a WordPress plugin.
📒 Note: There is also a free version of the WPCode plugin available. However, upgrading to the premium version will provide you with access to a cloud-based snippets library, code revisions, and additional features.
After activation, navigate to the Code Snippets » + Add Snippet section from the WordPress dashboard.
Visit the snippet library to add custom code to your website by clicking the ‘Use Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ option.
If you prefer to use a pre-made code snippet, simply click the ‘Use Snippet’ button under that option.
To add a custom code snippet, paste it into the ‘Code Preview’ box.
Next, scroll down to the ‘Insertion’ section and select the ‘Auto Insert’ mode. The code will automatically run on your website once the snippet is activated.
Finally, go to the top of the page and switch the toggle from inactive to active. Then, click the ‘Update’ button to save your settings.
You have successfully added the code snippet to your WordPress site.
For more detailed guidance, check out our beginner’s guide on how to easily add custom code in WordPress.
💡 Expert Tip:If you’re hesitant about using custom code to enhance your website, don’t worry—we’re here to help.
CanadaCreate is now offering affordable Website Design Services. With a dedicated project manager, we can help bring your ideas to life.
Whether you’re starting a simple blog or a comprehensive WooCommerce store, our team will expertly set up your WordPress site. We will ensure it is optimized for success with a strong focus on SEO.
Visit our CanadaCreate Pro Services page for more details!
Common Questions About WordPress Code Snippets
Here are some frequently asked questions from our readers regarding the use of custom code and code snippets in WordPress.
How can I display code on my WordPress site?
If you’re writing blog posts on technical subjects, incorporating code snippets can enhance your content. To do this, open the page or post where you want to show the code snippet and click the ‘+’ button to add a block.
After that, simply select the Code block from the block menu and insert your custom code into that block.
Finally, click the ‘Publish’ or ‘Update’ button at the top to save your changes.
The code snippet will now be visible on your WordPress site. For step-by-step guidance, check out our tutorial on displaying code easily on your WordPress site.
How can I build a WordPress website from scratch without any coding?
If you’re looking to create a website from the ground up without writing any code, you can utilize SeedProd.
Discover the leading WordPress page builder that empowers you to design custom themes and landing pages effortlessly, without any coding skills required.
We have personally utilized this tool to create complete websites and have had excellent experiences. For more information, check out our SeedProd review.
This plugin features over 300 pre-designed templates, a user-friendly drag-and-drop builder, and a variety of advanced blocks, enabling you to create a stunning website in just a few clicks.
For more information, refer to our comprehensive tutorial on creating a landing page in WordPress.
Where can I find WordPress code snippets?
You can explore WPCode’s extensive library, which offers over 900 code snippets that can be easily integrated into your website.
If you are not using WPCode, you can also find prewritten code snippets on platforms like Stack Overflow, CodePen, or GenerateWP.
We hope this article has helped you discover the most valuable WordPress code snippets for beginners. You might also find our tutorial on adding JavaScript to WordPress pages or posts useful, along with our recommendations for the best WordPress theme builders available.
If you enjoyed this article, please subscribe to our YouTube Channel for WordPress video tutorials. You can also connect with us on Twitter and Facebook.

