admin管理员组

文章数量:1122832

On frontend pages I can do something like

$currentPostId = url_to_postid($_SERVER['REQUEST_URI']);
if ($currentPostId && $currentPostId === $postId)
    // ...

So when I add a programmatically generated page I can know in init whether I am on that page. This is very useful because I can process POSTs before rendering the page and so I can redirect with a location header. I am looking for a backend solution:

    add_menu_page(
        $this->page->getTitle(), 
        $this->page->getMenuTitle(), 
        $this->page->getCapability(), 
        $this->page->getId(), 
        function (){
            $this->page->render();
        },
        $this->page->getMenuIcon(),
        $this->page->getMenuPosition()
    );

I do something like the upper, but I'd like to do a page init before rendering. Currently I initialize all the pages which is wasting resources when I know that the form I sent is on a certain page. So I just have to initialize that page and process the POST request.

On frontend pages I can do something like

$currentPostId = url_to_postid($_SERVER['REQUEST_URI']);
if ($currentPostId && $currentPostId === $postId)
    // ...

So when I add a programmatically generated page I can know in init whether I am on that page. This is very useful because I can process POSTs before rendering the page and so I can redirect with a location header. I am looking for a backend solution:

    add_menu_page(
        $this->page->getTitle(), 
        $this->page->getMenuTitle(), 
        $this->page->getCapability(), 
        $this->page->getId(), 
        function (){
            $this->page->render();
        },
        $this->page->getMenuIcon(),
        $this->page->getMenuPosition()
    );

I do something like the upper, but I'd like to do a page init before rendering. Currently I initialize all the pages which is wasting resources when I know that the form I sent is on a certain page. So I just have to initialize that page and process the POST request.

Share Improve this question asked Apr 2, 2024 at 17:22 inf3rnoinf3rno 1356 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

There are few functions/global variable in WP to determine

is_admin() - For checking if in admin panel
global $pagename  - to get the page name of admin 

I ended up checking the $_GET['page'] === $this->page->getId() and checking is_admin(). Not the perfect solution, but I did not have anything better for now.

本文标签: plugin developmentIs there a way to decide from init whether we are on a certain backend page