Croco Black Friday 2024 banner element
Copy discount code:
bfcroco2024
Days
Hours
Minutes
Croco Black Friday 2024 banner element
Help Center
7 Best Code Snippets Plugins for WordPress: Customize Your Website However You Want
mejcross
Marcus Cross
Technical writer
Updated on
Useful Resources

7 Best Code Snippets Plugins for WordPress: Customize Your Website However You Want

WordPress is an excellent website platform that works great straight out of the box. However, you may sometimes want to add customizations of your own. One way you can modify your WordPress site without installing lots of plugins is to use code snippets. A code snippets plugin is the best way to add code snippets.

Yes, I know that sounds like a contradiction – installing a plugin so you don’t have to use plugins. But one code snippet plugin can replace several other plugins, thus reducing the overall number of plugins required.

Table of Contents

What Are Code Snippets?

Code snippets are small segments of code that can be easily integrated into a website or application. They make it possible to add functionality or modify existing features with ease. 

Code snippets play a significant role in the development process, from customizing a site’s appearance to adding new features not included in the default installation.

In WordPress, code snippets can be added using a plugin or editing the theme’s functions.php file. This provides developers with a wide range of options for customizing their sites. Using code snippets, a developer can quickly and easily add new functionality to their site without writing complex code from scratch.

7 Useful Code Snippets Examples

Code snippets can be used in many ways. Here are some examples.

NOTE:

Before making any changes to your website, back it up. Also, I cannot be held responsible if the following code harms your website.

Protect your website from attacks

Add this code to your functions.php file to reject malicious URL requests:

global $user_ID; if($user_ID) {
    if(!current_user_can('administrator')) {
        if (strlen($_SERVER['REQUEST_URI']) > 255 ||
            stripos($_SERVER['REQUEST_URI'], "eval(") ||
            stripos($_SERVER['REQUEST_URI'], "CONCAT") ||
            stripos($_SERVER['REQUEST_URI'], "UNION+SELECT") ||
            stripos($_SERVER['REQUEST_URI'], "base64")) {
                @header("HTTP/1.1 414 Request-URI Too Long");
                @header("Status: 414 Request-URI Too Long");
                @header("Connection: Close");
                @exit;
        }
    }
}

If you do not need the search feature on your WordPress site, you can remove it entirely with this code snippet. The custom function nullifies the search feature, including the search bar in your sidebar and menu. This can be helpful if you have a low-spec server and your site does not require searching. To implement this, add the code to the functions.php file.

function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

Let contributors upload images

By default, WordPress restricts contributor accounts from uploading images. To allow contributors to upload images without granting additional rights, add the following PHP code snippet to your functions.php file:

if ( current_user_can('contributor') && !current_user_can('upload_files') )
     add_action('admin_init', 'allow_contributor_uploads');      
     function allow_contributor_uploads() {
          $contributor = get_role('contributor');
          $contributor->add_cap('upload_files');
     }

Hide the WordPress admin bar

The WordPress Admin Bar is a useful feature that provides quick access to key functions, such as adding new posts and pages. However, if you don’t need it and want to remove it, you can do so by pasting this code snippet into your functions.php file:

// Remove the admin bar from the front end
add_filter( 'show_admin_bar', '__return_false' );

Display popular posts

It can be useful to display your most popular posts. If any of your readers liked a certain bunch of your posts, new visitors might appreciate seeing them too. To achieve this, add this PHP code to functions.php:

function count_post_visits() {
    if( is_single() ) {
        global $post;
        $views = get_post_meta( $post->ID, 'my_post_viewed', true );
        if( $views == '' ) {
            update_post_meta( $post->ID, 'my_post_viewed', '1' );   
        } else {
            $views_no = intval( $views );
            update_post_meta( $post->ID, 'my_post_viewed', ++$views_no );
        }
    }
}
add_action( 'wp_head', 'count_post_visits' );

Afterward, you can insert the following code wherever you wish to display the popular posts in your template files:

$popular_posts_args = array(
    'posts_per_page' => 3,
    'meta_key' => 'my_post_viewed',
    'orderby' => 'meta_value_num',
    'order'=> 'DESC'
);
$popular_posts_loop = new WP_Query( $popular_posts_args );
  while( $popular_posts_loop->have_posts() ):
    $popular_posts_loop->the_post();
    // Loop continues
endwhile;
wp_reset_query();

If you want to add popular posts with JetEngine Listing Grid, here is the video with quick instructions:

Paginate your website

Good pagination is a useful way for users to browse a website. Instead of using “previous” and “next” links, pagination can be added to your content using a code snippet in the functions.php file of your WordPress theme.

global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 ) {
     // get the current page
     if ( !$current_page = get_query_var('paged') )
          $current_page = 1;
     // structure of "format" depends on whether we're using pretty permalinks
     $format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/';
     echo paginate_links(array(
          'base' => get_pagenum_link(1) . '%_%',
          'format' => $format,
          'current' => $current_page,
          'total' => $total,
          'mid_size' => 4,
          'type' => 'list'
     ));
}

Create shortcode for PayPal donation

You can use a code snippet to create a shortcode to make it easier for your website’s visitors to donate using the PayPal Donate function. To do this, simply insert the following code in the functions.php file:

function donate_shortcode( $atts, $content = null) {
global $post;extract(shortcode_atts(array(
'account' => 'your-paypal-email-address',
'for' => $post->post_title,
'onHover' => '',
), $atts));
if(empty($content)) $content='Make A Donation';
return '<a href="<https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation> for '.$for.'" title="'.$onHover.'">'.$content.'</a>';
}
add_shortcode('donate', 'donate_shortcode');
Then you can place the [donate] shortcode anywhere on your website, like this: [donate]Your Text[/donate].

7 Best Code Snippets Plugins

When adding code snippets to your WordPress site, it is better to use a plugin rather than editing the functions.php file directly. This approach is safer and easier to manage if you add many code snippets for various functions.

Although many code snippet plugins are available, some are better than others. To save you the trouble of browsing through all of them, I have narrowed the list to the seven best code snippet plugins for WordPress.

Code Snippets

🏆 Best for adding AI-powered code snippets.

code snippets wordpress plugin homepage

Code Snippets is the first and original WordPress plugin for adding code snippets, used by almost a million websites. It’s an AI-enhanced code snippet manager with cloud syncing and intuitive customization features to help you create a pixel-perfect website.

Code Snippets provides a clean and intuitive platform for managing all your website’s code customizations. With its centralized dashboard, you can effortlessly add, edit, or create code snippets, ensuring that every aspect of your website is perfectly crafted.

Pricing: free, with a Premium version starting from $39.90 per year.

Key features:

  • Ability to generate, manage, and organize an infinite number of PHP, HTML, CSS, and JS snippets, adapting to both novice and seasoned developers.
  • AI-powered coding – create code from scratch effortlessly and ensure accuracy with AI-driven Snippet Verification for optimal performance.
  • Advanced cloud integration for seamless snippets sync across sites, Cloud Backup, Public Snippets search, safeguarding code in a Private Vault, and utilizing Snippet Bundles instead of plugins.
  • It integrates with your preferred website builder, whether it’s Elementor, Gutenberg, or any other, for a smooth and familiar development experience.
  • Regular updates, delivering the latest features and optimizations for an ever-evolving development landscape.

WordPress.org rating: 4.8 out of 5 stars – from over 425 reviews.

Pros:

  • Can substitute other similar plugins combined.
  • Minimal impact on page speed.
  • No PHP errors, warnings, notices, or JavaScript issues.
  • Lightweight and secure.

Cons:

  • Not found; if you have any feedback, please share it in the comments below.
Pro tip

Get 5% off for Code Snippets Pro on all plans with the Croco5 discount code.

WPCode

🏆 Best for adding code snippets to any part of your website.

wpcode plugin

WPCode is the most popular WordPress plugin for adding code snippets, used by over a million websites. It allows you to easily add code snippets like Google Analytics, custom CSS, and Facebook Pixel to your WordPress site’s header and footer, as well as other areas of your website, without having to edit your theme’s functions.php file. You can also use WPCode to insert custom PHP, JavaScript, CSS, HTML, and text snippets with full conditional logic and code priority support.

Pricing: Free, with a Premium version starting from $49 per year.

Key features:

  • Code Snippets Library – simplify your site and reduce bloat with advanced snippets written by experts, tested across various setups.
  • Smart Conditional Logic allows advanced personalization options using conditional rules to limit where snippets are loaded.
  • Private Cloud Library to save snippets and reuse them across all your sites with the WPCode plugin.
  • Advanced Code Revisions – track changes made to your code with clear attribution, so you can easily return to what works.
  • Target specific web pages with precision using pre-made locations and rules.

WordPress.org rating: 4.9 out of 5 stars – from over 1,000 reviews.

Pros:

  • Lightweight.
  • Secure.
  • Enables you to get rid of other unnecessary plugins.
  • Easy to use – for example, you can easily toggle between functions.
  • Excellent support.

Cons:

  • The free version doesn’t have all the features of the Premium version, although it is still excellent.

🏆 Best for those worried they might put code snippets in the wrong places.

head and footer wordpress code manager plugin

The Header Footer Code Manager plugin allows you to easily add scripts and styles to any page or post without limitations. You can manage which pages or posts the script loads on, and it also supports custom post types. This plugin allows you to load scripts on specific posts or pages or the latest posts. 

You can control the position of the script loading on the page: head, footer, before content, or after content. You can even load scripts only on desktops or mobile devices, enabling or disabling either. If you need to add code manually anywhere, you can use shortcodes. Also, each snippet can be labeled for easy identification.

Pricing: Free, with a Premium version starting from $35 per year.

Key features:

  • Add scripts and styles to any page or post with no limits.
  • Manage which pages or posts the script loads on.
  • Supports custom post types.
  • Load scripts on specific posts or pages or the latest posts.
  • Control the position of the script loading on the page: head, footer, before content, or after content.
  • Load scripts only on desktops or mobile devices, enabling or disabling either.
  • Use shortcodes to add code manually anywhere.
  • Label each snippet for easy identification.
  • The plugin logs the user who added and last edited the snippet, along with the date and time of the edit.

WordPress.org rating: 4.9 out of 5 stars.

Pros:

  • Simple and lightweight plugin.
  • Easy to use.
  • Prevents you from accidentally placing code snippets in the wrong place.

Cons:

  • Some handy features are only available in the Premium version.

Simple Custom CSS

🏆 Best for easily adding custom CSS code.

free simple custom css for wordpress

The Simple Custom CSS plugin is user-friendly and allows administrators to easily add custom CSS styles that override the default styles of both plugins and themes. It was designed to meet the needs of those who want to add their CSS to their WordPress site, and the styles created using this plugin will remain even if the theme is changed.

Pricing: Free.

Key features:

  • Supports AMP.
  • Provides Customizer Control with live preview.
  • Offers a valuable code syntax highlighter.
  • Includes code linting for error checking.
  • Doesn’t require configuration.
  • Features a simple interface built on native WordPress UI.
  • Doesn’t require complicated database queries.
  • Comes with thorough documentation.
  • Allows Administrator access on WP Networks (Multisite).

WordPress.org rating: 4.8 out of 5 stars.

Pros:

  • Simple and lightweight.
  • Easy to use.
  • Works well.

Cons:

  • Some users have reported that it slows down their site.

🏆 Best for adding custom HTML, CSS, and JavaScript snippets.

header footer code plugin

The Head & Footer Code plugin allows you to add custom code to your site’s head, body, and footer sections. You can set site-wide custom content for each section or add article-specific custom code for individual posts. Additionally, you can set category-specific custom code for each section. You can choose which post types will have enabled article-specific custom fields and decide whether the article-specific code will replace or be appended to the site-wide code.

The plugin also allows you to choose the priority of printed custom code to head/body/footer sections, view if an article has defined any article-specific custom code, and see site-wide and article-specific entries in page source code wrapped to comments if you have set the WP_DEBUG constant to true in your wp-config.php file. Also, this plugin is supported by multisite installations and is PHP 8-ready.

Pricing: Free.

Key features:

  • Add custom content to the website’s head, body, and footer sections.
  • Set article-specific custom code for head, body, and footer sections.
  • Set category-specific custom code for head, body, and footer sections.
  • Choose the priority of printed custom code for the head, body, and footer sections.
  • Choose which post types will have enabled article-specific head, body, and footer fields.
  • View if an article has defined any article-specific custom code.
  • PHP 8-ready.

WordPress.org rating: 5 out of 5 stars.

Pros:

  • Very easy to use.
  • Works very well.
  • Great support.

Cons:

  • None that I could find.

Post Snippets

🏆 Best for creating custom shortcodes and reusable content.

post snippets plugin for wordpress

The Post Snippets plugin enables you to create a library of HTML, PHP code, or frequently used text snippets that can be easily inserted into your posts and pages. Predefined variables can be used to replace parts of the snippet when inserted. All snippets are accessible via a button in the post editor’s Visual mode. The snippet can be inserted as-is or as a shortcode to make updating easier. If you insert the snippet as a shortcode, PHP code is supported.

Pricing: Free, with a Premium version starting at $29 per year.

Key features:

  • Snippets can be easily inserted from a button in the post editor.
  • Custom shortcodes can be created with this plugin.
  • Shortcode snippets can be processed as PHP code if desired.
  • Snippets are accessible through buttons in both the visual and HTML editors.
  • User-friendly ‘Manage Snippets’ page for adding, editing, and removing snippets.
  • Snippets can include custom variables for insertion.
  • Snippets can be imported and exported between sites.
  • Complete documentation is available from the plugin’s help panel.
  • Data created by the plugin is automatically removed from the WordPress database upon uninstallation.

WordPress.org rating: 4.7 out of 5 stars.

Pros:

  • Very easy to use.
  • Doesn’t slow down your site.
  • Excellent support.

Cons:

  • You cannot sort the snippets.

Snippet Shortcodes

🏆 Best for creating reusable shortcodes for code or any website content.

If you need to use the exact text or HTML snippet multiple times on your website but only want to change it in one place, consider using the Snippet Shortcodes plugin. This plugin allows you to create your shortcodes and assign content to them. Using the standard WordPress editor, you can add text, HTML, JavaScript, images, or other elements. This provides the advantage of creating a shortcode once and reusing it throughout your site.

Pricing: Free, with a Premium version costing £4.99 GBP.

Key Features:

  • Generate a shortcode and use it in multiple areas throughout your website.
  • Revise the shortcode in one location to alter it throughout the entire website.
  • Add additional parameters to expand the functionality of your shortcodes.
  • Access free and premium helper shortcodes to enhance your experience.
  • Enjoy support for multiple sites.
  • Use shortcodes within WordPress menu titles.

WordPress.org Rating: 4.7 out of 5 stars.

Pros:

  • Easy to use.
  • Saves you a lot of time if you need to reuse the same content or code on many pages.
  • Excellent support.

Cons:

  • Some users have reported errors with the plugin, but these were from a few years ago, so it’s difficult to know whether those problems still exist or have since been fixed.

FAQ

How can I add code snippets to my WordPress website?

To add code snippets to your WordPress website, insert them into your theme’s functions.php file or use a plugin. Popular plugins include WPCode, Header Footer Code Manager, Simple Custom CSS, Head & Footer Code, Post Snippets, and Snippet Shortcodes. Each plugin has specialized functionality for adding custom code, CSS, HTML, and shortcode snippets to your site.

Where are WordPress code snippets stored?

WordPress code snippets can be stored in various locations, depending on how they are added to the site. Code snippets added through a plugin, such as WPCode or Header Footer Code Manager, will be stored within the plugin’s settings.
Code snippets added directly to a theme’s functions.php file will be stored within the theme’s files. Snippets created using the Post Snippets plugin will be stored within the plugin’s library and can be easily accessed and inserted into posts and pages using shortcodes.

What are the risks associated with adding code snippets to my WordPress website?

There are risks associated with adding code snippets to your WordPress website. If the code is not written correctly or is malicious, it can cause your website to crash or be hacked. Therefore, it is essential only to add code snippets from trusted sources and to test them thoroughly before adding them to your website.

How can I remove code snippets from my WordPress website?

To remove a code snippet from your WordPress website, delete the code from the file or plugin where it was added.

Summary

Code snippet plugins provide a more accessible and safer way to add custom code snippets to your WordPress website. Today, I have looked at seven of the best code snippet plugins for WordPress, and here is a recap:

  • WPCode – for adding code snippets to any part of your website.
  • Header Footer Code Manager – for those worried they might put code snippets in the wrong places.
  • Simple Custom CSS – for easily adding custom CSS code.
  • Head & Footer Code – for adding custom HTML, CSS, and JavaScript snippets.
  • Post Snippets – for creating custom shortcodes and reusable content.
  • Snippet Shortcodes – for creating reusable shortcodes for code or any website content.