Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WP – How to add a script to a specific page

  • This topic is empty.
Viewing 9 posts - 31 through 39 (of 39 total)
  • Author
    Posts
  • #182135
    Senff
    Participant

    is_page_template(templateName) IS the if statement I was referring to ;)

    The problem is that a template check can’t be called just everywhere you like — apparently it only returns something when it’s called in The Loop, so I’m trying to find another way to check for the template.

    #182136
    chrisburton
    Participant

    is_page_template(templateName) IS the if statement I was referring to ;)

    Sorry. Was directing my statement to the OP.

    The problem is that a template check can’t be called just everywhere you like — apparently it only returns something when it’s called in The Loop, so I’m trying to find another way to check for the template.

    This guy is doing it (even with the if statement) and it works. Hmm.

    http://wordpress.org/support/topic/conditional-wp_enqueue-with-is_page_template

    Resources:

    #182138
    Senff
    Participant

    I just found out thatis_page_templateonly works if it’s an actual PAGE — it won’t work on POSTS.

    Instead, the check should be done on something like this:

    if( 'mycustomtypename' == $post_type ) {
        wp_enqueue_script( 'googleapi' );
        wp_enqueue_script( 'googlemaps' );
    }
    

    This guy is doing it (even with the if statement) and it works. Hmm.

    I don’t think he actually said it works with that conditional statement…. I may be wrong. @taxicss can you elaborate please?

    #182140
    chrisburton
    Participant

    I don’t think he actually said it works with that conditional statement

    You’re right. It was another user on the WP forum that said it worked. Still kind of weird.

    #182152
    Alen
    Participant
    
    add_action( 'wp_enqueue_scripts', 'init_theme_scripts_styles' );
    function init_theme_scripts_styles()
    {
    
      global $wp_styles;
    
      # scripts
      # following code echos jQuery
      wp_enqueue_script( 'jquery' );
    
      # wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
      # Following code loads master.min.js from the js folder in your theme
      # see: http://codex.wordpress.org/Function_Reference/wp_enqueue_script
      wp_enqueue_script( 'main', get_template_directory_uri() . '/js/master.min.js', ['jquery'], '1.0', true );
    
      # this is a conditional to check if we are on certain page, then we echo our whatever script
      # you can do these conditionals for both, the CSS/JS within this code
      if ( is_page( 'ENTER-SLUG-HERE' ) ) {
        wp_enqueue_script( 'NAME', get_template_directory_uri() . '/js/NAME.js', ['jquery'], '', true );
      }
    
      # styles
      # wp_enqueue_style( $handle, $src, $deps, $ver, $media );
      # see: http://codex.wordpress.org/Function_Reference/wp_enqueue_style
      # following code loads master.min.css file from the css folder in your theme
      wp_enqueue_style( 'main', get_template_directory_uri() . '/css/master.min.css', [], '1.0', 'all' );
    }
    
    #182153
    Alen
    Participant

    in the code above if you’re using older version of php, you might need to replace,

    ['jquery'] with array('jquery')

    -Alen

    #182157
    Senff
    Participant

    No, that won’t work. As stated before, the script needs to be loaded when it’s a custom POST.

    if ( is_page( 'ENTER-SLUG-HERE' ) ) { will only work if it’s a PAGE. What might work is IS_SINGULAR.


    @taxicss
    can you try this for fun?

    // add google maps
        function load_google_script() {
            wp_register_script( 'googleapi', "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false");
            wp_register_script( 'googlemaps', get_template_directory_uri() . '/js/googlemaps.js', array(), '1.0.0', true );
    
        if(is_singular('YOURPOSTTYPE')) {
            wp_enqueue_script( 'googleapi' );
            wp_enqueue_script( 'googlemaps' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'load_google_script' );
    

    Not tested but give it a try. If it doesn’t work I’ll have to check it at home later today.

    #182161
    taxicss
    Participant

    @Senff Yes sir it’s working now with your last code. Thanks a lot. Now maybe I’ll try to make it more better by making it a plugin instead like you said.

    Again, thanks to all @Senff, @chrisburton, @Alen

    #182179
    Alen
    Participant

    Oh sorry guys somehow I missed the part about custom posts. Need to stop multitasking.

Viewing 9 posts - 31 through 39 (of 39 total)
  • The forum ‘Back End’ is closed to new topics and replies.