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: …’
- the_author_posts_link css class
- Remove class that has been added by parent theme
- How to check from which page the user is coming to the current page?
- Design view breaking on Pages
- CSS saved & enqueued, but wordpress doesn’t load css text, only file
- Execute only on a certain page
- How to make thumbnail image fit into a div where image dimentions are completely different?
- Word Count Function Preventing Permalink Editing
- Changing custom logo link
- Add a variable in functions.php and call it in single.php
- Linking text within textarea of custom meta box
- what is the best practice to add new field to an api route
- Auto-update products after they were published – Woocommerce
- php function to display commenter username or login
- How to get Post Type from the functions.php file
- Edit the_category (); for a hierarchical display
- Omit custom post type from wp-sitemap.xml based on meta key using wp_sitemaps_posts_query_args
- Multiple meta_key in one global $wpdb;
- Using data sent via AJAX in multiple functions on a WP plugin
- WordPress archive page showing 404 Error [duplicate]
- Utilize Function in Child Theme / Template File
- How to have different site identity logos on each page on Astra Theme [closed]
- Overide Variable in Child Theme
- child parent styles enqueue order
- Add URL parameter to all internal links using a specific theme
- Warning: call_user_func() expects parameter 1 to be a valid callback, function
- Randomize Color Scheme Selection in Theme
- How to make changes to style.php [closed]
- AJAX function not working [closed]
- Modify WooCommerce used to get all orders in dashboard
- WordPress can’t use ZipArchive
- display condition based on post term and status
- Only the first image uploads
- removing the standard dashboard widgets in WP so I Can replace with custom
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- Trying to generate a CSS file in wordpress uploads folder
- How to change order inside foreach using wp_enqueue_script?
- Change header on one page in WordPress
- Adding extra data to shortcode attributes and pass it to JS with wp_localize_script
- Add a default style if no Featured image is selected
- Add Product Permalink in woocommerce admin order page
- Cleaner ‘for each’ loop for registering scripts in functions.php
- Premature end of script headers: wp-mail.php?
- setcookie() issues on running with HTTPS on WordPress
- How do I make comment-reply-button with that takes to comment reply form on click
- Allow user to choose header style
- How to get all users by user_login as a json_encode array?
- How limit the width of a fixed menu-bar on scroll?
- Allow BBPress participant role to trash topics
- How Can I Center the Logo in the Navigation Menu? [closed]
- Replace TinyMCE with Custom Built Editor
- Particular meta tag – viewport – insertion when dealing with plugin
- How to create and add js and css file when server is not on my PC?
- Understanding responsive imagery
- How to get meta box labels?