Write a plugin that only runs on the plugins page

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #877
    thusman
    Participant

    Hello, I’m new to the world of wordpress plugin development.

    I’m writing an import plugin and I noticed it gets loaded even for anyonymous frontend users. I want it only to load/run when an admin is viewing the plugin page in the backend. I found myself a solution with the admin hook [current_screen](https://codex.wordpress.org/Plugin_API/Action_Reference/current_screen) and I’m interested to hear from more experienced developers if this is reasonable or if there are other build-in methods for such cases (e.g. prevent auto loading of the plugin on each request).

    add_action(‘current_screen’, ‘initPlugin’);
    function initPlugin($wpScreen) {
    //only run on my-import-plugin page
    if($wpScreen->base != ‘settings_page_my-import-plugin’) return;

    //execute plugin …
    echo ‘Hello, you are viewing the plugin screen’;
    }

    ​

    #878
    CharlesCSchnieder
    Guest

    Make a settings page for the plugin and call the function there, no?

    #879
    r1ckd33zy
    Guest

    This would work for your purposes, also there is the `get_current_screen()` function

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.