The 15 Effective Code Snippets for WordPress

As a developer or a beginner in WordPress development, you’ll find these code snippets to be indispensable tools for enhancing your theme, plugins, and overall website performance. With WordPress’s powerful core features and API, these snippets offer endless possibilities for creating winning websites.

In this article, we’ll provide an overview of 15 essential WP code snippets carefully curated from various collections. Whether you’re working on an eCommerce platform, a blog post, or customizing your website’s sidebar, widgets, header, or footer, these code snippets will come in handy.

Learn to use, access, and apply these code snippets effectively, from sanitizing user inputs to handling errors. We’ll provide inspiring examples and sample codes every WP user must have. Let’s unlock the potential of these powerful WordPress code snippets to streamline your application development.

Explore our blog for tips on escaping and data validation, and learn how to recover WordPress passwords.

1. Remove the Admin Bar

If you want to simplify your caching system and avoid having separate codes for login vs. public users, consider disabling the admin toolbar. To turn off the toolbar for all logged-in users, add the following WP code snippet to your theme’s functions.php file:

 add_filter('show_admin_bar', '__return_false'); 

2. Display Images in Your WordPress RSS Feeds

Stay updated with your favorite websites using RSS Feeds, which notify you of new posts or products. By default, WordPress only shows the title and excerpt in the feed. To display images in your RSS feed, use the following WP code snippets.

Add this code to your theme’s functions.php file or update it inside other PHP files. It will show the featured image of the post before the content in your site’s RSS feed, allowing you to have a more visually appealing and informative feed.

// Display featured image in WordPress RSS feed

add_filter('the_content', 'display_featured_image_in_rss_feed');
function display_featured_image_in_rss_feed( $content ) {
 global $post;
 if( is_feed() ) {
 if ( has_post_thumbnail( $post->ID ) ){
 $prepend = '
' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'margin-bottom: 10px;' ) ) . '
'; $content = $prepend . $content; } } return $content; }

3. Update Your “Read More” Link

When customizing your website, it may be necessary to update the “Read More” link. With a simple hook, you can modify the WordPress default “Read More” link’s CSS, text, and other HTML attributes.

The below code snippet will allow you to customize the “Continue Reading” text or create a button using HTML. Simply update the code to match your preferences and add it to the wp-content/themes/[theme_name]/functions.php file.

 / Modifying excerpt and content more function 
 customize_read_more_link( $more ){  

 return ' '.esc_html__('Continue Reading','text_domain').' '; 

add_filter('excerpt_more', 'customize_read_more_link');
add_filter( 'the_content_more_link', 'customize_read_more_link' );

4. Post Excerpt Length

Every website is unique in terms of design and layout. Magazine blogs will have different excerpt lengths compared to small or wholesale eCommerce businesses, and there may be other reasons to customize excerpt lengths.

By default, WordPress sets the excerpt length to 55 words, but the following code snippet of WordPress allows you to set it to 24 words, or any other length you prefer.

// Change the default excerpt length in WordPress (default is 55 words)
function change_excerpt_length( $length ) {
 return 24;
add_filter( 'excerpt_length', 'change_excerpt_length', 9999 );

5. Allow Shortcodes in Widgets

By default, WordPress doesn’t allow shortcodes to be used within widgets. However, shortcodes can be very useful and it can be helpful to use them in widgets. The code below will enable you to add short codes in the text widget and have them executed.

// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

Do you like to add your branding logo to wp-admin login panel and dashboard with ease? No problem, the below code will let you add the top left logo to your dashboard. Also, you can rebuild CSS to link to any file with background-image properties.

//Adds a custom logo to the top left of the WordPress admin
function athemeart_custom_logo_wp_dashboard() {
echo "";
add_action('wp_before_admin_bar_render', 'athemeart_custom_logo_wp_dashboard');
Note: Be sure you upload an admin-icon.png at your theme’s directory.

7. SVG upload

In today’s tech world, SVG format is turning more worthy for logo files. Yes, you’ll be able to use a plugin or below code to enable this functionality.

WP doesn’t permit SVG for security issues by default. But the below snippets only allows an admin to upload SVG.

//Enable SVG upload
function athemeart_enable_svg_upload( $mimes ) {
 //Only allow SVG upload by admins
 if ( !current_user_can( 'administrator' ) ) {
 return $mimes;
 $mimes['svg'] = 'image/svg+xml';
 $mimes['svgz'] = 'image/svg+xml';
 return $mimes;
add_filter('upload_mimes', 'athemeart_enable_svg_upload');

8. XML-RPC Disable:

In general, you needn’t enable XML-RPC always and have enabled it can cause security issues. So, use the below note to disable XML-RPC and improve WordPress website security.

//Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');

9. Migrate jQuery

It’s best to delete it to take one request away from page loads. The below notes will delete jQuery Migrate from your site. After deleting, take a few of your pages to ensure the site is still functioning perfectly.

So, use it:

//Remove jQuery migrate
function athemeart_remove_jquery_migrate( $scripts ) {
 if ( !is_admin() && !empty( $scripts->registered['jquery'] ) ) {
 $scripts->registered['jquery']->deps = array_diff( $scripts->registered['jquery']->deps, ['jquery-migrate'] );
add_action('wp_default_scripts', 'athemeart_remove_jquery_migrate');

10. Hiding WordPress version:

By obscurity, the WordPress version touches again on hidden security. Its version shows on the header section of the source code by default. Simply adding the following code can remove it. Or, you may use a premium plugin like Perfmatters to hide it by a 01 click.

function wp_version_remove_version() {
return '';
add_filter('the_generator', 'wp_version_remove_version');
Aware: Source code editing can also ruin your site if not done properly. If you’re not flexible, then call the developer first.

11. File Editing Disable

When you have too many users and administrators, the security of your website can be compromised. Additionally, giving authors or contributors admin access is not a recommended practice. Unfortunately, this scenario happens frequently.

To prevent this, you can implement the following WordPress code snippets to restrict the ‘edit_themes,’ ‘edit_plugins,’ and ‘edit_files’ capabilities for all users.

 define('DISALLOW_FILE_EDIT', true); 

WP saves revisions every sixty (60) seconds by default. And you can change its value by putting a custom interval as below:

define( 'AUTOSAVE_INTERVAL', 160 );

Or, if you want to decrease the largest number or disable post revisions, settle the below statement:

define( 'WP_POST_REVISIONS', false );

And if you want to limit the largest number of revisions, instead, settle the below statement:

define( 'WP_POST_REVISIONS', 10 );

Plus, WP saves trash pages, posts, and comments for thirty (30) days; after that, it deletes permanently. You can replace its value with the below statement:

define( 'EMPTY_TRASH_DAYS', 10 );
Tip: You have the power to disable trash by turning its value to 0. But rest assured, you can’t recover contents anymore.

13. Memory Size

Sometimes you may see a message like the below:

Fatal error: Allowed Memory Size of XYZ Bytes Exhausted.

It means WP tries to allocate 40Mb for one site and 64MB for multi-site installations by default. But you can add a custom value to adopt with the below line:

define( 'WP_MEMORY_LIMIT', '128M' );

And if want you can place the largest memory limit by the below statement:

define( 'WP_MAX_MEMORY_LIMIT', '256M' );

14. Constant Updates:

It’s a crucial feature for an admin that enables keep securing their website every time.

You may disable each automatic updates by settling the below statement.

Important: To disable security updates is not a good plan, ever.

Automatic or Constant updates are not working with major releases. But you’re able to update any core by below statement:

# Disables all core updates:
define( 'WP_AUTO_UPDATE_CORE', false );

# Enables all core updates, including minor and major:
define( 'WP_AUTO_UPDATE_CORE', true );

# Default value is minor:
define( 'WP_AUTO_UPDATE_CORE', 'minor' );
Remember: If you place DISALLOW_FILE_MODS to true, each edited file will disable. And it also affects your installations and updates, including theme and plugin.

15. Security:

You can disable the file editor offered in the admin panel. The below statement will veil the Editor Appearance screen:

define( 'DISALLOW_FILE_EDIT', true );

After that, you may force to move data over SSL for any sign-in and admin session by using the below statement:

define( 'FORCE_SSL_ADMIN', true );

The next two constants permit blocking external requests and listed allowed hosts.

# External Hosts:
define( 'WP_HTTP_BLOCK_EXTERNAL', true );

# List Admitted Hosts:
define( 'WP_ACCESSIBLE_HOSTS', ',*' );


These are the most practical and useful code snippets for WordPress. If you encounter any issues, you can use these snippets with ease. If, however, the code snippets don’t provide a solution, feel free to reach out to us through the comment section below.

To enrich your expertise in WordPress, we recommend bookmarking a few tech blogs. Check out CodePen, Stack Overflow, Envato Tuts+, WordPress Codex, Udemy, freeCodeCamp, and other learning communities.

As a core development company, we are always here to support you. Simply leave a comment with your needs, and we will promptly provide the necessary information you require.

Inspire us with your love!

khurram shahzad

I'm Khurram Shahzad, a passionate technical writer specializing in WordPress, servers, HTML, CSS, and more. Hire me on platforms like Fiverr, Upwork, PeoplePerHour, Craigslist, DesignCrowd, and more. Stay updated by following me on Facebook, Twitter, Instagram, and LinkedIn. Thanks for considering me for your content needs!

You can check also