
The Best Code Snippets For WordPress
When you use WordPress plugins, you can add new functions to your website. But every plugin makes your website a bit slower. Instead of using plugins, you can use one plugin in combination with code snippets. This will keep your website fast and give you a nice overview of all the functionalities you add.
In this blog post, I want to share the best code snippets you can use for your blog website.
PHP Code Snippets
Redirect Non-Users To Other Page
add_action('template_redirect', 'redirect_non_logged_in_users');
function redirect_non_logged_in_users() {
// Check if the user is not logged in and is not accessing wp-login.php or wp-admin
if (!is_user_logged_in() && !is_page('wp-login.php') && !is_admin()) {
// Redirect to the specified external URL
wp_redirect('https://ferdy.com');
exit;
}
}


Jennifer
Some people comment that the redirect non-user code doesn’t work. Here’s what I used with the plugin and it works:
add_action(‘template_redirect’, function () {
// Don’t redirect if user is logged in
if (is_user_logged_in()) {
return;
}
// Prevent redirect on login page, admin, or REST/API calls
if (is_admin() || defined(‘DOING_AJAX’) || defined(‘REST_REQUEST’) || is_login()) {
return;
}
// Set the external redirect URL
$redirect_url = ‘https://puppseas.kit.com/301e8b38df’;
// Perform redirect
wp_redirect($redirect_url);
exit;
});
ilia corthout
Hi,
the code snippet doesn’t work + it’s also not the same code as you use in your youtube video; “How to make a wordpress website 2025”..
Can you help me out?
Thanks!
Dave
the code snippet doesn’t work for me?
Robin Sharma
fedy bhai ,wo kha gya code snippet webp wala jo apne video me bataya tha ???
where’s that code snippet for converting all the images to webP?????/
Eddie
I tried the jpg convert to webp and my file sizes actually went higher and some almost doubled my file size.
Ferdy
That is not what should happen. Thanks for letting me know. You could adjust the code so that images will be compressed even better.
Shar Ali
Amazing helping support for the developer. Very easy way from you. Thanks Ferdy.
Yasir Shabbir
As a developer, I’ve been an avid follower of Ferdy since 2018. 🎥 Every single video brings fresh insights and new techniques that elevate my skills! 🚀 Your content is a constant source of inspiration, and I truly appreciate all that you do! 🙌