Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#39820 closed defect (bug) (invalid)

[Resolved] Can't change header status code response

Reported by: skarabeq's profile skarabeq Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.2
Component: General Keywords:
Focuses: Cc:

Description

After update wordpress from 4.6.1 to 4.7.2 I was unpleasantly surprised.In ajax response like this:

<?php
function post_contact_form($data)
{
    $json_return = array();
    if(!isset($data['name'])){
    $error_status_header = 422;
    status_header($error_status_header);
    $json_return['error'] =  __("Error. Please fill your name.");
    }
    else{
    $json_return['success'] = __("Form submitted.");
    }
    echo json_encode($json_return);
    wp_die();

}
add_action('wp_ajax_post_contact_form', 'post_contact_form');
add_action('wp_ajax_nopriv_post_contact_form', 'post_contact_form');

When I try to change status header to 422 , I just got http status header 200. In this case my fron-end logic was broked, because I check the ajax status code in ajax error handle. Also I tried with status code 400 the result is same. Please fix it because this bug has created extremely inconvenience to the functionality of my site.

Change History (7)

#1 @Compute
6 years ago

You should be able to send arguments to wp_die(). Response code can be sent to the title argument.

https://developer.wordpress.org/reference/functions/wp_die/
https://developer.wordpress.org/reference/functions/_ajax_wp_die_handler/

if ( is_int( $args ) ) {
	$args = array( 'response' => $args );
} elseif ( is_int( $title ) ) {
	$args  = array( 'response' => $title );
	$title = '';
}

So if you use:

wp_die( '', 422 );

Or:

wp_die( 'Message', 'Title, array( 422 ) );

You should be able to change the response code.

Last edited 6 years ago by Compute (previous) (diff)

#2 @skarabeq
6 years ago

@Compute thank you for support.

#3 @Compute
6 years ago

Did it work? Can we close this ticket?

#4 @skarabeq
6 years ago

  • Resolution set to worksforme
  • Status changed from new to closed
  • Summary changed from Can't change header status code response to [Resolved] Can't change header status code response

Yes, it works! Thanks!

#5 @flixos90
6 years ago

  • Milestone Awaiting Review deleted

#6 @SergeyBiryukov
6 years ago

  • Resolution changed from worksforme to invalid
  • Summary changed from [Resolved] Can't change header status code response to Can't change header status code response

#7 @skarabeq
6 years ago

  • Summary changed from Can't change header status code response to [Resolved] Can't change header status code response
Note: See TracTickets for help on using tickets.