Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

-Wordpress plugin- correctly way to include js files without wp_enqueue_scripts

I've been usually to include JavaScript files in my plugins in the following way:

function A_pubblish_scriptFEnd(){
    $A_scriptFE=plugins_url( 'Js/CPFront.js', __FILE__ );
    wp_enqueue_script( 'CP-js-FE', $A_scriptFE, true );
}
add_action( 'wp_enqueue_scripts', 'A_pubblish_scriptFEnd' );

It worked for every single plugin but now I'm having a problem if I upload more than one plugin WordPress gives me an error, cause I can't repeat wp_enqueue_scripts more than one time.

How can I include in a correct way multiple js files for multiple plugin? What is the correct way to include files in the main php file?

question from:https://stackoverflow.com/questions/65884144/wordpress-plugin-correctly-way-to-include-js-files-without-wp-enqueue-scripts

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the correct way to enqueue scripts.

/**
 * Proper way to enqueue scripts and styles.
 */
function themeslug_enqueue_style() {
    wp_enqueue_style( 'my-theme', 'style.css', false );
}
 
function themeslug_enqueue_script() {
    wp_enqueue_script( 'my-js', 'filename.js', false );
}
 
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );

Having multiple actions on the wp_enqueue_scripts hook is perfectly fine. ( https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/ )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...