Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For efficiency we are trying to hook some JS scripts only a certain page template of a theme that is to hold a form: page-with-form.php

On the theme functions.php we've defined a init_method as such:

function my_init_method(){

    $dir = get_bloginfo('stylesheet_directory')."/js/jquery.js";
    wp_deregister_script('jquery');
        wp_register_script('jquery',$dir);
    wp_enqueue_script('jquery');

    $dir = get_bloginfo('stylesheet_directory')."/js/jquery.validate.min.js";
    wp_deregister_script('jquery.validate.min');
        wp_register_script('jquery.validate',$dir);
    wp_enqueue_script('jquery.validate.min');

    $dir = get_bloginfo('stylesheet_directory')."/js/funcion.js";
    wp_deregister_script('funcion');
        wp_register_script('funcion',$dir);
    wp_enqueue_script('funcion');
}    

We then expected we could just add at the firts line of page-with-form.php page template (before get_header()) :

add_action('init', 'my_init_method');
  • Is this not possible?
  • Are we forced to add an if rule to the functions.php function to detect that we are on that page?

Code modifications based on answers: On the page template load the function directly, no need to add_action("init"...

my_init_method();
...
get_header();
share|improve this question
    
For clarification, because i believe responders may have overlooked this point in your question. Are you referring to running the above code on a page that has a specific page template attached to it? (that's how i read the opening part of your question). In that case it would probably be more ideal to use is_page_template() in place of is_page() ... –  t31os Aug 1 '11 at 13:53
    
yes, either way would work but I was refering to page_template –  luison Aug 4 '11 at 13:06
add comment

3 Answers

up vote 7 down vote accepted

init is much earlier than page template loading and not appropriate place for enqueues (despite many tutorials and docs using it for that).

Hook your function to wp_enqueue_scripts and make sure you are doing that hooking before wp_head() call in template.

share|improve this answer
    
Understood now that the page-template is too late for init action and what you meant was call a personalized function. We've now just called my_init_method() as such with no init action and it works. Other alternatives to load conditionally if the page or page_template should also work but we have not tested them. –  luison Aug 4 '11 at 13:09
    
@luison Just hooking function in template will work, but as per my answer it is best practice to hook enqueues to wp_enqueue_scripts hook. –  Rarst Aug 4 '11 at 15:06
    
And the point being, you can call is_page_template() inside the callback on wp_enqueue_scripts and get the correct return value(true/false), where as init would have been too early to call is_page_template() or the like. Just clarifying incase it's not clear. –  t31os Aug 5 '11 at 0:01
    
I'm going to update the wp_enqueue_scripts codex entry today, about time those bad examples were replaced(since it's encouraging users to enqueue on init). –  t31os Aug 7 '11 at 11:36
    
@t31os looking forward to it! I am mostly busy creating missing pages for function reference lately, so no resolve left for editing. :) –  Rarst Aug 7 '11 at 13:53
show 2 more comments

When you loaded your template file, you already passed the 'template_redirect' hook, which is too late (See: Plugin/Action API Reference).

You need to set the add_action call in your functions.php file, which is loaded before the after_setup_theme hook - which is the first available hook for themes.

share|improve this answer
add comment

In addition to using the correct hook (as pointed out by @Rarst), you will need to move your functional code to functions.php, and wrap the function contents in an if ( is_page( 'page-with-form' ) ) conditional.

share|improve this answer
    
Moving to functions.php not really required. If it's more convenient to keep this in template there is no issue with that. –  Rarst Aug 1 '11 at 15:48
    
I disagree. for various reasons, function definitions belong in functions.php. –  Chip Bennett Aug 1 '11 at 18:05
    
I am merely stating technical nuance, not claiming that everything in template would be best practice. Also as per original question function definition itself is in functions.php, they only want hook directive in template (which is just fine as for me). –  Rarst Aug 1 '11 at 18:37
    
I should have clarified: the OP states that the add_action() call is in the template file; that's the one that should be moved to functions.php. But even WRT technical nuance, the template loads at a different point in the execution from when functions.php loads, so adding add_action() calls in the template file might not work, from a technical perspective, depending on what hook is being called. –  Chip Bennett Aug 1 '11 at 19:02
1  
I disagree that add_action() must be in functions.php. Even though it is common (and good) practice it is not always possible/desireable/easy to have all adds centralized. I mentioned in my answer that it should be put before wp_head() call if kept in template. –  Rarst Aug 1 '11 at 20:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.