问题
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
回答1:
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.
回答2:
You can't set a cookie in a shortcode function because the headers have already been sent by WordPress. setcookie() defines a cookie to be sent along with the rest of the HTTP headers, so if you want to set a cookie value it needs to be added in a hook prior or during the send_headers hook.
回答3:
Please use ob_start();
function at the top of the file and ob_end_flush()
function after the code where you want to use cookies.
ob_start function: Turns on output buffering.
ob_end_clean function: Clears buffer and closes buffering.
For more details http://php.net/manual/en/ref.outcontrol.php
回答4:
Thank you for all of your answers! The OB_START didn't work for me maybe there was a problem in my code but I couldn't use it. The admin post was also a great way to solve it but the easier way for me was to change my code less and I used the add_action inside my function.
add_shortcode( 'watchlist', 'cwatchlist_short' );
add_action( 'init', 'my_setcookie_example' );
add_action( 'init', 'my_setcookie_isset');
function my_setcookie_isset() {
if(isset($_COOKIE['xvalue'])){
} else{
setcookie ("xvalue","0",time() + 108000, COOKIEPATH, COOKIE_DOMAIN);
}
if (isset($_COOKIE['thejson'])){
} else{
setcookie ("thejson"," ",time() + 108000, COOKIEPATH, COOKIE_DOMAIN);
}
}
function my_setcookie_example() {
if (isset($_POST["symbol"])){
setcookie ("thejson","1",time() + 108000, COOKIEPATH, COOKIE_DOMAIN);
So basically what I did was to add a new function that was going to be run before the header and it checked if the form was completed or not. If textbobx SYMBOL was completed then I set a cookie. Also I set all of my cookies with a nonimportant value to have no error during the process. So if you have a problem except setting a cookie inside a shortcode and a function that check isset of a textbox. Run that function usuing INIT before the header!
Thank you to all because this is my conclusion from all of your answers and ideas and links!
Best Regards
来源:https://stackoverflow.com/questions/50205548/setcookie-not-working-wordpress-because-of-header