Actually, there are 2 issues in your code:
- You were using PHP custom file name instead of
menu_slug
. - You were not using action and method attributes of form tag. When you were submitting form WordPress redirecting into admin.php?on=Turn+Registration+On which is not a valid page.
add_submenu_page
function 4th parameter must be unique menu slug of your plugin not the custom PHP file.
Please have a look at the add_submenu_page
function.
I have updated the code and tested. it working fine.
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_menu_page( 'Registration Switch', 'Registration', 'manage_options', 'page-toggle', 'registration_admin_page', 'dashicons-welcome-write-blog', 6 );
}
function registration_admin_page() {
?>
<div class="wrap">
<h2>Registration Information</h2>
<h2>Toggle Registration</h2>
<form action="<?php echo admin_url('admin.php?page=page-toggle'); ?>" method="post">
<input type="submit" name="on" value="Turn Registration On" class="button-secondary" onclick="return confirm('Are you sure you want to enable registration?')" />
<br>
<br>
<input type="submit" name="off" value="Turn Registration Off" class="button-secondary" onclick="return confirm('Are you sure you want to disable registration?')" />
</form>
<?php
if( isset( $_REQUEST['off'] )) {
$my_post3 = array('ID' => 90,'post_status' => 'draft');
wp_update_post( $my_post3 );
$my_post4 = array('ID' => 83,'post_status' => 'publish');
wp_update_post( $my_post4 );
echo "<h2>Registration is turned OFF.</h2>";
} elseif (isset( $_REQUEST['on'] )) {
$my_post1 = array('ID' => 90,'post_status' => 'publish');
wp_update_post( $my_post1 );
$my_post2 = array('ID' => 83,'post_status' => 'draft');
wp_update_post( $my_post2 );
echo "<h2>Registration is turned ON.</h2>>";
}
?>
</div>
<?php
}
?>