Wp_UPDATE_POST出现“内部服务器错误”

时间:2017-03-05 作者:mattthetechguy

我正在为我的一个网站编写一个插件,当按下按钮时,该插件将切换两页状态。因此,当按下ON按钮时,它将在发布另一页的同时将一页转换为草稿,而按下OFF按钮则将另一页转换为草稿。

我遇到的问题是,当您按下按钮时,会出现“内部服务器错误”页面。你需要刷新两次,然后才能进入我想要的页面。我怎样才能解决这个问题。

我的代码

<?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
}
?>
我怀疑是因为我打电话wp_update_post 有两次,虽然我对编码很在行,所以不知道如何修复它。

1 个回复
最合适的回答,由SO网友:Syed Fakhar Abbas 整理而成

实际上,您的代码中有两个问题:

您使用的是PHP自定义文件名,而不是menu_slug.add_submenu_page 函数4th参数必须是插件的唯一菜单段,而不是自定义PHP文件。

请看一下add_submenu_page 作用

我已经更新了代码并进行了测试。它工作得很好。

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
}
?>

相关推荐