簡體   English   中英

如何在我的wordpress插件中添加JavaScript文件?

[英]How to add a javascript file to my wordpress plugin?

我想將共享按鈕集成到我的wordpress插件中,我知道有很多插件可以做到這一點,但是我只想在特定頁面的末尾添加一個按鈕。

我將其添加到我的控制器中,該控制器可加載/注冊其余的javascript,但這無法正常工作。

這是我的控制器代碼

        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')
        ));

這是同一控制器視圖中的代碼

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

有人可以告訴我我在做什么錯嗎?

像這樣做:

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')
        ));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM