1

I am (for the first time) developing a WordPress-plugin which is a simple registration-form. It sends the data to an API. At settings page you can chose to which page the user should be redirected to after the submit is successful. But the redirect is not working.

I am getting the link for the page like this:

$options = get_option('rfw_options');
$successPage = (isset($options['rfw_field_success_page']) ? esc_html($options['rfw_field_success_page']) : 'Select Page');

// some code for API call

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status == 200) { // that means if successful 
    $successLink = get_permalink(get_page_by_title($successPage));
    header("Location: '. $successLink .'");
}

Even if I set the header location like following its not working:

header("Location: https://www.google.no/");

Here you can see how I do set up my form:

function rfw_html_form_code() {
    echo '<form action="' . esc_url($_SERVER['REQUEST_URI']) . '" method="post">';
    echo '<div class="flex-container"><p>';
    _e('Fornavn (obligatorisk)', 'recman-form-widget');
    echo '<br><input type="text" name="rfw-fname" pattern="[A-Za-z\wåäöæéøâèêóòôÅÄÖÆÉØÂÈÊÓÒÔ]+" value="' . $_POST['rfw-fname'] . '"  required />';
    echo '</p>';
    echo '<p>';
    _e('Etternavn (obligatorisk)', 'recman-form-widget');
    echo ' <br /><input type="text" name="rfw-lname" pattern="[A-Za-z\wåäöæéøâèêóòôÅÄÖÆÉØÂÈÊÓÒÔ]+" value="' . $_POST['rfw-lname'] . '"  required />';
    echo '</p></div>';
}

I hope somebody can help me to solve this issue.

Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
dahlsdata-tahira
  • 359
  • 1
  • 6
  • 23
  • `header("Location: '. $successLink .'");` is syntactically wrong, `header("Location: $successLink");` will do the trick (since you are using doublequotes). But likely you have output before sending the headers, enable error-reporting `error_reporting(E_ALL); ini_set("display_errors", 1);` on the top of your file, after ` – Qirel Mar 21 '17 at 15:19
  • Also see http://stackoverflow.com/questions/6236607/redirection-not-working-in-wordpress?rq=1 and [Headers Already Sent PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Qirel Mar 21 '17 at 15:20
  • There was a time (not too sure if it applies now) that you had to have exit; on the end of header(Location:"www.blah.com") otherwise I think it would continue - maybe just in an old safari. Either way - it's a practice I still do. – Richard Housham Mar 21 '17 at 15:29

4 Answers4

0

If one of these doesn't work... You're doing something wrong

header( "LOCATION: http://example.com/" );  // Note that this will not change url / state

<head><meta http-equiv="refresh" content="2;url=http://example.com" /></head>

<script type="text/javascript"> window.location = "http://www.example.com/" </script>
0

You can actually take a reference from this thread How to make a redirect in PHP?

On the other had may be some syntax error. Please try and replace your code with header("Location: ".$successLink."");

Try dumping the variable $successLink with var_dump($successLink);. Inspect if your variable is returing the desired links from the page title or not.

You can also redirect with wp_redirect() function. Take a look around https://developer.wordpress.org/reference/functions/wp_redirect/

Community
  • 1
  • 1
Ninja
  • 428
  • 4
  • 17
  • 1
    `header("Location: ".$successLink."");` why bother with the last concatenated (empty) string here? – naththedeveloper Mar 21 '17 at 16:12
  • 1
    Lets first check what is the actual value of varaible `$successLink` by dumping it and then append it within `header()` function – Ninja Mar 21 '17 at 16:41
0

WordPress has a inbuilt function for redirection wp_redirect().

You can use it like this:

wp_redirect($successLink);
exit(); //always exit after redirect.

Hope this helps!

Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
0

Thanks for all the help but I had to work with another solution.

First of all I had to set the action-attribute of the form-element to empty.

<form action="" method="post">

My if-statement looks a little different now (I am just calling a function here now):

if ($status == 200) {

    rfw_success_redirect();

    $_POST = array();
}

The rfw_success_redirect() function looks like this:

function rfw_success_redirect()
{
    $options = get_option('rfw_options');
    $successPage = (isset($options['rfw_field_success_page']) ? esc_html($options['rfw_field_success_page']) : 'Select Page');
    $successLink = get_permalink(get_page_by_title($successPage));

    wp_enqueue_script(
        'javascript',
        plugins_url('js/javascript.js', __FILE__)
    );

    $scripts_params = array(
        'successLink' => $successLink
    );

    wp_localize_script('javascript', 'scriptParams', $scripts_params);
}
add_action('rfw_success_redirect', 'my_enqueue_scripts');

In my javascript.js I am setting window.location = scriptParams.successLink;

So I had to use javascript to redirect the page.

dahlsdata-tahira
  • 359
  • 1
  • 6
  • 23