I have build a plugin in which i have used setcookie like this :

function myFunctionAbc () {
$response = wp_remote_get('http://my/request/url/', array('timeout' => 240, 'decompress' => false));
$responseData = json_decode($response['body'], true);
setcookie('my_data', $response['body']);

return $responseData;

and in template file am doing this :

/* Template Name: custom results */

if (function_exists('myFunctionAbc')){
    $response = myFunctionAbc();
} else {
echo 'no data';

Now if i comment setcookie line, everything works fine but with setcookie, wordpress redirecting it to home page. I can't use init hook as i need data on some particular actions.

Note : I am using wordpress 4.6.1 and its working fine on my local system but having this issue on server.

Please help.


Please use the Domain & other parameters also in cookie...

setcookie('cookie_name', cookie_val, time()+3600, COOKIEPATH, COOKIE_DOMAIN, false);
| improve this answer | |

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.