1

I am coding a plugin for one of my sites which will toggle two pages statuses when a button is pressed. So when the ON button is pressed it will turn one page to draft while publishing the other, and vice versa with the OFF button.

The problem I am having is that when you press the button it bring you to a "Internal Server Error" page. You need to refresh that twice before it brings you to the page I want it to bring me to. How can I fix this.

My Code

<?php
/*
Plugin Name: Page Toggle
Description: Toggle page status
Version: 0.1
Author: Author
*/

add_action( 'admin_menu', 'my_admin_menu' );

function my_admin_menu() {
    add_menu_page( 'Registration Switch', 'Registration', 'manage_options', 'registration.php', 'registration_admin_page', 'dashicons-welcome-write-blog', 6  );
}

if( isset( $_REQUEST['on'] )) {
    $my_post1 = array('ID' => 8,'post_status' => 'publish',);
    wp_update_post( $my_post1 );
    $my_post2 = array('ID' => 66,'post_status' => 'draft',); 
    wp_update_post( $my_post2 );
    echo "<h2>Registration is turned ON.</h2> <p><a href='#'>Go Back</a></p>";
}

if( isset( $_REQUEST['off'] )) {
    $my_post3 = array('ID' => 8,'post_status' => 'draft',); 
    wp_update_post( $my_post3 );
    $my_post4 = array('ID' => 66,'post_status' => 'publish',); 
    wp_update_post( $my_post4 );
    echo "<h2>Registration is turned OFF.</h2> <p><a href='#'>Go Back</a></p>";
}

function registration_admin_page() {
    ?>
    <div class="wrap">
        <h2>Registration Information</h2>
        <h2>Toggle Registration</h2>
        <form>
            <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>
    </div>
    <?php
}
?>

I suspect it is because I call wp_update_post twice, although I am fairly noob with coding so don't know how to fix it.

2

Actually, there are 2 issues in your code:

  1. You were using PHP custom file name instead of menu_slug.
  2. 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
}
?>
  • Thanks for the help! I am really noob when it comes to coding. – mattthetechguy Mar 5 '17 at 23:52
  • I am getting a "Sorry, you are not allowed to access this page." when I click on the buttons. Why is that? – mattthetechguy Mar 5 '17 at 23:53
  • Actually, I think I have figured it out. Thanks for the help! – mattthetechguy Mar 5 '17 at 23:55
  • I have updated the code. Let me know if you are still getting the same error. My mistake I were using incorrect menu slug. admin.php?page=st-test it must be admin.php?page=page-toggle – Syed Fakhar Abbas Mar 5 '17 at 23:59
  • Yeah, I figured that out and fixed it. All is working well now! Thanks again for the help. – mattthetechguy Mar 6 '17 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.