0

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?

  • I'd use Ajax to process the form. A very simple example. – brasofilo Oct 13 '13 at 4:59
  • @s_ha_dum - Thanks for your comment. In the accepted answer post, it says it is a heavy block of code, and should be used only when necessary. I'm back at square one. – Mario.Hydrant Oct 13 '13 at 15:53
  • It is a heavy block of code. You have process the post body twice, but shortcodes by default run late in the page load. They cannot alter things that run earlier than they do. – s_ha_dum Oct 13 '13 at 16:00
  • @sa_ha_dum Thanks for your quick reply! What if I used nonces to qualify the page? The whole reason I need to run at init is so I can process user submitted data - I can include a nonce. I've never done it before though. Hehe! – Mario.Hydrant Oct 13 '13 at 16:10
0

I've found an answer, although messy.

/*
// 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;
});

In Area A I've set a qualifying statement to see if a user submitted a variable along with their interaction with my plugin. If the plugin is mine, the code is evaluated, and action and view modes are evaluated. As well, the function will set the class variable public_loaded to true.

In Area C I've set a qualifying statement to see if the class variable has been set to true; if not, a default view is set for the shortcode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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