4,9 based on 300 reviews

Trusted by 1500+ businesses and student of all shapes and sizes

ferdyaiwhite
ferdyaiwhite

About

Tutorials

Recommended

• Tools

• Themes

• plugins

Blog

Knowledge

The Best WordPress Code Snippets to Replace Plugins (2026)

Stop loading unnecessary plugins. Use these powerful WordPress code snippets to add features, improve speed, and customize your site without…

Ferdy
WordPress code snippets tutorial
}

4 Minutes To Read

Ferdy

June 30, 2024

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.ai/');
        exit;
    }
}

13 Comments

  1. 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! ????

    Reply
  2. Shar Ali

    Amazing helping support for the developer. Very easy way from you. Thanks Ferdy.

    Reply
  3. Eddie

    I tried the jpg convert to webp and my file sizes actually went higher and some almost doubled my file size.

    Reply
    • 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.

      Reply
  4. 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?????/

    Reply
  5. Dave

    the code snippet doesn’t work for me?

    Reply
  6. 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!

    Reply
  7. 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;
    });

    Reply
    • Terry

      The snippet is not working, nor is the one done by Jennifer. I was using the information from your How to make a wordpress website youtube video. Please can you respond.

      Reply
  8. Terry

    Hi Ferdy

    I tried your code snippet for Redirect Non-Users To Other Page as you suggested in your YouTube video. I followed all your instructions, but it is not working. I’m trying to build my website, but it is showing live, which I don’t really want.
    Your help would be greatly appreciated.
    Thanks

    Reply
  9. Terry

    Hi Ferdy
    Ignore my previous comment as it appears to be working correctly now. Thank you for all your help.

    Reply
  10. Robert

    Nice one! I was looking for this one.

    Reply
  11. Lina

    I am learning to build a webpage and the snippet is not working.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *