A
A
Alexey Nikolaev2016-02-24 16:00:46
WordPress
Alexey Nikolaev, 2016-02-24 16:00:46

In which filter / action is it more correct to determine the current page?

Good day.
You need to determine the current page in order to log it into the page views along with the user data. After reviewing a couple of plugins that have similar functionality, I noticed that, as a rule, wp_head is used there . However, is this correct? This filter is not designed to manipulate the page object, including its definition. But the problem is that I also did not find a more suitable filter. What do you think is the best filter to use to just detect the current page and do something abstract (no redirects or template changes)?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Kozlov, 2016-02-25
@trampick

I would use php
$my_url = explode('/', $_SERVER["REQUEST_URI"]);to get by enumeration the last element that is responsible for the slug (it is the url name of the post, category, page). It should be noted that pagination may be present in the url line (page/2 for example).
Then I would use the standard functions is_front_page(), is_single(), is_page(), is_category() to check what the current page is. You need to pass a parameter to these functions, just slug. The problem with this method is that there may be posts, pages, categories with the same slug. For categories, the check is simple, again, find the word category in $my_url (it is by default, but you can change it).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question