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.
- What does the function do? – Jacob Peattie Commented Jul 28, 2023 at 6:20
1 Answer
Reset to default 1You 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
版权声明:本文标题:php - How to make Wordpress Plugin run on single specific Admin Page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736857908a1955793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论