简体   繁体   中英

wp_redirect gives header already send error

I am trying to build a wordpress plugin that gets info from my form and puts it in muy database. This works perfectly fine but whenever i try to redirecty the page to the page where you can edit the items i get an error that the headers are already send. I tried everything from putting the get_header(); below the redirect to trying to work with hooks buth nothing works. There must be something that I am missing here.

<?php
global $wpdb;
$path='admin.php?page=my_pirazzo_locations';
$path2='admin.php?page=my_pirazzo_items';
$url=admin_url($path);
$url2=admin_url($path2);
//global $wpdb;
$userdb = $wpdb->prefix. 'users';
$getdata = $wpdb->get_results("SELECT * FROM $userdb");
//if (isset($_GET['id']))
//{
//    $id= $_GET['id'];
//    echo $id;
//}

//echo $userdb;
if (isset($_POST["submit"]) && $_POST["client"] != "" && $_POST["startdate"] != "" && $_POST["enddate"] != "") {

global $wpdb;

$userdb = $wpdb->prefix . 'users';
$table = $wpdb->prefix . "b2bdomain";
$user_id = strip_tags($_POST["client"]);
$startdate = strip_tags($_POST["startdate"]);
$enddate = strip_tags($_POST["enddate"]);
$isActive = 0;
if ($startdate < $enddate) {
    $isActive = 1;
}
$query = $wpdb->get_results("SELECT user_login FROM $userdb WHERE ID = $user_id");
$name = $query[0]->user_login;
$wpdb->insert(
    $table,
    array(
        'userid' => $user_id,
        'isActivated' => $isActive,
        'name' => $name,
        'start_time' => $startdate,
        'end_time' => $enddate,
    )
);

$url = 'admin.php?page=my_pirazzo_edit';
ob_clean();
ob_start();
wp_redirect($url);




};

?>

  <!-- This file should primarily consist of HTML with a little bit of PHP. -->
<div class="wrap">
<h1>beheer zone</h1>
<div class="postbox">
    <div class="meta-th">
        <h2>nieuwe zone toevoegen</h2>
    </div>
    <div class="meta-td">

        <form method="post" name="cleanup_options" action="">
            <fieldset>
                <label for="client">Selecteer een Klant</label>
                <select name="client" id="client">
                    <?php foreach($getdata as $data){ ?>
                        <option value="<?php echo $data->ID ?>"><?php echo $data->user_login ?></option>
                    <?php } ?>
                </select>

            </fieldset>
            <fieldset>
                <label for="startdate">Startdatum</label>
                <input type="date" name="startdate" >
                <label for="enddate">Einddatum</label>
                <input type="date" name="enddate" >
            </fieldset>



            <?php submit_button('bewaar de zone', 'primary','submit', TRUE); ?>

        </form>
    </div>

</div>
<hr>
<div class="postbox">
    <h2>voeg locatie's toe</h2>
    <a href="<?php echo $url ?>"><button class="button-primary">voeg een locatie toe</button></a>
</div>
<hr>
<div class="postbox">
    <h2>voeg Producten toe</h2>
    <a href="<?php echo $url2 ?>"><button class="button-primary">voeg een locatie toe</button></a>
</div>

You can try sending your post data in wp_loaded hook. You can only use wp_redirect before header content is sent to the browser. So, instead of processing form in the template file. Moving them into wp_loaded hook might help you.

Please check below function for reference. Refer for https://cdn.tutsplus.com/wp/authors/legacy/tom/2012/09/25/wordpress-core-load-lifecycle.png

add_action( 'wp_loaded', 'pirazzo_edit_form' );
function pirazzo_edit_form(){
  if( isset($_POST["submit"]) && $_POST["client"] != "" && $_POST["startdate"] != "" && $_POST["enddate"] != "") {
    //YOUr additional code here

    // process form, and then Redirect
    $url = 'admin.php?page=my_pirazzo_edit';
    wp_redirect($url);
    exit();
  }
}

Put the above code in your functions.php file to test it out.

Also, are you new to WordPress ? There are few things that hit me when i read your code properly. Now, there are many functions that comes built in with WordPress core for example querying the users. I see you have used wpdb object which you don't need to do.

This below is for your user selection field. Following is how we can query users directly using get_users function. Refer https://codex.wordpress.org/Function_Reference/get_users

<select name="client" id="client">
   <?php $all_users = get_users(); ?>
   <?php foreach($all_users as $data){ ?>
   <option value="<?php echo $data->ID ?>"><?php echo $data->first_name .' ' .$data->last_name; ?></option>
 <?php } ?>
</select>

Similarly, i do not see any nonce fields in your form submission. Please use nonce for security. Please check following on how we can implement this in WordPress.

<form method="post" name="cleanup_options" action="">
<?php
//Please check why this is needed https://codex.wordpress.org/Function_Reference/wp_nonce_field
wp_nonce_field( '_nonce_demo_action_field', '_demo_action_name' );
?>

Add the above code wp_nonce_field() below form or anywhere inside form. You are posting nonce secret data. So, we will be validating this when we are intercepting the post request sent like in the following:

//Functions.php
add_action( 'wp_loaded', 'pirazzo_edit_form' );
function pirazzo_edit_form(){
        if( isset($_POST["submit"]) && wp_verify_nonce( $_POST['_demo_action_name'], '_nonce_demo_action_field' ) ) {
        global $wpdb;

        $userdb = $wpdb->prefix . 'users';
        $table = $wpdb->prefix . "b2bdomain";
        $user_id = strip_tags($_POST["client"]);
        $startdate = strip_tags($_POST["startdate"]);
        $enddate = strip_tags($_POST["enddate"]);
        $isActive = 0;
        if ($startdate < $enddate) {
            $isActive = 1;
        }
        $query = $wpdb->get_results("SELECT user_login FROM $userdb WHERE ID = $user_id");
        $name = $query[0]->user_login;
        $wpdb->insert(
            $table,
            array(
                'userid' => $user_id,
                'isActivated' => $isActive,
                'name' => $name,
                'start_time' => $startdate,
                'end_time' => $enddate,
                )
            );

        $url = 'admin.php?page=my_pirazzo_edit';
            wp_redirect( $url );
            exit;
        }
    }

This above should be added in functions.php file. I have not touched anything inside the code. So, you will need to refactor it yourself.

Hope you understand the process i have written. Let me know on the comments if there are any confusions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM