0

Ive got a very simple function that simply sends mail using values from the form.

Everything works, tthe values get passed through to the wp_mail and it gets sent out no issue.

But for some reason

setcookie('my_cookie', $usersemail, 1 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );

Even if I do

function set_user_cookie() {
    $school = "Some Value";
    setcookie('default_school', $school, time()+3600);
}
add_action( 'init', 'set_user_cookie');

That doesnt seem to work, the full function is below:

function deliver_mail() {

        // if the submit button is clicked, send the email
        if ( isset( $_POST['cookieformSubmit'] ) ) {

            // sanitize form values
            $name    = sanitize_text_field( $_POST["cookieForm_name"] );
            $email   = sanitize_email( $_POST["cookieForm_email"] );
            $number = sanitize_text_field( $_POST["cookieForm_number"] );
            $downloadurl = sanitize_text_field( $_POST["cookie_download_url"] );

            $message = '';
            $message .= $name;
            $message .= $email;
            $message .= $number;
            $message .= $downloadurl;

            // get the blog administrator's email address
            $to = 'myemail@myemail.com';

            $headers = "From: $name <$email>" . "\r\n";



            // If email has been process for sending, display a success message
            if ( wp_mail( $to, $subject, $message, $headers ) ) {
                echo '<div>';
                echo '<p>Thanks for contacting me, expect a response soon.</p>';
                echo '</div>';
                setcookie('hbp_cookie_doc_download', $email, 1 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );

            } else {
                echo 'An unexpected error occurred';
            }
        }
    }

I get the text 'Thanks for contacting me, expect a response soon.' when I submit the form so I know its getting to that point but for some reason it just doesn't set the cookie. Im not using ingonito mode and I havent got any ad blockers and I'm accepting cookies.

So im not sure what im doing wrong any ideas would be greatly appreciated.

Cheers

Josh

0

Setting cookies is done via HTTP headers. This means you can't set any cookies via serverside methods (PHP->WordPress) anymore after any content has been sent to the browser.

A working example of setting cookies in WordPress (in one of my plugins):

Cookie Manager: https://github.com/download-monitor/download-monitor/blob/e26022f9a96abf174a874873b8cd35a0db98882b/includes/class-dlm-cookie-manager.php

Setting the actual cookie: https://github.com/download-monitor/download-monitor/blob/e26022f9a96abf174a874873b8cd35a0db98882b/includes/class-dlm-download-handler.php#L416

The cookie is set on the hook parse_request which is triggered after init so this probably isn't the issue in your case. Make sure there is no content send to the browser prior to your setcookie call.

| improve this answer | |
  • Awesome cheers, ended up making it work using javascript but this works too cheers – M0n5terBunny Apr 7 '17 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.