Skip to content Skip to sidebar Skip to footer

Directory Structure Issue When Calling A Js File And Ajax

I can not seem to make the call to the proper JS file location despite trying everything that I can think of. Below is the way that it should be from everything I know to find that

Solution 1:

If you check my old answers, I have already fixed this issue for you as well.

Proper way:

wp_enqueue_script('myscript', get_template_directory_uri() . '/assets/js/myjsfile.js', array('jquery'), false, true);

Use get_template_directory_uri() to get the correct URI of the active theme so that it will point to the root of your theme.

https://mywebsite.com/wp-content/themes/mytheme

If you are using a child theme, use get_stylesheet_directory_uri().

Update:

// JavaScript Document
jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    // add your action inside data objectdata: { 
      action: 'waitlist_update' 
    },
    success: function(data){
        // callback function
    }
});

PHP

add_action('wp_ajax_waitlist_update', 'update_function');
    
functionupdate_function() {
  global$wpdb;
  $results = $wpdb->query( $wpdb->prepare("UPDATE 'my_table' SET `currentstatus` = 'myupdate1' WHERE wdt_ID = '1'"));
  die($results);
}

wp_ajax_waitlist_update the waitlist_update is the action that ajax will trigger from your JavaScript. update_function is the function that will be called after the Ajax is triggered.

Solution 2:

It would be easier to just specify the full path to the file in the head of your html document, example;

 https://mywebsite.com/wp-content/themes/"mytheme"/assets/js/myjsfile.js 

Or in PHP set a path equal to the document root of your site plus folders that point to the final location of the file;

// Check the correct folder your script is located in$path = $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/"mytheme"/assets/js/";

Now include your js file in the HTML

<scriptsrc='<?php$path."myjsfile.js"; ?>'></script>

Post a Comment for "Directory Structure Issue When Calling A Js File And Ajax"