I need to run a function on my Wordpress, on every page of the site you'll visit, on every device, and template used (PC, Tablet, Mobile, ...).

This function will check if a Cookie has been set. If yes, great, else, if you click a certain link, it will set that Cookie based on some parameters.

  • sounds like a job for javascript, enqueued in the footer.php for your theme.
    – C C
    Feb 2, 2017 at 19:53

2 Answers 2


Generally functions hooked with init action will be available on every page and posts with in WordPress. it will immediately fire upon WP load. so maybe something like below will work.

function ao_check_cookie() {
 //Your code here.
add_action( 'init', 'ao_check_cookie' );

You can use the wp_head filter to get it done. This way you do not have to process it at the admin pages in the back-end with the init hook as @Abdul Rehman suggested.

Since you are dealing with cookies, make sure to add a low priority (i.e. 9) to ensure it is called at an early stage.

add_action('wp_head', 'my_cookie_check', 9);
function my_cookie_check() {
 // Your logic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.