I know this is an old post and you have probably already found an answer, but here goes…
When setting cookies, you need to include it in the HTTP headers as explained here: http://php.net/manual/en/function.setcookie.php
This means that you need to add a WordPress action hook similar to as follows in your functions.php file (untested) before the headers are sent.
I have used ‘template_redirect’, but you could read into using ‘init’ also.
add_action( 'template_redirect', 'set_cookie');
function set_cookie() {
// Set cookie
if (isset($_POST["styles"])) {
setcookie("chosenStyle", $chosenStyle ,time()+31536000, "");
wp_redirect( home_url() ); // Redirect back to home page
exit;
}
}
Have a read about WordPress actions/filters and this page:
http://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
Related Posts:
- Best way of adding CSS which can be manipulated by the user via theme option panels?
- Change logo based on incoming domain
- Shortcode content output but not in correct place
- Conditional Header in wordpress
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- How to style options page in dashboard?
- Css loads very slow [closed]
- wp-comments-post.php file returns a blank page
- responsive.css in the WordPress should be prioritized
- height should be set to auto to avoid pixelation in the post thumbnail function
- Moving code from theme header to functions.php
- How to make container class in PHP
- linking stylesheets and scripts with functions.php
- Clickable image link sends people to wrong URL
- Creating a mobile site by adding a new class to the body tag from functions.php
- Add meta tags to a custom header
- CSS change in woo commerce Place Order Text [closed]
- Problem with adding custom CSS class to image in ACF Photo Gallery plugin [closed]
- Custom functions for string data calculations
- how to changes mobile menu toggle breakpoint in WordPress
- Reliable way to add nonce to HTTP Header in WordPress?
- How to remove get_header From all single pages in wordpress with functions.php without plugin?
- PHP function for horizontal Woocommerce thumbnails and badges
- Problem with displaying CSS Stylesheets – Am I adding them correctly in my wordpress child theme?
- I got big white screen when i attach main style sheet of Html in functions.php
- Using Javascript On Page – Header and Footer Now Missing
- Add my footer menu to the header
- Site title not showing. Please help me
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- Add class for the_post_thumbnail_caption()
- How to include different html tags in the header for different pages?
- Displaying POST content with HTML tags and all
- Highlight Single Page Ancestor
- Styling admin page rows in order of importance (checkboxes)
- How to have Function of a plugin using global vars into a shortcode?
- Cant get theme to change footer color through theme customizer
- Changing 2011 theme header image based on user dropdown
- Converting Menu Links to List Elements
- How to add margin underneath woocommerce total price amount -> productpages
- How do I get Roboto Google Font to display normal font-style? [closed]
- Integrating CSS Into a WP Function Call [closed]
- Load slideshow.css file only if Slideshow is checked / on
- Sorting Problem
- Dynamic Email Handler with ‘header (“Location: …’
- How do I make a shortcode to display Dokan seller badge list?
- How can we upload custom page files?
- Calling a PHP function from a menu item
- Removing website URL in comments causes misalignment of submit button and tickbox
- Missing sidebar parameter “fix” – before_content
- Get different images for mobile and desktop with php, advanced custom fields without using js and ajax
- Randomise upload filenames (or another solution to hide the original image URL from theft?)
- How do I display a user’s previous orders as a select box option on a product?
- How Display Posts on category
- Using file_get_contents with Gravity Forms uploads folder to create images in the media library returning false
- allow subscribers to see private posts without plug in
- Replace word in “the_content” only for index.php
- How do you add customer capability after Woocommerce purchase?
- How to get specific image in media library with php
- Function set default image when image not present
- Custom location for attached images
- WooCommerce – Reset quantity input field when variation changes
- Query all posts of a custom taxonomy term
- get current product name in functions.php
- Get first URL from post content
- Setting custom canonical urls
- Handling Body class based on Template
- Trigger popup in a php if/else statement
- Setting a cookie upon specific URL visit
- Get_avatar filter?
- How to stop featured image thumbnail [set as background image] on blog index page just repeating same image across all posts
- Can’t get page content in WordPress
- WordPress child theme fails to override parent navigation menu in /inc/structure/header.php
- Way to querry data (tags) from a wordpress database?
- get post based on category chosen in drop down – The ajax method
- Slider loading issue
- Adding Default Settings to Theme My Login plugin
- Image as Sales Badge
- how to make separate field in wordpress user_data_
- Function not working at one place
- jQuery not defined
- Displaying the Month and Year that a page was Created?
- Changing custom logo link
- Add a class to the anchor tag on HTML5 gallery
- Remove meta description on certain pages
- add_image_size for header_image
- Menu — How to add “current-menu-grand-ancestor” css class
- Plugin CSS not enqueing
- Use menu link or onClick to set a variable
- Swapping wp_dropdown_categories function with wp_category_checklist
- functions.php is being included twice, creating PHP fatal errors
- Automatically remove a canceled order in Woocommerce
- Parent theme CSS overriding child CSS rules
- Display fields as values in array from external SQL DB
- Only Show Link If div Exists
- How to put a variable in a instance in the widget
- Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
- how to remove metadata from the posts of my blog?
- Trying to establish connection to External Database
- add_action shortcut?
- Passing the custom field values in the wp_get_current_user array function