0

I'm trying in the WP boilerplate in the public area to redirect after form submittion. But i'm getting a error.

Cannot modify header information - headers already sent by (output started at

public\partials\filename.php:

public function redirect_to_dashboard($value) {
if ( isset( $_POST) ) {
    $redirect = get_permalink( $value );
    wp_redirect($redirect);
    exit;
}} 

I calling the function with a valid permalink (same file public\partials\filename.php):

redirect_to_dashboard($escape_room_data->id)

I also added in the includes\filename-plugin.php the add_action

$this->loader->add_action( 'wp-loaded', $plugin_public, 'redirect_to_dashboard' );

Calling

global $wpdb;

if (!empty($_POST)) {
$code = $_POST['code'];

$like = "'%" . $wpdb->esc_like( $code ) . "%'";
$result = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_teptickets WHERE code = %s", $wpdb->esc_like($code)));

if (!empty($result)) {
    if ($result->used == false) {
        $table_name = $wpdb->prefix . "teptickets";
        $wpdb->update( $table_name, array(
            'used' => '1'
        ), array(
            'id' => $result->id
        ) ); 
    } else {
        echo 'code already used';
        $escape_room_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_tepescapes WHERE id = %s", $wpdb->esc_like($result->escape_room)));
        redirect_to_dashboard($escape_room_data->id);
    }
} else {
    print_r('code not valid');
}}

Thank for helping!

MrY
  • 9
  • 2
  • Does this help you? https://wordpress.stackexchange.com/questions/12535/which-hook-should-be-used-to-add-an-action-containing-a-redirect – Howard E Nov 14 '21 at 20:58
  • where do i need to put the action? add_action( 'template_redirect', 'wpse12535_redirect_sample' ); – MrY Nov 14 '21 at 21:12
  • You should show how you're calling this `redirect_to_dashboard($escape_room_data->id) ` – Howard E Nov 14 '21 at 22:49
  • @HowardE i added the code, i hope it usefull for you – MrY Nov 15 '21 at 16:55
  • When/how do you use this `redirect_to_dashboard($escape_room_data->id)` at least show that part of the code. How could someone recreate your issue without knowing this. – Howard E Nov 15 '21 at 16:58
  • @HowardE what code do you need more? The function and call is in the same file. The question is still how can i do a redirect in WP, because i getting the headers already in use. – MrY Nov 16 '21 at 15:21
  • Answer can be found here: https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php/8028987#8028987 – MrY Nov 16 '21 at 15:29

0 Answers0