Code Snippets

7 Useful WordPress Code Snippets for Beginners

Are you seeking for WordPress code snippets to use on your site? Adding code snippets to your WordPress site enables you to create unique styles and functionalities that are not achievable with themes and plugins. Snippets can also help to improve the security and usability of your website’s admin dashboard.
In this article, we are going to give you a list of the most useful WordPress code snippets for beginners.

Why Add Code Snippets in WordPress?

If you have a WordPress website, adding some useful code snippets to your theme files or using a code snippets plugin can allow you to achieve endless customization and make your site stand out. You can use custom code to modify individual aspects of your website. For example, you may change the text selection color in WordPress by adding a simple CSS code snippet.
As a newbie, including some handy code snippets will improve your site’s performance and speed by eliminating the need for several plugins. Aside from that, snippets can help you improve your coding skills and take advantage of the WordPress community’s enormous library of code snippets, which is freely available.
Having said that, let’s look at some of the best WordPress code snippets for beginners.

1. Allow SVG File Upload​

SVG (Scalable Vector Graphics) is a file format that defines vector graphics with the XML markup language. This format allows you to magnify photographs without compromising quality.
These files are smaller and lighter than JPEG or PNG, allowing you to improve page speed. However, WordPress does not support SVG file uploads by default because SVGs can include malicious code that jeopardizes site security.
Allow SVG File Upload
Keeping this in mind, if you still wish to submit SVG files to your website, you can use the following code snippet:
 * Allow SVG uploads for administrator users.
 * @param array $upload_mimes Allowed mime types.
 * @return mixed
    function ( $upload_mimes ) {
        // By default, only administrator users are allowed to add SVGs.
        // To enable more user types edit or comment the lines below but beware of
        // the security risks if you allow 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;
 * Add SVG files mime check.
 * @param array        $wp_check_filetype_and_ext Values for the extension, mime type, and corrected filename.
 * @param string       $file Full path to the file.
 * @param string       $filename The name of the file (may differ from $file due to $file being in a tmp directory).
 * @param string[]     $mimes Array of mime types keyed by their file extension regex.
 * @param string|false $real_mime The actual mime type or false if the type cannot be determined.
    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;

You can include this code in your theme’s functions.php file or use a code snippets plugin such as WPCode. Later in the article, we will demonstrate how to do this.

2. Disable the WP Admin Bar

By default, WordPress displays an admin bar at the top of your website for all logged-in users, including subscribers, authors, editors, and other user roles. This admin bar will take them to your WordPress dashboard, where they may make changes to your site based on their user permissions.
However, it might be annoying while viewing the front end of your website because it occasionally overlaps with design components such as the header.
Disable the WP Admin Bar
disable the WP admin bar, simply add the following PHP code snippet to your WordPress site:
					/* Disable WordPress Admin Bar for all users */
add_filter( 'show_admin_bar', '__return_false' );
Upon code execution, the admin bar won’t display on the website’s front end.

3. Add Featured Images to RSS Fe​

RSS feeds allow people to receive regular updates from your WordPress site via a feed reader such as Feedly.
This can assist market your content and increase traffic to your website. You may make RSS feeds more visually appealing and improve the user experience by including featured photos or thumbnails next to the entries.
Add Featured Images to RSS Fe
To easily display post thumbnails in your RSS feeds, apply the following essential WordPress code snippet:
 * Add 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 = '<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' );

This might make your feed more intriguing and encourage users to return to your site.

4. Disable Automatic Update Emails

WordPress will send you an email notification every time it automatically updates any plugins, themes, or the core itself.
This can get really frustrating if you have many WordPress sites and are continuously receiving these messages when you access your email account.
Disable Automatic Update Emails
In that situation, you can easily prevent automatic update emails by including the following PHP code snippet on your website:
					// Disable auto-update emails.
add_filter( 'auto_core_update_send_email', '__return_false' );
// Disable auto-update emails for plugins.
add_filter( 'auto_plugin_update_send_email', '__return_false' );
// Disable auto-update emails for themes.
add_filter( 'auto_theme_update_send_email', '__return_false' );

After that, you will not receive any notifications about the plugin or theme auto updates.

5. Change ‘Howdy, Admin’ in the Admin Bar

When you log into your WordPress dashboard, you will see a ‘Howdy’ followed by your display name in the upper right corner of the screen.
This greeting may not seem natural to you, appear antiquated, or even be annoying.
Change ‘Howdy, Admin’ in the Admin Bar
To easily alter the greeting in the admin bar, simply add the following code snippet to your WordPress site:
					function wpcode_snippet_replace_howdy( $wp_admin_bar ) {
    // Edit the line below to set what you want the admin bar to display intead of "Howdy,".
    $new_howdy = 'Welcome,';
    $my_account = $wp_admin_bar->get_node( 'my-account' );
            'id'    => 'my-account',
            'title' => str_replace( 'Howdy,', $new_howdy, $my_account->title ),
add_filter( 'admin_bar_menu', 'wpcode_snippet_replace_howdy', 25 );

Once you’ve added the code, you must also include your preferred greeting next to the $new_howdy = line.

6. Disable XML-RPC

XML-RPC is a core WordPress API. It enables consumers to connect to their websites via third-party services. If you wish to administer your website with an automation tool such as Uncanny Automator or a mobile app, you must first enable XML-RPC.

However, if you do not wish to use any of these features, we recommend disabling XML-RPC to prevent hackers from accessing your website. Hackers can use these flaws to obtain your login information or start DDoS attacks. If you want to update your wordpress security click here.

To disable XML-RPC on your website, add the following code snippet:
					add_filter( 'xmlrpc_enabled', '__return_false' );

7. Change Excerpt Length

Excerpts are the first few lines of your blog entries that appear below the post titles on your WordPress homepage, blog, or archives page.
You may choose to decrease the length of your snippet to pique users’ interest and encourage them to click on the post for more information. Similarly, you can enhance the length to provide readers with more context and crucial information without requiring them to click on the post.
Change Excerpt Length
To adjust the excerpt length, simply add the following code snippet to your website.
    function ( $length ) {
        // Number of words to display in the excerpt.
        return 40;

By default, this snippet restricts the excerpt to 40 words, but you can change the value on Line 5 to whatever works best for your blog.

How to Add Code Snippets in WordPress (Easy Method)

Now that you’ve learned the most valuable WordPress code snippets for beginners, you can easily integrate them into your theme’s stylesheets or functions.php file.
However, keep in mind that even the smallest error in inputting the code can cause your site to fail and become unreachable. Furthermore, if you change themes, all of your custom code will be gone, and you will need to re-add everything.
This is why we always recommend utilizing WPCode.
How to Add Code Snippets in WordPress (Easy Method) 1
It is the greatest WordPress code snippets plugin on the market, making it extremely secure and simple to add custom code to your website. Additionally, the plugin includes a collection of over 900 code snippets, including all of those described above.
How to Add Code Snippets in WordPress (Easy Method) 2
First, install and activate the WPCode plugin. It’s worth noting that you can also utilize the free WPCode plugin. However, purchasing to the premium plugin grants you access to a cloud-based snippet library, code modifications, and other features.
Once activated, navigate to the Code Snippets » + Add Snippet page from the WordPress dashboard. This will lead you to the snippet library, where you may add custom code to your website by selecting the ‘Use Snippet’ button from the ‘Add Your Custom Code (New Snippet)’ menu.
However, if you wish to utilize a pre-made code snippet, simply click the ‘utilize Snippet’ button beneath that option.
How to Add Code Snippets in WordPress (Easy Method) 3
If you want to add a custom code snippet, simply paste it into the ‘Code Preview’ box. Then, scroll down to the ‘Insertion’ area and select ‘Auto Insert’ mode. After you activate the snippet, the code will be executed automatically on your website.
How to Add Code Snippets in WordPress (Easy Method) 4
Finally, go to the top of the page and turn the inactive switch to active. After that, simply click the ‘Update’ button to save your settings.
You’ve successfully put the code snippet to your WordPress site.
How to Add Code Snippets in WordPress (Easy Method) 5

Frequently Asked Questions About WordPress Code Snippets

How do I display code on my WordPress site?

If you create blog entries about technical issues, including code snippets can be helpful. To do so, navigate to the page/post where you want the code snippet to appear and click the ‘+’ button to add a block.
After that, just insert the Code block from the block menu and enter your custom code inside the block itself.
How do I display code on my WordPress site
Finally, use the ‘Publish’ or ‘Update’ buttons at the top to save your changes. The code snippet will now appear on your WordPress website.

Where can I get WordPress code snippets?

The WPCode library provides access to over 900 code snippets that may be quickly added to your website. If you don’t use WPCode, you can still receive prewritten code snippets from sites like Stack Overflow, CodePen, or GenerateWP.
We hope this article helped you discover the best WordPress code snippets for beginners.
Scroll to Top