短代码之前的WordPress插件事件调用
我正在编写一个插件,在何时触发插件代码的特定功能方面遇到了一些困难短代码之前的WordPress插件事件调用,wordpress,events,plugins,shortcode,Wordpress,Events,Plugins,Shortcode,我正在编写一个插件,在何时触发插件代码的特定功能方面遇到了一些困难 /* // Plugin information goes here */ // ***** Area A $GLOBALS['example_class'] = new example_class; class example_class { // ***** Area B public function admin_init() { add_menu_page(
/*
// Plugin information goes here
*/
// ***** Area A
$GLOBALS['example_class'] = new example_class;
class example_class {
// ***** Area B
public function admin_init() {
add_menu_page(
// ...
);
} // End of admin_init function
} // End of example class
add_action('init', function() {
global $example_class;
// ***** Area C
if ( ?????? ) {
// Sanitize and set the view role
$view = ( isset( $_REQUEST['view'] ) ) ? sanitize_key( $_REQUEST['ex'] ) : 'get_all';
// Manage submitted data
switch ( $view ) {
// ...
} // End of switch for view
// Sanitize and set the action role
$action = ( isset( $_REQUEST['action'] ) ) ? sanitize_key( $_REQUEST['action'] ) : NULL;
// Manage submitted data
switch ( $action ) {
//...
} // End of switch for action
} // End of if page is being shown
});
add_action( 'admin_menu', function() {
global $example_class;
$example_class->admin_init();
});
add_shortcode( 'show_public_random', function () {
global $example_class;
// ...
});
根据stackexchange上的建议,我将插件的控制器端分离为一个由init
事件调用的函数。但是,我不希望在每次页面加载时对init
事件函数中包含的代码求值-我只希望在加载包含短代码的页面时才对代码求值
我曾尝试加载一个布尔类变量,该变量初始化为false,但在add_shortcode函数中被更改为true,但到那时,为时已晚,init
事件已触发,函数的内容未运行
请帮助我-我应该在代码的C区使用哪个表达式?我应该针对什么进行测试,以确保只有在使用短代码时才运行
init
事件函数?我找到了一个答案,尽管很混乱
/*
// Plugin information goes here
*/
$GLOBALS['example_class'] = new example_class;
class example_class {
var $public_loaded = false,
$content = '';
public function admin_init() {
add_menu_page(
// ...
);
} // End of admin_init function
public function get_random( ) {
// ...
}
} // End of example class
add_action('init', function() {
global $example_class;
// ***** Area A
// Check for arbitrary variable sent with every user interaction
if ( if ( isset( sanitize_key( $_REQUEST['tni'] ) ) ) {
// ***** Area B
/* Set the class variable `public_loaded` to true after it's
* clear we're loading a public page which uses our plugin */
$example_class->public_loaded = true;
// Sanitize and set the action role
$action = ( isset( $_REQUEST['action'] ) ) ? sanitize_key( $_REQUEST['action'] ) : NULL;
// Manage submitted data
switch ( $action ) {
// ...
} // End of switch for action
// Sanitize and set the view role
$view = ( isset( $_REQUEST['view'] ) ) ? sanitize_key( $_REQUEST['ex'] ) : 'get_all';
// Manage submitted data
switch ( $view ) {
// ... Generate content and store in $this->content
} // End of switch for view
} // End of if page is being shown
});
add_action( 'admin_menu', function() {
global $example_class;
$example_class->admin_init();
});
add_shortcode( 'show_public_random', function () {
global $example_class;
// ***** Area C
/* Check to see if page has loaded using the telltale sign
* If not, load a default view - a random post */
if ( $example_class->public_loaded === false ) {
$example_class->content = $example_class->get_random();
// ...
}
// Return the generated content
return $example_class->content;
});
在区域A中,我设置了一个限定语句,以查看用户是否提交了一个变量以及他们与我的插件的交互。如果插件是我的,则对代码进行评估,并对action
和view
模式进行评估。此外,该函数还将类变量public\u loaded
设置为true
在区域C中,我设置了一个限定语句,以查看类变量是否已设置为true;如果没有,则会为快捷码设置默认视图。您想检查什么?
init
表示要执行的第一件事,在此之前,您的任何条件都不起作用。。您可能应该尝试将您的函数与其他apt挂钩。我需要根据操作和视图运行条件,以决定在页面上显示什么,因此我选择的apt挂钩需要在调用快捷码之前。