
WordPress is much more flexible than many people realise. While plugins and page builders make it easy to add functionality to a website, there are situations where writing a few lines of PHP is a cleaner and more efficient solution.
Whether you’re developing custom themes, building plugins or maintaining existing WordPress websites, knowing how to work with WordPress functions and hooks can save you time and give you greater control over your projects.
In this article, we’ll explore 10 useful WordPress PHP code snippets that can help you handle common development tasks without installing additional plugins.
1. Disable WordPress Comments Completely
If you’re building a website that doesn’t require comments, you can disable the comment functionality using the following snippet.
// Disable support for comments and trackbacks
function disable_comments_support() {
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
}
add_action('admin_init', 'disable_comments_support');
// Close comments and pings on the frontend
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
What it does:
- Removes comment support from registered post types in the administration area.
- Prevents new comments from being submitted.
- Hides existing comments on the frontend.
This is useful for business websites, portfolio websites and landing pages that don’t need a commenting system.
2. Add a Custom Shortcode
WordPress shortcodes allow you to execute custom functionality directly inside posts, pages and other supported content areas.
Here’s an example of how to create a simple shortcode that displays the current year.
function display_current_year() {
return date('Y');
}
add_shortcode('current_year', 'display_current_year');
You can now use the shortcode anywhere WordPress supports shortcodes:
[current_year]
It will automatically display the current year.
This is particularly useful for dynamic copyright notices and other content that needs to update automatically.
3. Redirect Users After Login
Sometimes you may want to redirect users to a particular page immediately after they log in.
For example, you might want customers to go to their account page instead of the WordPress dashboard.
function custom_login_redirect($redirect_to, $request, $user) {
if (isset($user->roles) && is_array($user->roles)) {
if (in_array('customer', $user->roles, true)) {
return home_url('/my-account/');
}
}
return $redirect_to;
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);
What it does:
The function checks the user’s role after login. If the user has the customer role, they are redirected to the My Account page.
Other users retain the default redirect behaviour.
4. Change the Default Excerpt Length
WordPress uses a default excerpt length that may not suit your website’s design.
You can customise the number of words displayed using the excerpt_length filter.
function custom_excerpt_length($length) {
return 25;
}
add_filter('excerpt_length', 'custom_excerpt_length', 999);
This changes the default excerpt length to 25 words.
You can adjust the number to suit your blog layout.
5. Add a Custom Body Class
WordPress automatically generates CSS classes for the body element. You can extend these classes to make styling specific pages or sections easier.
function add_custom_body_class($classes) {
if (is_page('contact')) {
$classes[] = 'contact-page';
}
return $classes;
}
add_filter('body_class', 'add_custom_body_class');
You can now target the contact page using CSS:
.contact-page {
background-color: #f5f5f5;
}
This approach is useful when applying page-specific styling without editing theme templates.
6. Change the WordPress Login Logo URL
By default, clicking the WordPress login logo takes users to WordPress.org.
You can change this destination to your own website.
function custom_login_logo_url() {
return home_url();
}
add_filter('login_headerurl', 'custom_login_logo_url');
This is useful when building websites for clients and creating a more consistent branded login experience.
7. Remove the WordPress Version Number
WordPress outputs its version number in certain frontend locations. You can remove the generator tag using the following snippet.
remove_action('wp_head', 'wp_generator');
This removes the WordPress generator meta tag from the document head.
Note: This is a minor hardening measure, not a substitute for keeping WordPress, themes and plugins updated.
8. Redirect the Author Archive to the Homepage
If your website doesn’t use author archive pages, you may want to redirect visitors away from them.
function redirect_author_archive() {
if (is_author()) {
wp_safe_redirect(home_url(), 301);
exit;
}
}
add_action('template_redirect', 'redirect_author_archive');
This checks whether the current request is an author archive and redirects visitors to the homepage.
Use this only if author archives are not needed for your website’s content or SEO strategy.
9. Add a Custom Image Size
WordPress allows developers to register custom image sizes for use in themes.
For example, you might need a specific thumbnail size for blog cards.
function register_custom_image_sizes() {
add_image_size('blog-card', 600, 400, true);
}
add_action('after_setup_theme', 'register_custom_image_sizes');
To retrieve the image using the registered size:
the_post_thumbnail('blog-card');
The true parameter enables hard cropping to the specified dimensions.
Important: Newly registered image sizes generally apply to newly uploaded images. Existing images may need thumbnail regeneration.
10. Redirect Users to a Custom Page After Logout
You can also control where users are sent after logging out of WordPress.
function custom_logout_redirect() {
return home_url('/login/');
}
add_filter('logout_redirect', 'custom_logout_redirect');
In this example, users are redirected to a custom login page after logging out.
Make sure the /login/ page exists on your website before using this snippet.
Where Should You Add These Code Snippets?
There are several ways to add custom PHP functionality to WordPress.
1. Child theme’s functions.php
You can add snippets to your active child theme’s functions.php file. This is suitable for functionality closely related to the theme.
2. Custom plugin
For functionality that should remain active even when changing themes, creating a custom plugin is generally the better approach.
3. Code management plugins
Plugins such as WPCode allow you to add and manage custom PHP snippets without directly editing theme files.
Final Thoughts
These 10 WordPress PHP snippets cover several common development tasks, from managing comments and user redirects to registering custom image sizes.
Although many of these tasks can also be accomplished using plugins, understanding how to implement them with PHP gives you more control over your WordPress projects.
As you become more familiar with WordPress hooks, filters and functions, you’ll find it easier to build custom functionality and develop more efficient solutions.
Have a useful WordPress PHP snippet of your own? Share it in the comments!