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