Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+6 votes
236 views
in Q2A Core by

How to initialize this function on a specific q2a page request?

public function initialize() {

    parent::initialize();

    $qa_count_user_visits = isset($_COOKIE['qa_count_user_visits']) ? (int)$_COOKIE['qa_count_user_visits'] + 1 : 1;

    setcookie('qa_count_user_visits', $qa_count_user_visits, time() + 86400 * 365, '/', QA_COOKIE_DOMAIN, (bool)ini_get('session.cookie_secure'), true);

}

Q2A version: 1.8.6

1 Answer

+3 votes
by
selected by
 
Best answer

Being code that is running in the context of a layer you have 2 options: check by template or check by request. Requests are unique while templates are sometimes shared among some group of pages.

A simple way to see the template would be adding this code inside the initialize() function:

echo $this->template;

In order to see the request you could use:

echo qa_request(); // Or also echo $this->request;

You can access specific parts of the request by using qa_request_part() function.

Once you have identified the template or request you want your code to execute on, then you could just check this way:

public function initialize() {
    parent::initialize();
    if ($this->template === 'question') {
        echo 'this code will only run in the "question" page';
    }
}
by
Thanks a lot, pupi1985 for helping me!
...