0

I have been scratching my head the past two hours trying to figure out why my .js file is not enqueuing from my plugin. The prototype.php file of the code is as follows:

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

And the following is my JavaScript prototype.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;
})

What am I missing?

1 Answer 1

2

The name prototype is already used by WordPress.

In wp-includes/script-loader.php, you'll find this line:

$scripts->add( 'prototype', 'https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js', array(), '1.7.1');

Your file will be correctly enqueued if you change the script handle:

wp_enqueue_script( 'yourprefix_prototype', plugins_url( '/prototype.js', __FILE__ ), array('jquery'), '1.0', true );

You'll find the list of included JS scripts and their handles on the wp_enqueue_script() page.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.