The Best Code Snippets For WordPress

Stop slowing down your WordPress site with too many plugins. Use smart code snippets instead to keep your site fast, organized, and fully customizable.

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.ferdyishappy.com');
        exit;
    }
}
Ferdy Korpershoek

Written by Ferdy

Making websites since 1999 and sharing free, in-depth WordPress tutorials on YouTube since 2015.

About Ferdy

Rather
watch?

Prefer to learn by watching? Every tutorial is free, step by step, on YouTube.