Request a Quote
Web Dev Notes.

Exploring WordPress, Code & Creativity

Sharing knowledge on WordPress, PHP, and modern web development.

Improve WordPress Speed with These Simple Tweaks

Site speed is not just about user experience — it's also a major ranking factor in Google’s algorithm. If your WordPress site feels sluggish, don’t rush to install another performance plugin. You can achieve fast, smooth performance with just a few clean, developer-friendly tweaks — no extra bloat required.


1. Disable Unused WordPress Features (Like XML-RPC and Emojis)

WordPress loads many features by default that most sites don’t need — like XML-RPC, emoji scripts, and oEmbed. Disabling them reduces HTTP requests and cleans up your HTML output.


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

		// Remove emoji scripts
		remove_action('wp_head', 'print_emoji_detection_script', 7);
		remove_action('wp_print_styles', 'print_emoji_styles');
				

2. Host Google Fonts Locally

External font requests can slow down page load, especially from Google Fonts. Instead, download the fonts and serve them from your server. This also improves GDPR compliance.

Steps:

  • Go to google-webfonts-helper
  • Select your font and weights
  • Download the font files and copy the generated CSS
  • Put fonts in your theme’s /fonts/ folder
  • Link them via wp_enqueue_style()

3. Use Native Lazy Loading for Images

Native lazy loading is supported by all major browsers. Just add loading="lazy" to your image tags:


		<img src="image.jpg" loading="lazy" alt="..." />
				

If your theme doesn’t do this automatically, you can add it dynamically:


		// Add lazy loading to images in content
		add_filter('the_content', function($content) {
		  return preg_replace('/<img(.*?)src=/', '<img loading="lazy"$1src=', $content);
		});
				

4. Defer JavaScript for Better Performance

Defer non-critical JavaScript so the browser can prioritize HTML and CSS rendering. Use this filter in functions.php to apply defer on enqueued scripts:


		add_filter('script_loader_tag', function($tag, $handle) {
		  if (!is_admin() && !in_array($handle, ['jquery'])) {
			return str_replace(' src', ' defer src', $tag);
		  }
		  return $tag;
		}, 10, 2);
				

5. Minify CSS & JS Manually Using Gulp or Webpack

Instead of relying on minification plugins, use build tools to compress and concatenate your files during development.

Gulp Example:


		// gulpfile.js
		const gulp = require('gulp');
		const cleanCSS = require('gulp-clean-css');
		const uglify = require('gulp-uglify');
		const concat = require('gulp-concat');

		// Minify CSS
		gulp.task('css', function () {
		  return gulp.src('src/css/*.css')
			.pipe(concat('style.min.css'))
			.pipe(cleanCSS())
			.pipe(gulp.dest('dist/css'));
		});

		// Minify JS
		gulp.task('js', function () {
		  return gulp.src('src/js/*.js')
			.pipe(concat('scripts.min.js'))
			.pipe(uglify())
			.pipe(gulp.dest('dist/js'));
		});
				

This gives you full control over what gets optimized — and removes the dependency on plugins like Autoptimize or WP Rocket.

Why These Tweaks Matter

Performance plugins are great for non-technical users, but they often add unnecessary scripts and are hard to debug. These manual techniques ensure you know exactly what runs on your site.

Bottom Line: With just a few lines of code and basic build tools, you can dramatically improve your WordPress speed, reduce load times, and give users (and Google) a much better experience — all without any extra plugins.

Essential PHP Snippets for Every WordPress Developer (Copy-Paste Ready)

Whether you’re building themes, child themes, or extending plugins, having a toolkit of reliable PHP snippets can save hours of time. Below are some of my go-to code blocks that I reuse in nearly every project — and now you can too.

1. Disable WordPress Emojis

Removes extra scripts and styles injected by default in WordPress:


		remove_action('wp_head', 'print_emoji_detection_script', 7);
		remove_action('wp_print_styles', 'print_emoji_styles');
				

2. Add Custom Class to Menu Items

Adds a specific class to every navigation menu item, useful for styling:


		add_filter('nav_menu_css_class', function($classes, $item) {
		  $classes[] = 'custom-class';
		  return $classes;
		}, 10, 2);
				

3. Set Custom Excerpt Length

Control how many words are shown in excerpts without a plugin:


		add_filter('excerpt_length', function($length) {
		  return 20; // set your desired word count
		});
				

4. Redirect Users After Login Based on Role

Send different users to different pages after login:


		add_filter('login_redirect', function($redirect_to, $request, $user) {
		  if (isset($user->roles) && is_array($user->roles)) {
			if (in_array('administrator', $user->roles)) {
			  return admin_url();
			} else {
			  return home_url('/dashboard');
			}
		  }
		  return $redirect_to;
		}, 10, 3);
				

5. Remove WordPress Version Number

Helps improve security by hiding the WP version in HTML output:


		remove_action('wp_head', 'wp_generator');
				

6. Add Featured Image Support (Theme Dev)

If your theme doesn't yet support thumbnails, add this to functions.php:


		add_theme_support('post-thumbnails');
				

7. Disable Gutenberg (Block Editor) for Posts

Force the Classic Editor for specific post types:


		add_filter('use_block_editor_for_post_type', function($use_block_editor, $post_type) {
		  if ($post_type === 'post') {
			return false;
		  }
		  return $use_block_editor;
		}, 10, 2);
				

8. Automatically Add alt Attribute to Images

Ensure images have alt tags based on post title (for accessibility and SEO):


		add_filter('the_content', function($content) {
		  global $post;
		  return preg_replace('/<img(.*?)alt=""/', '<img$1alt="' . get_the_title($post) . '"', $content);
		});
				

9. Remove Query Strings from Static Resources

Helps caching tools by removing versioning from asset URLs:


		add_filter('script_loader_src', 'remove_ver', 9999);
		add_filter('style_loader_src', 'remove_ver', 9999);

		function remove_ver($src) {
		  if (strpos($src, '?ver='))
			$src = remove_query_arg('ver', $src);
		  return $src;
		}
				

10. Limit Post Revisions

Reduce database bloat by limiting the number of saved revisions:


		define('WP_POST_REVISIONS', 5); // place in wp-config.php
				

Where to Place These:

  • Most snippets go inside your theme’s functions.php file
  • For better structure, consider turning them into a mu-plugin

All of these snippets are tested and compatible with the latest versions of WordPress. They're perfect for developers who want fast, clean, and customizable code without relying on third-party plugins.

Copy, paste, tweak — and ship better WordPress projects.

How to Create a Lightweight WordPress Theme from Scratch

If you’re tired of bloated themes with hundreds of unused options, building your own lightweight theme is the perfect solution. It gives you full control, faster performance, and clean, maintainable code.

Step 1: Basic Theme Structure

Create a new folder in /wp-content/themes/, e.g., mytheme, and inside it, create these essential files:

  • style.css
  • index.php
  • functions.php
  • header.php
  • footer.php

Step 2: Setup style.css

This file tells WordPress it’s a theme:


		/*
		Theme Name: My Custom Theme
		Author: Your Name
		Version: 1.0
		*/
				

Step 3: Register Scripts & Styles in functions.php

Always enqueue CSS/JS the WordPress way — not with hardcoded <link> tags.


		function mytheme_enqueue() {
		  wp_enqueue_style('main-css', get_stylesheet_uri());
		  wp_enqueue_script('main-js', get_template_directory_uri() . '../assets/js/main.js', [], false, true);
		}
		add_action('wp_enqueue_scripts', 'mytheme_enqueue');
				

Step 4: Create header.php and footer.php

Split your layout into reusable template parts. For example, header.php might look like this:


		<!DOCTYPE html>
		<html <?php language_attributes(); ?>>
		<head>
		  <meta charset="<?php bloginfo('charset'); ?>">
		  <?php wp_head(); ?>
		</head>
		<body <?php body_class(); ?>>
		  <header>
			<h1><a href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a></h1>
		  </header>
				

In footer.php, include:


		  <footer>
			<p>© <?php echo date('Y'); ?> MySite. All rights reserved.</p>
		  </footer>
		  <?php wp_footer(); ?>
		</body>
		</html>
				

Step 5: Build Your index.php

This is the default template WordPress falls back to:


		<?php get_header(); ?>

		<main>
		  <?php
			if (have_posts()) :
			  while (have_posts()) : the_post();
				the_title('<h2>', '</h2>');
				the_content();
			  endwhile;
			else :
			  echo '<p>No content found</p>';
			endif;
		  ?>
		</main>

		<?php get_footer(); ?>
				

Step 6: Use WordPress Hooks & Filters

Clean themes rely on actions and filters to enhance functionality without cluttering templates.

  • add_action() – hook into execution flow
  • add_filter() – change or modify data before output

Example: change excerpt length globally:


		add_filter('excerpt_length', function() {
		  return 20;
		});
				

Step 7: Make it Responsive and Accessible

  • Use semantic HTML5: <main>, <article>, <nav>
  • Include ARIA attributes if needed
  • Add a responsive meta viewport tag
  • Test with screen readers and keyboard navigation

Final Thoughts

By building your own theme, you avoid extra bloat, reduce page load times, and get exactly what your project needs. It’s perfect for performance-focused WordPress developers who prefer writing clean, custom code.

Once you’re done, zip your theme folder and upload via the WordPress dashboard or FTP. Congratulations — you now have a fast, custom, developer-friendly theme!

WordPress Functions.php Hacks You’ll Actually Use

The functions.php file is like a toolbox for your theme. Used wisely, it can add powerful features, speed up performance, and remove plugin dependency — all with clean, native PHP.

Here are tried-and-tested snippets that you’ll actually use in real-world projects.

1. Hide WordPress Version for Security

This removes the generator meta tag (e.g., WordPress 6.4.3) to prevent bots from targeting known vulnerabilities:


		remove_action('wp_head', 'wp_generator');
				

2. Disable Gutenberg for Posts (Use Classic Editor)

If you prefer the Classic Editor for certain post types:


		add_filter('use_block_editor_for_post_type', function($is_enabled, $post_type) {
		  if ($post_type === 'post') {
			return false;
		  }
		  return $is_enabled;
		}, 10, 2);
				

3. Control Excerpt Length & Read More Text

Set a global excerpt length and customize the "read more" suffix:


		// Change excerpt length
		add_filter('excerpt_length', function() {
		  return 25; // words
		});

		// Change "read more" text
		add_filter('excerpt_more', function() {
		  return '...';
		});
				

4. Auto Remove Unused Image Sizes

Prevent WordPress from generating unnecessary image sizes:


		function disable_extra_image_sizes() {
		  remove_image_size('medium_large');
		  remove_image_size('1536x1536');
		  remove_image_size('2048x2048');
		}
		add_action('init', 'disable_extra_image_sizes');
				

5. Add Custom Post Types (Without Plugins)

Clean way to register a CPT like "Portfolio":


		function register_portfolio_post_type() {
		  register_post_type('portfolio', [
			'labels' => [
			  'name' => 'Portfolio',
			  'singular_name' => 'Portfolio Item',
			],
			'public' => true,
			'has_archive' => true,
			'rewrite' => ['slug' => 'portfolio'],
			'supports' => ['title', 'editor', 'thumbnail'],
			'show_in_rest' => true,
		  ]);
		}
		add_action('init', 'register_portfolio_post_type');
				

6. Add Custom Dashboard Widget

Display notes, tips, or support links on the admin dashboard:


		function my_dashboard_widget() {
		  echo "<p>Welcome to your custom dashboard!</p>";
		}
		function register_my_widget() {
		  wp_add_dashboard_widget('custom_help_widget', 'My Dashboard Widget', 'my_dashboard_widget');
		}
		add_action('wp_dashboard_setup', 'register_my_widget');
				

7. Disable XML-RPC and REST API Access

Block unused API endpoints to reduce attack surface:


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

		// Restrict REST API
		add_filter('rest_authentication_errors', function($result) {
		  if (!is_user_logged_in()) {
			return new WP_Error('rest_disabled', 'REST API restricted.', ['status' => 403]);
		  }
		  return $result;
		});
				

8. Remove Unwanted Scripts (Like jQuery Migrate)

Useful for speed-conscious developers who manage their own JS stack:


		add_filter('wp_default_scripts', function($scripts) {
		  if (!is_admin() && isset($scripts->registered['jquery'])) {
			$scripts->registered['jquery']->deps = array_diff(
			  $scripts->registered['jquery']->deps,
			  ['jquery-migrate']
			);
		  }
		});
				

Where to Add These Snippets:

  • functions.php of your active theme
  • OR wrap them in a custom plugin for reusability

These hacks are clean, lightweight, and follow WordPress best practices. Use them to build faster, more secure, and more maintainable websites — without relying on third-party plugins for basic tweaks.

Less bloat. More control. Pure WordPress.

Let’s Chat Now
Hi 👋
How can I help you today?