admin管理员组

文章数量:1134247

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.

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.

Share Improve this question asked Jul 27, 2023 at 21:21 Angel HessAngel Hess 137 bronze badges 1
  • What does the function do? – Jacob Peattie Commented Jul 28, 2023 at 6:20
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: phpHow to make Wordpress Plugin run on single specific Admin Page