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
- Hide or Show Read More Button by Content Area
- Proper use of Output Buffer
- Does My Child-Theme Functions.php Need if{die} Security In It? [duplicate]
- Should I write a PHP function in home.php?
- Edit tag cloud widget number
- function_exists call in function.php
- Debugging an error: wp_enqueue_style was called incorrectly
- How to log out everywhere else, destroy all sessions “all other devices”?
- the_post_thumbnail() based on the Post ID
- class=”parent” for wp_list_pages?
- Allow authors to post only in categories they create in WordPress
- Hide a menu-item and its submenus and display a ‘Log in’ link if the user is logged out
- Creating a WordPress form with a PHP script and default header
- Changing the HTML Of the content
- functions.php not adding css to website?
- if has theme mod
- How to override a non pluggable and non hookable function in a WordPress parent theme?
- Count Title and Post Characters
- Remove css styles from specific page
- Custom plugin issue when trying to use the shortcode twice on a page [closed]
- Logout and redirect using the WP Menu
- if file_exists not working with wp_enqueue_style
- Check if current page is wp-admin
- HTML table from shortcode with multiple parameters
- Generating add_settings_section() calls dynamically
- Sort registered users by post count? (inside admin dashboard)
- How to change a public function inside a class of a plugin?
- Put CSS inside a PHP file and include it the right way
- Missing sidebar parameter “fix” – before_content
- Randomise upload filenames (or another solution to hide the original image URL from theft?)
- Function set default image when image not present
- WooCommerce – Reset quantity input field when variation changes
- Query all posts of a custom taxonomy term
- jQuery not defined
- Use menu link or onClick to set a variable
- Automatically remove a canceled order in Woocommerce
- Trying to establish connection to External Database
- WordPress add post format support not working
- How to remove the cufon script from Dzonia Lite theme [closed]
- Login/logout in header
- Can I install/embed WordPress on a ‘single page’?
- How to put “Read more” link in Custom Excerpt inside p tag?
- Error when adding excerpt to the content through functions.php
- Add data attribute to each li in menu
- Prev/Next child navigation for current page modifications?
- Pass Category Name, Description and Photo into variables to pass to jQuery
- Get users that likes the post
- How to abort saving data in save_extra_profile_fields function WordPress?
- Create highly customized submenu (possibly using wp_list_pages)
- My php header() function is not working [closed]
- Get the date of the className or classID
- $wpdb->wp_users returning empty value for
- WordPress Custom field Colors