skip to Main Content

I have created an under repair status for my template. My code works correctly without any problems.

My only problem with this code is that if the site administrator activates this code from within the customization and does not deactivate this code after making the desired changes and exits his account, the site will no longer be accessible.

And to fix this problem, he must disable the repair mode through the host.

Now, I want to create a mode so that in case of such a problem, only the site manager can disable this code by entering his email or username as well as his password, so that he does not have to do it through the host.

I wrote my code as follows:

function custom_maintenance_mode_page() {
        if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
             
            $text = 'The site is being updated. please wait.'; 
            $text2 = 'To deactivate, please enter your email and password (the email and password of the site administrator)';
    
            echo '<html>';
            echo '<body>';
            echo '<div style="text-align:center;margin-top:100px;">';
            echo '<p class="txt-edit_themes">' . esc_html( $text ) . '</p>';
            echo '<p class="txt-edit_themes">' . esc_html( $text2 ) . '</p>';
            echo '<p><input class="login-user-log-field" type="text" name="log" id="user_Login" placeholder="Email or username"></p>';
            echo '<p><input class="login-user-log-field" type="password" name="pwd" id="user_Pass" placeholder="Password"></p>';
            echo '<p><input id="login-user-log_submit" type="submit" name="submit" value="To deactivate"></p>';
            echo '</div>';
            echo '</body>';
            echo '</html>';
            die(); 
        }
    }
    add_action( 'get_header', 'custom_maintenance_mode_page' );

Now I just want to create a condition so that if the email and password of the site administrator are correct, the repair mode will be disabled.

Please help me to create such a state because I don’t know what to do in this part.

2

Answers


  1. Here is an example of how this could work. This code allows the user to log in and checks whether it has the necessary permissions to disable repair mode. If they do, it will disable the repair mode (as a plugin) and redirect the user to the homepage.

    function custom_maintenance_mode_page() {
        if (isset($_POST['submit_deactivate'])) {
            $user_login = $_POST['log'];
            $user_pass = $_POST['pwd'];
            $creds = array(
                'user_login'    => $user_login,
                'user_password' => $user_pass,
                'remember'      => false
            );
            $user = wp_signon( $creds, false );
            if ( is_wp_error( $user ) ) {
                $error = $user->get_error_message();
            } else {
                if (current_user_can( 'edit_themes' )) {
                    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
                    $plugin = plugin_basename( __FILE__ );
                    deactivate_plugins( $plugin );
                    wp_redirect( home_url() );
                    exit;
                } else {
                    $error = 'You do not have permission to deactivate repair mode.';
                }
            }
        }
    
        if ( (!current_user_can( 'edit_themes' ) || !is_user_logged_in()) ) {
            $text = 'The site is being updated. please wait.'; 
            $text2 = 'To deactivate, please enter your email and password (the email and password of the site administrator)';
            ?>
    
            <html>
                <body>
                    <div style="text-align:center;margin-top:100px;">
                        <p class="txt-edit_themes"><?php echo esc_html( $text )?></p>
                        <p class="txt-edit_themes"><?php echo esc_html( $text2 )?></p>
                        <?php
                        if (isset($error)) {
                            echo '<p class="error-edit_themes">' . $error . '</p>';
                        }
                        ?>
                        <form name="loginform" id="loginform" method="post">
                            <p><input class="login-user-log-field" type="text" name="log" id="user_Login" placeholder="Email or username"></p>
                            <p><input class="login-user-log-field" type="password" name="pwd" id="user_Pass" placeholder="Password"></p>
                            <p><input id="login-user-log_submit" type="submit" name="submit_deactivate" value="To deactivate"></p>
                        </form>
                    </div>
                </body>
            </html>
            <?php
            die(); 
        }
    }
    add_action( 'get_headers', 'custom_maintenance_mode_page' );
    
    Login or Signup to reply.
  2. The code first checks if the current user is logged in and has permission to edit themes. If not, it displays the maintenance mode page.

    If the user is logged in and submits the form, the code then gets the email and password from the form. It then uses the wp_get_user_by() function to get the user object for the email address.

    If the user object exists and the password matches, the code then disables maintenance mode by setting the maintenance_mode transient to 0. It then redirects the user to the home page.

    function custom_maintenance_mode_page() {
        if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
            $text = 'The site is being updated. please wait.';
            $text2 = 'To deactivate, please enter your email and password (the email and password of the site administrator)';
    
            echo '<html>';
            echo '<body>';
            echo '<div style="text-align:center;margin-top:100px;">';
            echo '<p class="txt-edit_themes">' . esc_html( $text ) . '</p>';
            echo '<p class="txt-edit_themes">' . esc_html( $text2 ) . '</p>';
            echo '<p><input class="login-user-log-field" type="text" name="log" id="user_Login" placeholder="Email or username"></p>';
            echo '<p><input class="login-user-log-field" type="password" name="pwd" id="user_Pass" placeholder="Password"></p>';
            echo '<p><input id="login-user-log_submit" type="submit" name="submit" value="To deactivate"></p>';
            echo '</div>';
            echo '</body>';
            echo '</html>';
            die();
        }
    
        if ( isset( $_POST['submit'] ) ) {
            $email = $_POST['log'];
            $password = $_POST['pwd'];
    
            $user = wp_get_user_by( 'email', $email );
    
            if ( $user && $user->user_pass === $password ) {
                // Disable maintenance mode
                set_transient( 'maintenance_mode', 0, 0 );
    
                // Redirect to the home page
                wp_redirect( home_url() );
                die();
            }
        }
    }
    add_action( 'get_header', 'custom_maintenance_mode_page' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search