0

Having this :

    <?php

class Planner_plugin{

    public function __construct(){
        add_shortcode('planner::display', array($this, 'display_planner'));
        register_activation_hook( __FILE__, array( 'Planner_plugin' , 'install'));
        add_action('wp_enqueue_scripts', array($this,'planner_load_scripts')); // chargement du script      
        ...
    }

    public function planner_load_scripts(){
        wp_register_style('planner_css', plugins_url('css/style.css', __FILE__)); //my style
        wp_register_style('opensans_css', plugins_url('css/open-sans_Font.css', __FILE__)); //Open sans font
        wp_register_style('fa_css', plugins_url('css/font-awesome.min.css', __FILE__));
        wp_register_script('planner_script', plugins_url('js/calendar.js', __FILE__), array( 'jquery' ), '', true ); // my script who generate a planner
        wp_enqueue_style('planner_css');
        wp_enqueue_style('opensans_css');
        wp_enqueue_style('fa_css');
        wp_enqueue_script('planner_script');
        wp_localize_script('planner_script','wp_included_vars', array(
                'date' => strtotime('now')
            )
        );
    }

    ...
}

$plugin = new Planner_Plugin();
?>

When everything is generated, i'll have multiple register for event listener (6 times) so it will trigger my function 6 time everytime i made an action in my component. But the hook "wp_enqueue_scripts" have to be triggered only 1 time, no ? How can i fix this ?

EDIT

In my class, i have this too :

function display_planner(){
    ?>
    <div class="planner">
        <div class="calendar">
            <div class="calendar-header">   
                <div class="headerWrapper">
                    <a class="btn btn-prev previousYear">
                        <i class="icon-angle-left"></i>
                    </a>
                    <span class="currentYear">July</span>
                    <a class="btn btn-next nextYear">
                        <i class="icon-angle-right"></i>
                    </a>
                </div>

                <div class="headerWrapper">
                    <a class="btn btn-prev previousMonth">
                        <i class="icon-angle-left"></i>
                    </a>
                    <span class="currentMonth"></span>
                    <a class="btn btn-next nextMonth">
                        <i class="icon-angle-right"></i>
                    </a>
                </div>
            </div>
        </div>          
    </div>
    <?php
}

So when i refresh, the page, it will put multiple event listener to previous Month/year and nextMonth/years, as i said. But i have too javascript code, who generate my calendar as well, and by the javascript, it will be added only 1 time.

0

Use a static method - the add_action will fire for each instantiation, but the hook will only be registered once:

add_action( 'wp_enqueue_scripts', __CLASS__ . '::planner_load_scripts' );

static public function planner_load_scripts() {

}
  • it not solved the problem, i always have multiple event listeners for elements loaded by php. But for element loaded by javascript, i only have 1 event listener, as i need. – Nicolas Frbezar May 3 '16 at 14:45
  • Okay I am officially completely lost. Can you try and re-explain your problem in your original question? – TheDeadMedic May 3 '16 at 14:47
  • i've updated the post. – Nicolas Frbezar May 3 '16 at 14:56

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.