Since you are using init “add_action”, then it is best not to use it in a template – since “add_action” is used by WordPress core’s “do_action” – which comes before the templates.
If you want this function to take higher priority, place it in functions.php but use the ‘priority’ variable of “add_action”:
add_action( $tag, $function_to_add, $priority );
The default priority is 10. See more here
Alternatively, if this is relevant only for a specific template, then I don’t think you need add_action at all – or even a function for that matter.
Simply place the inner code of your function BEFORE get_header() in your template:
if(isset($_POST['region']))
{
// Set Cookie
setcookie('region', $_POST['region'], time()+1209600);
// Reload the current page so that the cookie is sent with the request
header('Region: '.$_SERVER['REQUEST_URI']);
}
Related Posts:
- Remove Actions/Filters added via Anonymous Functions
- Programatically add options to “add new” custom field dropdown
- Excluding iPad from wp_is_mobile
- opening links in new tab using – add_filter( ‘the_content’, ‘make_clickable’);
- How to add classes to images based on their categories?
- Use ‘add_theme_support’ instead of ‘add_custom_image_header’ In WordPress 3.4
- Generating Responsive Background Image Sizes in PHP
- Replacing select2 in admin backend for all selects
- Set default options for inserting media
- How to hook into the quick edit action?
- Session is not starting
- Override a class function to include a custom template
- How to auto-update child posts whenever the parent post is updated?
- I created a child theme and it doesn’t work for some of the css files
- the_post_thumbnail fallback using hooks
- How to detect first visit of a user?
- How to programmatically set a menu to be the Primary Menu?
- Hide WYSIWYG editor on certain templates
- Disable shortlinks like ?p=1234 to prevent scraping
- Redirect a list of URLs to another URL, using functions.php
- Override woocommerce wc-class function
- WP_Remote_Get Not working
- Debugging with functions.php
- How to hide, and not to remove the attributes metabox?
- how to edit functions.php in a child theme
- Only display link to author social media when it exists [closed]
- Get current page_id before loop, in functions.php
- Menu fallback “menu_class” rendering a “div” instead of a “ul”
- Create single.php for specific tag by tag id or name
- How to limit character length in BuddyPress function output [closed]
- Using $themename Variables
- Ordering posts by publish date not working?
- How to add a slide toggle to the Woocommerce-Layout__Activity-Panel area in the orders admin page
- Is possible add icon in title posts only in specific tag?
- Wp_schedule_event functions.php
- How to auto refresh when user change postcode
- Add sequential number to a Gravityforms form
- How to search using ajax for exact phrase or words in an input field?
- Can’t generate thumbnail images
- Display tags that only appear in one category
- Need help “sanitizing” a custom function that pulls category slug into body class
- Space Not Being Generated in HTML Output?
- How to display retweet count and likes in the meta above the excerpt
- Auto Generated HTML source code formatting
- Do not show on homepage functions.php code needed
- Get Comma Seperated Taxonomy Linked Terms and Last Child Separated By “&” Instead Of Comma
- Possible to hook into Media Library preview File column and use a custom image?
- How to remove a specific word at the beginning of the content and add it to the end of the content
- Programmatically add Yoast meta description after post save
- Woocommerce checkout field
- Warning: preg_match(): No ending delimiter ‘^’ found in
- Exclude Category ID in function
- get_template_directory_uri() links to child theme not parent
- Functions Filter Question [closed]
- Remove attachment page for audio media type only
- Where to store credentials used in a function? [duplicate]
- “options.php” not found
- Both the require and add_template_part function are not including the file
- get_template_directory() – references parent theme directory
- Function that prevents users from uploading photos more photos
- Why I can’t load my JavaScripts using an hook inside my functions.php file?
- Shortcode of a function
- Conditional function for excluding first image from content, not working
- A better way to get stats
- Conditional custom menu?
- What function actually renders the wp_admin_bar ? How can I call it?
- Get URLs for AJAX Filter Checkboxes WordPress
- redirecting a URL if accessed directly
- Detect Ajax call inside pre_get_posts function
- What is wp_get_post_tags for media tags?
- Filter works on last selection but no others
- How do I trigger WP CLI DB export using a PHP function?
- WordPress does not load jQuery
- get_current_user_id() always return 0 in if else statement
- Redirect specific author posts to another url
- Show number of sold products but be first for less than 2
- How to retrieve image IDs from shortcode content?
- I need to ‘wp_dequeue_script’ and ‘styles’ and ADD a bunch of other css and js
- Class::$template_dir_path=get_template_directory();
- admin-ajax.php & my wp-admin folder url showing in header
- WordPress – Custom Nav menu for logged in users – Shopkeeper theme
- Change post count on tag/terms pages to 10
- How to use if (is_page_template (”))
- wordpress function through ajax not being called
- Error function main() is a non-object to construct my Ajax.php
- Automatically add custom fields (post_meta) to all published posts, hourly, via wp_cron?
- Gravity Forms After Submission – GFFormsModel::update_lead_field_value?
- Fatal error after changes to functions.php, even with original file
- update_post_meta after form is submited
- Should I use `get_stylesheet_uri()` or `get_template_directory_uri()` when calling my CSS Stylesheet?
- new to javascript – using in instead of functions.php, not loading correctly
- Remove Disqus JavaScript from homepage
- Missing argument 2
- User’s Comments Number: Storing it in a meta field for different uses
- Show one post per author and order by custom field
- Text before price on WooCom
- Warning: call_user_func_array() expects parameter 1 to be a valid callback
- Change Post status based on custom field date +1 day
- How to add height and width dimensions automatically to archive descriptions
- How to store page-{id}.php templates in custom folder