themes – setcookie on WordPress Page Template

Question

I am having an issue with setcookie.
I need to set a cookie on a page action (form).

I have tried on the template file, but not working.

then find out if I set it from functions.php with init hook, it’s working.

then I put condition like

if(is_page_template('mytemplate.php')){...

then it’s not working.

how can I do that?

here is my code.

add_action( 'init', 'hello_my_cookie' );

function hello_my_cookie() {
    if(is_page_template('mytemplate.php')){
        if(isset($_POST['GGmyCookie'])){
            setcookie( 'myCookie','Hello Cookie!', time()+3600);
            
        }
    }
    
}

0
Sarequl Basar 1 month 2022-05-31T16:31:04-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse