7 Essential WordPress Tips to Improve SEO and Customise Your Site Easily
WordPress development comes with various challenges, from managing multisite setups to customising the interface and optimising performance. This listicle dives into practical tips for enhancing SEO and customising the WordPress interface.
(I’ve gathered these trusty code snippets from some of my older blog posts. Some might be a bit old, but they might still come in handy.)
SEO and Search Engine Optimisation Tips
Optimising your site for search engines is crucial. These tips focus on controlling what search engines can access, helping you maintain a clean SEO profile.
1. Add Custom Rules to robots.txt in WordPress
When search engines like Google crawl your site, they can index everything by default, including files and sections you might not want to appear in search results. For instance, you might not want certain file types, such as PDFs, to be indexed by Google.
This snippet allows you to add custom rules to the robots.txt
file in WordPress, preventing search engines from indexing specific files. By controlling what is indexed, you maintain a cleaner and more relevant SEO profile.
function matt_watson_disallow_pdf_index($output, $public) {
if ('1' !== $public || ! is_string( $output )) {
return $output;
}
$output .= "Disallow: *.pdf
";
return $output;
}
add_filter('robots_txt', 'matt_watson_disallow_pdf_index', 0, 2);
2. Add /blog to Post Slug
For larger websites, organising content effectively can significantly improve SEO and user experience. One way to do this is by adding /blog
to your post slugs, which helps structure your URLs more clearly, particularly on content-heavy sites.
This snippet allows you to customise the post slug in WordPress by adding /blog
, improving the overall content hierarchy and making it easier for search engines to understand the structure of your site.
function matt_watson_add_blog_to_post_slug() {
if ( post_type_exists( 'post' ) ) {
return;
}
register_post_type(
'post',
[
'rewrite' => [ 'slug' => 'blog' ],
'supports' => [ 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ]
]
);
}
add_action( 'init', 'matt_watson_add_blog_to_post_slug', 1 );
3. Add Custom Rewrite Tags and Customise Author URLs with a Dynamic Prefix
Customising your URLs can significantly improve SEO by making them more descriptive. This snippet adds a custom rewrite tag, %faculty%, allowing you to modify URLs for posts (in this case, journal posts) to include both an author’s name and another dynamic value, like a category or department. This makes URLs more informative, which helps search engines better understand the context of the page.
In the example, %faculty% is replaced by a static value ('my-test-faculty'), but it could be dynamically set from post metadata. The %author% tag is automatically replaced with the author’s nicename, creating a URL structure like:
/my-test-faculty/john/journal/
This improves readability for users and search engines, benefiting your SEO.
// Add a custom rewrite tag for 'faculty'
function matt_watson_register_rewrite_tags() {
add_rewrite_tag( '%faculty%', '([^&]+)' ); // We add the faculty tag for URL rewrites
}
add_action( 'init', 'matt_watson_register_rewrite_tags' );
// Modify the permalink structure for the 'journal' post type
function matt_watson_post_type_link( $post_link, $post, $leavename, $sample ) {
// Check that the post is of type 'journal' and return early if not
if ( 'journal' !== get_post_type( $post ) ) {
return $post_link;
}
// Get the author's nicename to insert into the URL
$authordata = get_userdata( $post->post_author );
if ( ! $authordata ) {
return $post_link;
}
$author = $authordata->user_nicename;
// Replace the %author% tag in the URL with the actual author's nicename
$post_link = str_replace( '%author%', $author, $post_link );
// Replace the %faculty% tag with a static value or a dynamic value (this can be replaced with post meta or other logic)
// For now, using a static test value 'my-test-faculty'
$post_link = str_replace( '%faculty%', 'my-test-faculty', $post_link );
// Example of the resulting URL structure:
// /my-test-faculty/john/journal/
return $post_link;
}
add_filter( 'post_type_link', 'matt_watson_post_type_link', 10, 4 );
Customisation Tips for the WordPress Interface
Customising the WordPress interface can improve usability for site admins and users alike. These tips will help you add custom styles, controls, and settings to WordPress with ease.
4. Disable the Admin Bar for Non-Admins
The WordPress admin bar can be distracting for non-admin users, especially if it’s not necessary for their experience. Disabling the admin bar for these users can simplify the frontend interface and reduce clutter.
This snippet provides a solution for hiding the admin bar for non-admin users, with a guard to ensure administrators and backend users still see it.
function matt_watson_remove_admin_bar() {
if ( current_user_can( 'administrator' ) || is_admin() ) {
return;
}
show_admin_bar( false );
}
add_action( 'after_setup_theme', 'matt_watson_remove_admin_bar' );
5. Custom Conditional Statements for WordPress Stylesheets
Sometimes, you need to load specific stylesheets based on certain conditions, such as the browser version. This is particularly useful for older browsers like Internet Explorer.
This snippet enables conditional loading of stylesheets using the style_loader_tag
filter. A guard ensures the custom logic only applies to the desired stylesheet.
function matt_watson_custom_style_loader_tag( $tag, $handle ) {
if ( 'ie-8-plus' !== $handle ) {
return $tag;
}
return "<!--[if gt IE 8]><!-->$tag<![endif]-->";
}
add_filter( 'style_loader_tag', 'matt_watson_custom_style_loader_tag', 10, 2 );
6. Extend the WordPress Customiser Using JavaScript
The WordPress Customiser is a powerful tool for making theme adjustments, but you may need to extend its functionality to create custom sections or panels.
This snippet demonstrates how to extend the Customiser using JavaScript, providing more flexibility in tailoring it to fit your theme or plugin’s needs.
const { customize } = wp;
customize.bind( 'ready', function () {
if (!customize.panel) {
return;
}
const panelKey = 'mattwatson-customizer-panel';
const sectionKey = 'mattwatson-customizer-section';
customize.panel.add( new customize.Panel( panelKey, {
title: __( 'Matt Watson Panel', 'mattwatson' ),
priority: 1000,
} ) );
customize.section.add( new customize.Section( sectionKey, {
title: __('Matt Watson Section', 'mattwatson'),
panel: panelKey,
} ) );
});
7. Add a Custom Settings Link to Your WordPress Plugin
Adding a settings link to your plugin’s listing in the WordPress admin interface can make it easier for users to find and adjust the plugin’s settings.
This snippet shows how to add a custom settings link directly in the plugin list, improving the user experience by reducing the number of steps needed to access settings.
function matt_watson_plugin_settings_link( $links ) {
if ( ! is_array( $links ) ) {
return $links;
}
$label = esc_html__( 'Settings', 'mattwatson' );
$slug = 'mattwatson_plugin_settings';
array_unshift( $links, "<a href='$slug'>$label</a>" );
return $links;
}
add_action( 'plugin_action_links_' . plugin_basename(__FILE__), 'matt_watson_plugin_settings_link', 10 );
By applying these SEO and interface customisation tips, you can enhance your WordPress site’s performance, user experience, and overall flexibility. Whether you're managing large amounts of content or improving the backend interface, these snippets provide efficient solutions for common WordPress challenges.
Subscribe to my newsletter
Read articles from Matt Watson directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Matt Watson
Matt Watson
Hello there! I’m Matt Watson — a developer, father, and husband, remotely coding from the charming landscapes of Yorkshire, UK. I’ve been crafting wonderful things with WordPress since 2006 and spinning webs (the good kind) since 1996 — yes, I survived the dial-up era! Solving (and occasionally committing) WordPress-based crimes, I juggle code by day and dad jokes by night. Remote worker since 2014, I’ve mastered the art of debugging in my slippers while keeping the coffee industry in business.