How do I use className::method()/array(‘className’, ‘method’) in add_action(‘init’, ”)

I’m new to WP, and still have to study more concepts, good practices and functions. Besides, my english isn’t pretty good. So, take it easy 😛

I was trying to develop my own plugin when I got a trouble with add_action('init', '') (not sure yet). Here is my class (named MeuPlugin):

Read More
private static $wpdb;

public static function initialize(){
    global $wpdb;

    //add_action('init', MeuPlugin::newslettersPostType());
    add_action('init', array('MeuPlugin', 'newslettersPostType'));
    add_action('admin_menu', array('MeuPlugin', 'addMenu'));

    MeuPlugin::$wpdb = $wpdb;
}

public static function install(){
    if (is_null(MeuPlugin::$wpdb)) MeuPlugin::initialize();
}

public static function newslettersPostType() { 
    $labels = array(
        'name' => __('Newsletters'),
        'singular_name' => __('Newsletter'),
        'add_new' => __('Adicionar Novo'),
        'add_new_item' => __('Novo Item'),
        'edit_item' => __('Editar Item'),
        'new_item' => __('Novo Item'),
        'view_item' => __('Ver Item'),
        'search_items' => __('Procurar Itens'),
        'not_found' =>  __('Nenhum registro encontrado'),
        'not_found_in_trash' => __('Nenhum registro encontrado na lixeira'),
        'parent_item_colon' => '',
        'menu_name' => 'Newsletters'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'public_queryable' => true,
        'show_ui' => true,          
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => 5,
        //'register_meta_box_cb' => 'newsletters_meta_box',     
        'supports' => array('title','editor','thumbnail','excerpt')
    );

    register_post_type('newsletters', $args);
    //flush_rewrite_rules();
}

public static function addMenu(){
    add_options_page('MeuPlugin - Gerenciamento','MeuPlugin',10,__FILE__,array("MeuPlugin","testPage"));
}

public static function testPage(){
    echo '...';
}

And then I use

$pluginFile = substr(strrchr(dirname(__FILE__),DIRECTORY_SEPARATOR),1).DIRECTORY_SEPARATOR.basename(__FILE__);

register_activation_hook($pluginFile, array('MeuPlugin', 'install'));

add_filter('init', array('MeuPlugin', 'initialize'));

to “run” my plugin.

My question: why add_action('init', array('MeuPlugin', 'newslettersPostType')); (“line” 7) doesn’t work, while add_action('admin_menu', array('MeuPlugin', 'addMenu')); (“line” 8) does?

P.S.: when I use add_action('init', MeuPlugin::newslettersPostType()); (“line” 6) instead of add_action('init', array('MeuPlugin', 'newslettersPostType')); (“line” 7), it works fine. Why I have to put className::method() instead of array('className', 'method')?

I hope I have been clear. Thanks!

Related posts

1 comment

  1. Hi the “add_action” function defined like this:

    function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
        return add_filter($tag, $function_to_add, $priority, $accepted_args);
    }
    

    1st param is a string;
    2nd param is a reference to function and not an array as you tried to use

     array('MeuPlugin', 'newslettersPostType')
    

    Currently i cannot explain why the line 8 :

    add_action('admin_menu', array('MeuPlugin', 'addMenu'));
    

    is working….. it shouldn’t

Comments are closed.