1

I am writing my first plugin and I have this code in the main plugin php to replace home page with custom template

add_filter( 'template_include', 'replace_home_page' );

function replace_home_page( $template ) {
      if (is_home()){
    return plugin_dir_path( __FILE__ ) . 'mhomepagetemplate.php';
  }
  return $template;
}

before above code I call code to register some javascript

    $main_js_name='mhp_main.js';

    function load_js(){
        wp_enqueue_script($main_js_name,plugins_url($plugin_name.'/js/'.$main_js_name),array( 'jquery' ));
echo    '<BR><BR>'. plugins_url($plugin_name.'/js/'.$main_js_name);
    }

    add_action('wp_print_script','load_js');

the javascript file contains only one line now

console.log('test from js');

I was expecting to see the text text from js in javascript console but it is not there. I cannot find the code anywhere on the home page.

The custom template calls <?php wp_head(); ?>

Could someone help me to make the javascript work?

2

The action is plural: wp_print_scripts, however, the action you want to enqueue scripts on is wp_enqueue_scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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