Home > OS >  Why isn't my javascript working in wordpress ( Did I link it the right way)
Why isn't my javascript working in wordpress ( Did I link it the right way)

Time:10-15

Here is the function.php

/**
 * Enqueue scripts and styles.
 */
function maxprofessional_scripts() {
    wp_enqueue_style( 'maxprofessional-style', get_stylesheet_uri(), array(), _S_VERSION );
    wp_enqueue_style( 'maxprofessional-main', get_template_directory_uri() . '/css/main.css' );
    wp_enqueue_script( 'maxprofessional-javascript', get_template_directory_uri() . 'js/app.js' );
    wp_enqueue_style( 'bootstrap-icons', 'https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css' );


    wp_style_add_data( 'maxprofessional-style', 'rtl', 'replace' );

    wp_enqueue_script( 'maxprofessional-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _S_VERSION, true );

    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'maxprofessional_scripts' );

Have I linked my javascript file correctly ? Am I doing anything wrong ?

CodePudding user response:

you must register first:

wp_register_script('app-js', get_template_directory_uri() . '/js/app.js', false, '1.0', true);

and then enqueue:

wp_enqueue_script( 'app-js' );

CodePudding user response:

You forgot to use "/" before js directory

 wp_enqueue_script( 'maxprofessional-javascript', get_template_directory_uri() . '/js/app.js' );

Also It's good to use jquery as script dependency. Example

wp_enqueue_style( 'space-style', SPACE_CSS . '/master.css', null, SPACE_VERSION );

wp_enqueue_script( 'space-swiper-script', SPACE_JS  . '/swiper.min.js', ['jquery'], SPACE_VERSION, true );
  • Related