E
E
Endru2015-06-09 14:46:47
CMS
Endru, 2015-06-09 14:46:47

Wordpress how to include certain js on one page?

Good afternoon everyone. Tell me how in wordpress to connect certain JS on one page, others on another. Head closes in header.php, and body opens in the same place.
those. you need to connect in the body itself, is it real?
And if not, what are the other ways?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
E
Elmechador, 2015-06-10
@Elmechador

WordPress has great conditional tags, like is_page() , where you can pass the page/post ID in the parameters and get the result accordingly:
if (is_page(id)){
wp_enqueue_script('needFullScript'); // In the body of the function, we connect the desired script, before that we register it using wp_register_script()
}

A
alex1nd, 2016-05-18
@alex1nd

is_page - for pages
is_single - for writing

////////////////
function my_styles() {
    if ( is_page( 933 ) ) {
        //подключаем стиль
        wp_enqueue_style ( 'contact', get_template_directory_uri() 
            . '/altercss.css', array(), '1.0' );  
            //подключаем скрипт
            wp_enqueue_script('alterscript', get_template_directory_uri() . '/alterscript.js');     
    }
}
add_action( 'wp_enqueue_scripts', 'my_styles' );
//////////////////

I checked everything works, you only need to change the ID and Paths to the files

D
Dmitry Shinkar, 2015-06-09
@DeadCowsDontMoo

Now, of course, I’ll write nonsense, but since no one writes - as they say in fishlessness and a diver - a snack.
I see 2 options:
the first - through some if ... (I hope you understand what I mean)
the second - if you don’t have 100500 pages there - connect everything (and even though everything should work, I give a 100% guarantee that this option is not right)

K
Kirill Krivitsky, 2015-06-09
@KrivitskiyKirill

You can create your own page template and write the plug-in JS in the head at the top, you can read how to do this here or here

A
Alexander Lead, 2015-06-09
@alexlead

There is no universal answer. If you have something simple use JS in body:
https://learn.javascript.ru/external-script

E
Evgeny Kolman, 2020-11-20
@Evgeny_13

I found a solution like this:
1. Add a hook to init.php

add_action( 'wp_enqueue_scripts', 'alps_gutenberg_blocks_scripts' );
function alps_gutenberg_blocks_scripts(){
  wp_enqueue_script( 'alps_gutenberg_front', plugins_url('src/front.js', plugin.php'), array( 'jquery' ) );
}

2. Add a jQuery script and add the path to it in the hook
and that's it)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question