JavaScript is not enqueuing

时间:2017-08-17 作者:Pierce

在过去的两个小时里,我一直在挠头,想弄明白为什么我会这样。js文件没有从我的插件排队。原型。php文件的代码如下:

    <?php
/**
 * Plugin Name: Prototype Insert
 * Description: Insert prototype data into database
 * Version: 1.0.0
 * Author: Pierce Burnett
 * License: GPL2
 */


add_action( \'wp_enqueue_scripts\', \'ajax_test_enqueue_scripts\' );
function ajax_test_enqueue_scripts() {
    wp_enqueue_script( \'prototype\', plugins_url( \'/prototype.js\', __FILE__ ), array(\'jquery\'), \'1.0\', true );

    wp_localize_script( \'prototype\', \'postprototype\', array(
        \'ajax_url\' => admin_url( \'admin-ajax.php\' )
    ));

}

add_action( \'wp_ajax_nopriv_post_prototype\', \'post_prototype\' );
add_action( \'wp_ajax_post_prototype\', \'post_prototype\' );

function post_prototype() {
    global $wpdb;

    $prototype_user_id = get_current_user_id();
    $prototype_product_ID = $_REQUEST[\'productid\'];
    $prototype_link = $_REQUEST[\'link\'];
    $prototype_project_link = $_REQUEST[\'project_link\'];

    $wpdb->insert( 
    $wpdb->prefix.\'prototypes\', 
    array( 
        \'user_id\' => $prototype_user_id, 
        \'project_id\' => $prototype_product_ID, 
        \'download_link\' => $prototype_link,
        \'project_link\' => $prototype_project_link
    ), 
    array( 
        \'%d\', 
        \'%d\',
        \'%s\',
        \'%s\' 
    ) 
);
}
下面是我的JavaScript原型。js公司

    //AJAX for prototype list when downloaded 

jQuery(document).on(\'click\', \'.hasprototype a\', function(event){
    alert(\'click\');
        event.preventDefault();
        var linkPrototype = jQuery(this).attr(\'href\');
        var prototypeProductID = jQuery(\'.single-ignition_product .breakout-out\').data(\'productid\');
        jQuery.ajax({
        url : postprototype.ajax_url,
        type : \'post\',
        data : {
            action : \'post_prototype\',
            productid : prototypeProductID,
            link: linkPrototype, 
            project_link: window.location
        },
        success : function() {
            alert(\'success\');
            window.location = linkPrototype;
        }
    });

    return false;
})
我错过了什么?

1 个回复
最合适的回答,由SO网友:RRikesh 整理而成

名称prototype 已被WordPress使用。

在里面wp-includes/script-loader.php, 您会发现这一行:

$scripts->add( \'prototype\', \'https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js\', array(), \'1.7.1\');
如果更改脚本句柄,文件将正确排队:

wp_enqueue_script( \'yourprefix_prototype\', plugins_url( \'/prototype.js\', __FILE__ ), array(\'jquery\'), \'1.0\', true );
您可以在wp_enqueue_script()

结束

相关推荐