0

I've written a plugin and it has regular functions and a few class functions. At the end of the class function I have a line that runs the class like this:

$the_function = new the_function();

The plugin is only supposed to be running on one spesific page (not multiple pages) in wordpress admin zone, for example it is supposed to run only on:

/edit.php?post_type=myposttype

The plugin seems to run on all pages and it happens to breaks the submit button on the wordpress customizer page. It stops customizer from saving data after pressing the customizer save button.

As a temporary fix I have added this around the line that runs the class function:

if(strpos($_SERVER['REQUEST_URI'],$myposttype))

That works fine but it occurs to me that I imaging there is a more logical way to make a spesific function only run on a spesific wordpress admin page. To learn, I have read some other examples and I want more comments, please. What is your preferred method to make a single spesific function run on a spesific admin page?

I also tried to use if(!is_customize_preview) but this is not spesific enough alone.

1
  • What does the function do? Commented Jul 28, 2023 at 6:20

1 Answer 1

1

You should use the load-edit.php action hook, and check for your post type (untested):

add_action( 'load-edit.php', static function () {
    $screen = get_current_screen();

    if ( empty( $screen->post_type ) || 'myposttype' !== $screen->post_type ) {
        return;
    }

    new the_function();
} );

You could also use the current_screen hook, but load-edit.php seems more correct.

2
  • Thanks. Works good. But one issue/question. This does not function on the edit page for example: /post.php?post=276&action=edit - so is there a way to make this method work on the edit page also?
    – Angel Hess
    Commented Jul 28, 2023 at 22:30
  • The hook for that page would be load-post.php.
    – Caleb
    Commented Jul 28, 2023 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.