plugins – WordPress Hooks : Where to place callbacks that repetitively yield the same effect?

Question

To give you the context first: Let’s say you create a custom plugin by yourself. That plugin has for example:

A) 30 different endpoints / REST calls,

B) adds 15 different admin pages to the wp-admin,

C) enqueues a script specific to every single admin page,

and so on.

To enable the entire functionality of this, I do:

A) 30 different calls to register_rest_route, and hook them to the rest_api_init hook.

B) Load the 15 different admin pages, and hook them to the admin_menu hook.

C) Enqueue all of the page-specific scripts via switch($page_slug) and hook that to the admin_enqueue_scripts hook.

Everything of this works perfectly, which is why won’t detail the code any further. Because my question does not really concern code, but the logic of wordpress and hooks under the hood: Currently, the code for A) – C) is written in the main plugin file of my custom plugin, and I’ve generally learned to do it that way. Meaning, AFAIK, that the entire hookings run on every single page load.

I now wondered: Couldn’t you for example hook your 30 endpoints of your REST API only for example via the activation hook of the plugin or similar? And similar for the admin pages?

Just feels odd to me that you’re loading the same code, whose output is theoretically a static registration, over and over again in production systems, and this on every single page load.

0
DevelJoe 4 months 2022-08-06T08:15:24-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse