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 wrote my first class in functions.php which I want to use to create multiple tabs with a simple call all over my template files. When I create a new instance directly in functions.php everything is fine for that instance. But when I call my class via a helper function outside my functions.php, all action hooks do not take effect. Are there any suggestions how to get this done?

Regards

class EasyTabs {

private static $instance;

public static function get_instance() {

    if ( !self::$instance instanceof self )
        self::$instance = new self;
    return self::$instance;

}

public function container_id() {
    $id = rand(1000,9999);
    return $id;
}

public function get_the_tabs() {
    ...
}

public function tabs_js() {
    ...
}

public function tabs_styles_and_scripts() {
    // style
    wp_register_style('jquery-easytabs-style', get_stylesheet_directory_uri() . '/inc/easytabs/style.css', array(), '', 'all');
    wp_enqueue_style('jquery-easytabs-style');

    //script
    wp_register_script('jquery-easytabs-min', get_stylesheet_directory_uri() . '/inc/easytabs/jquery.easytabs.min.js', array('jquery'), '', false);
    wp_enqueue_script('jquery-easytabs-min');
    wp_register_script('jquery-hashchange-min', get_stylesheet_directory_uri() . '/inc/easytabs/jquery.hashchange.min.js', array('jquery'), '', false);
    wp_enqueue_script('jquery-hashchange-min');
}

public function __construct() {
    //actions
    add_action('wp_enqueue_scripts', array ( $this, 'tabs_styles_and_scripts' ));
    add_action('wp_head', array($this, 'tabs_js'));
}

}

if ( !function_exists( 'the_tabs' ) ) {
    function the_tabs() {
        echo EasyTabs::get_instance()->get_the_tabs();
    }
}

Edit:

Thanks for the hint, looking for a hook which fires later did the trick: http://shibashake.com/wordpress-theme/wp_enqueue_script-after-wp_head

class RFRQ_Tabs {

    public $containerID;
    public $posttype;

    public function __construct( $posttype ) {
        // set vars
        $this->set_container_ID();
        $this->set_posttype($posttype);
        //add scripts and styles
        add_action('wp_footer', array($this, 'scripts'));
        add_action('wp_print_footer_scripts', array($this, 'ex_scripts'), 1);
    }

    ...

    public function scripts() {
        echo "<script type='text/javascript'>";
            echo "jQuery(document).ready( function() {";
                echo "jQuery('#tab-container-" . $this->get_container_ID() . "').easytabs();";
            echo "});";
        echo "</script>";
    }


    public function ex_scripts() {        
        if (is_admin())
            return;
        wp_enqueue_script('jquery-easytabs-min', get_stylesheet_directory_uri() . '/inc/easytabs/jquery.easytabs.min.js', array('jquery'), '', true);
        wp_enqueue_style('jquery-easytabs-style', get_stylesheet_directory_uri() . '/inc/easytabs/style.css', array(), '', 'all');
        }  
}
/**
 * RFRQ
 * helper function to instance an tab-object
 */
if (!function_exists('the_tabs')) {
    function the_tabs( $posttype ) {
        $easytabs = new RFRQ_Tabs( $posttype );
        echo $easytabs->get_the_tabs();
    }
}
share|improve this question
1  
When are you calling the_tabs function? –  Krzysiek Dróżdż Jun 28 '13 at 10:33
add comment

1 Answer

up vote 1 down vote accepted

I guess that you call the_tabs function (so add your actions to hooks) too late (after these hooks were fired).

For example if you call it in some template file (i.e. single.php) after wp_head function call, your filters won't run - you add them to this hook after it already was fired.

share|improve this answer
add comment

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.