I am writing a plugin and I am having some difficulty with when to trigger specific functions of the plugin code.
/*
// 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;
// ...
});
As per suggested in a previous post, I separated the controller side of my plugin into a function called by the init
event. However, I do not want the code contained in the init
event function to be evaluated at every page load - I want my code to be evaluated only when the page containing the shortcode is loaded.
I have tried loading a boolean class variable that initializes as false but is changed to true from within the add_shortcode function, but by that time, it's too late - the init
event has fired, and the function's contents is not run.
Please help me - which expression should I use in Area C of my code? What should I test against to ensure the init
event function is run only when the shortcode is being used?
init
is so I can process user submitted data - I can include a nonce. I've never done it before though. Hehe!