Learn how to restrict editors’ login in WordPress by time.

Have you ever needed to limit the access time of some users on your WordPress site?

In this tutorial, I’ll show you how to restrict editors’ login (or any other role you want) in WordPress to a specific time, ensuring more security and control over who accesses your site at specific times.

This WordPress function checks if the user trying to log in is an editor and if the current time is between 9:00 AM and 2:00 PM, Monday to Friday. If these conditions are met, the editor is automatically logged out, redirected to the homepage, and the login process is terminated. You can refine this functionality to redirect to a page containing a notice or other content.

In the functions.php file, insert the following function:

/**
 * Checks if the user role is in the restricted roles array and if the current time is between 9am and 2pm, Monday to Friday.
 *
 * @return void
 */
function restrict_login_editor() {
    $current_user = wp_get_current_user();

    if (in_array('editor', (array) $current_user->roles)) {
        $current_time = current_time('H:i');
        $day_of_week = date('N');  // Get the day of the week number (1 for Monday, 2 for Tuesday, etc.)

        // If the current time is between 9am and 2pm and it's a weekday (Monday to Friday)
        if ($current_time >= '09:00' && $current_time <= '14:00' && $day_of_week >= 1 && $day_of_week <= 5) {
            // Log out the editor, redirect to the homepage, and exit the login process
            wp_logout();
            wp_redirect(home_url());
            exit;
        }
    }
}

// Add action to check the time and day of the week at login
add_action('wp_login', 'restrict_login_editor');

With this simple tutorial, you’ve learned how to restrict editors’ login in WordPress based on the current time. This functionality can be useful to ensure that certain users access the site only during the desired hours, increasing security and control over access to your WordPress site.

If you prefer not to use code, there is a plugin that performs the same action: User Blocker.

Create the foundation for themes, plugins, and blocks in 10 seconds with just one command.

Do you want to create themes, plugins, or blocks but feel lost on where to start? There’s a WP-CLI command (and if you don’t know what WP-CLI is, that’s okay—ask me in the comments to create an article explaining it) that simplifies the development process and ensures a consistent structure!

The wp scaffold is a tool within WP-CLI (WordPress Command Line Interface) that allows you to instantly generate standard code structures for various WordPress components. This functionality is particularly useful for creating themes, plugins, and Gutenberg blocks.

In this article, we’ll explore the utilities of wp scaffold and how WordPress developers can make the most of this tool.

What is wp scaffold?

wp scaffold is a WP-CLI command that simplifies the creation of standard files and directories for themes, plugins, and blocks. It follows a convention over configuration approach, speeding up the development process and allowing developers to focus more on the specific content of their project. And the best part: everything is already following WordPress standards! No mistakes!

Basic Syntax

The basic syntax of the wp scaffold command is as follows:

wp scaffold <type> <name>
  • <type>: The type of structure to be created (e.g., “theme”, “plugin”, “block”).
  • <name>: The name of the theme, plugin, or block to be created.

Let’s explore some specific utilities of wp scaffold for different types of projects.

1. Creating WordPress Themes

To create a WordPress theme using wp scaffold, you can use the following command:

wp scaffold theme <theme-name>

This command will create the basic structure of a WordPress theme in the current directory or a directory specified by the user.

2. Developing WordPress Plugins

To create a plugin, the command is similar:

wp scaffold plugin <plugin-name>

This will create the necessary files and directories to start developing a WordPress plugin.

3. Building Gutenberg Blocks

For those interested in creating blocks for the Gutenberg editor, the wp scaffold command also offers support:

wp scaffold block <block-name>

This will generate the basic structure for a Gutenberg block, allowing you to start development immediately.

Additional Tips

In addition to themes, plugins, and blocks, there are other functionalities for “wp scaffold”:

  • Child Theme: wp scaffold child-theme
  • Post Types: wp scaffold post-type
  • Taxonomies: wp scaffold taxonomy

Conclusion

wp scaffold is a powerful tool for WordPress developers who want to speed up the process of creating themes, plugins, and blocks. By adopting this convention over configuration approach, you save time and ensure a consistent and standardized structure in your projects.

For more detailed information, consult the WordPress documentation on wp scaffold: https://developer.wordpress.org/cli/commands/scaffold/.


You can read the full article in Portuguese here.