Why Use Code Snippets?
Plugins can slow down your WordPress site, but code snippets let you add custom functionality without extra bloat. By adding small pieces of PHP, CSS, or JavaScript directly, you keep your site fast and organized.
Here’s why snippets are great:
- ✔ Improve site speed (no heavy plugins needed)
- ✔ Keep customizations safe (won’t disappear after theme updates)
- ✔ Easy to manage (enable/disable snippets with one click)
How to Add Code Snippets to WordPress
Step 1: Install the Free “Code Snippets” Plugin
- Go to Plugins > Add New in WordPress
- Search for “Code Snippets” and install the plugin
- Activate it
Now, you’ll see a “Snippets” tab in your dashboard.
7 Useful WordPress Code Snippets
1. Convert Images to WebP (Faster Loading)
Automatically convert uploaded JPEG/PNG images to WebP for better performance.
PHP Snippet:
add_filter('wp_handle_upload', 'convert_to_webp');
function convert_to_webp($file) {
$image = wp_get_image_editor($file['file']);
if (!is_wp_error($image)) {
$image->save($file['file'], 'image/webp');
}
return $file;
}
How to use:
- Go to Snippets > Add New
- Paste the code, name it “Convert Images to WebP”, and activate
2. Remove “Thank You for Creating with WordPress” Footer
Replace or remove the default admin footer text.
PHP Snippet:
function remove_footer_text() {
return ''; // Leave empty to remove
}
add_filter('admin_footer_text', 'remove_footer_text');
3. Enable Duplicate Posts & Pages
Duplicate any post/page without a plugin.
PHP Snippet:
function duplicate_post_as_draft() {
global $wpdb;
if (!current_user_can('edit_posts')) return;
if (isset($_GET['post'])) {
$post_id = $_GET['post'];
$post = get_post($post_id);
$new_post = array(
'post_title' => $post->post_title . ' (Copy)',
'post_status' => 'draft',
'post_type' => $post->post_type
);
$new_post_id = wp_insert_post($new_post);
wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
exit;
}
}
add_action('admin_action_duplicate_post_as_draft', 'duplicate_post_as_draft');
How to use:
After activating, hover over a post and click “Duplicate”.
Final Thoughts
Using code snippets is a smart way to:
- ✅ Speed up WordPress (fewer plugins = better performance)
- ✅ Add custom features without breaking your site
- ✅ Keep everything organized in one place
Try these snippets today and see the difference!
Need more snippets? Check out My blog for more useful codes and tips.
Have questions? Drop them in the comments! 🚀