Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am developing a WP plugin called e.g. DD_Awesome_Plugin and this is my code so far (simplified version without additional code logic within class functions):

class DD_Awesome_PLugin {

    public function __construct()
    {

    }

    public function add_menu_page()
    {
        add_options_page('DD Awesome PLugin', 'DD Awesome PLugin', 'administrator', __FILE__, array('DD_Awesome_PLugin', 'display_options_page'));
    }

    public function display_options_page()
    {
        $plugin_options = get_option('dd_my_awesome_plugin');
        echo "Here we go admin!";

        /* and after echo I need to triger a function "trigger_me_from_class()" that is located outside of the class. */
    }

}


add_action('admin_menu', function() {
    DD_Awesome_PLugin::add_menu_page();
});

add_action('admin_init', function() {
    new DD_Awesome_PLugin();
    include_once dirname(__FILE__) . ('/simple_html_dom.php');
});


 /* just trigger this function "trigger_me_from_class" from "display_options_page" function (situated in DD_Awesome_plugin class above) */
public function trigger_me_from_class()
{
    $str = date('Y-m-d H:i:s', time());
    wp_mail('info@example.com', 'DD success message', "Success at $str." );
}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Create a static getter for your class instance:

class DD_Awesome_Plugin
{
    /**
     * Plugin main instance.
     *
     * @type object
     */
    protected static $instance = NULL;

    /**
     * Access plugin instance. You can create further instances by calling
     * the constructor directly.
     *
     * @wp-hook wp_loaded
     * @return  object T5_Spam_Block
     */
    public static function get_instance()
    {
        if ( NULL === self::$instance )
            self::$instance = new self;

        return self::$instance;
    }

    public function add_menu_page()
    {
        add_options_page(
            'DD Awesome PLugin', 
            'DD Awesome PLugin', 
            'administrator', 
            __FILE__, 
            array( $this, 'display_options_page' )
        );
    }
}

And now you get the plugin instance with:

add_action('admin_menu', function() {
    DD_Awesome_Plugin::get_instance()->add_menu_page();
});

Or:

add_action( 
    'admin_menu', 
    array( DD_Awesome_Plugin::get_instance(), 'add_menu_page' ) 
);
share|improve this answer
    
Thanks, I will test it and let you know if it works. –  Derfder May 22 '13 at 8:20
    
@Derfder It does. :) Example 1: T5 Head Meta, example 2: T5 Spam Block. –  toscho May 22 '13 at 8:22
    
Hmm, but all your functions are in the class. My function function trigger_me_from_class() is outisde of that class. How to trigger such function from some function residing in the class? –  Derfder May 22 '13 at 8:25
    
Just call that function? Note, plain functions cannot have visibility keywords like public. –  toscho May 22 '13 at 8:40
    
ok, so just remove the word public? ok, and how to call them from classes function like: <?php trigger_me_from_class(); ?> ? –  Derfder May 22 '13 at 8:53
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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