I have a WordPress plugin that wants to create cookies but it doesn't work. I would really appreciate a help.
add_shortcode( 'watchlist', 'cwatchlist_short' );
function cwatchlist_short() {
echo '<div class="wrap"><center>';
echo "<form method='POST' action=''";
echo '<p>Please enter the initial of the cryptocurrency or the symbol you want to add to the watchlist!</p> <p>For example: BTC</p>';
echo "<p><input type='text' name='symbol' placeholder='Initials or symbol'>";
echo "<p><input type='submit' value='Submit'>";
echo "</form></center>";
echo '</div>';
if (isset($_POST["symbol"])){
$x = $_COOKIE['xvalue'] + 1;
setcookie( 'xvalue', $x, time() + 108000, COOKIEPATH, COOKIE_DOMAIN );
Cannot modify header information - headers already sent
by
I really need help.
Thank you
best regards
setcookie function can not work on direct page run process, its always throw header already set error message.
To solve this issue must use new function for setcookie & add this function on init action.
For Ex.
add_action( 'init', 'my_setcookie_example' );
function my_setcookie_example() {
setcookie( "name", "value", time(), "/", COOKIE_DOMAIN );
}
In Your case,need to remove "setcookie( 'xvalue', $x, time() + 108000, COOKIEPATH, COOKIE_DOMAIN );" & place into function, After that add "add_action" hook on place of it.