How to add external javascript in WordPress

I want to embed external JavaScript placed in a folder in WordPress. if I enable the use of the script tag, it shows the "<" expected error below my code

global $wpdb; include("/asset/php/chart4php/inc/chartphp_dist.php"); $p = new chartphp(); $p->data_sql = $wpdb->get_results("select t2.name, count(t1.id) as score from custom_status as t2 left join wpsp_ticket as t1 on t2.name = t1.status group by t2.name"); $p->chart_type = "bar"; // Common Options $p->title = "Category Sales"; $p->xlabel = "Category"; $p->ylabel = "Sales"; $color = array("#1AAF5D","#F2C500","#F45B00","#8E0000","#0E948C"); $p->color = $color[rand(0,4)]; $out = $p->render('c1'); 

Tried to include js files

 wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'ajax_search' ); wp_register_script( 'chartphp', plugins_url('/asset/php/chart4php/chartphp.js', __FILE__), array('jquery')); 

wp_enqueue_script ('chartphp');

I also want to include 3 relocation files

 <script src="/asset/php/chart4php/jquery.min.js"></script> <script src="/asset/php/chart4php/chartphp.js"></script> <link rel="stylesheet" href="/asset/php/chart4php/chartphp.css"> 

enter image description here

+5
source share
2 answers

The right way to include scripts in WordPress is wp_enqueue_script () .

If you previously registered the script, you can simply reference it:

You can either bind the script to a previously registered descriptor using the wp_register_script () function, or provide this function to all the parameters needed to connect a script.

Note that if you include default scripts such as jQuery in the dependency parameter, you do not need to register and / or place them separately.

+2
source

In the past, I added external javascript using:

 wp_register_script( 'validation', 'https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) ); wp_enqueue_script( 'validation' ); 

From what I see, you do not queue the script after registering in WP.

Also double check the plugin url path as an additional measure.

+2
source

Source: https://habr.com/ru/post/1264201/


All Articles