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();
is_page_template()
in place ofis_page()
...