简体   繁体   中英

How to add a javascript file to my wordpress plugin?

I want integrate a share button to my wordpress plugin, I know there are many plugins that will do it, but I just want a button at the end of a specific page.

I added this to my controller, the one that loads/registers the rest of the javascripts, but this is not working.

Here is my code from the controller

        wp_enqueue_script(
            'wpProQuiz_front_javascript',
            plugins_url('js/wpProQuiz_front' . (WPPROQUIZ_DEV ? '' : '.min') . '.js', WPPROQUIZ_FILE),
            array('jquery-ui-sortable'),
            WPPROQUIZ_VERSION,
            $footer
        );

        wp_register_script(                  //this is the js file I am registering
            'share_button_javascript',
            plugins_url('js/shareButton.js', __FILE__)

        );

        //wp_enqueue_script('share_button_javascript');

        wp_localize_script('wpProQuiz_front_javascript', 'WpProQuizGlobal', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'loadData' => __('Loading', 'wp-pro-quiz'),
            'questionNotSolved' => __('You must answer this question.', 'wp-pro-quiz'),
            'questionsNotSolved' => __('You must answer all questions before you can completed the quiz.',
                'wp-pro-quiz'),
            'fieldsNotFilled' => __('All fields have to be filled.', 'wp-pro-quiz')
        ));

And here is the code in the view of the same controller

<script>var shareButton = new ShareButton()</script>
<sharebutton>Share</sharebutton>

Can someone tell me what am I doing wrong here?

Do it like this:

wp_enqueue_script(
            'wpProQuiz_front_javascript',
            plugins_url('js/wpProQuiz_front' . (WPPROQUIZ_DEV ? '' : '.min') . '.js', WPPROQUIZ_FILE),
            array('jquery-ui-sortable'),
            WPPROQUIZ_VERSION,
            $footer
        );

        //this is the js file You are registering

        wp_register_script('share_button_javascript', plugins_url('js/shareButton.js', __FILE__), array('jquery'), '', true);
       wp_enqueue_script('share_button_javascript');

        //wp_enqueue_script('share_button_javascript');

        wp_localize_script('wpProQuiz_front_javascript', 'WpProQuizGlobal', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'loadData' => __('Loading', 'wp-pro-quiz'),
            'questionNotSolved' => __('You must answer this question.', 'wp-pro-quiz'),
            'questionsNotSolved' => __('You must answer all questions before you can completed the quiz.',
                'wp-pro-quiz'),
            'fieldsNotFilled' => __('All fields have to be filled.', 'wp-pro-quiz')
        ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM