2

My script doesn't work in Wordpress. I'm trying to convert my HTML page into a Wordpress Theme.. Ive got the CSS linked..

<link type="text/css" rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/min/mycss.css" />     

but my script doesn't connect..

<script type="text/javascript" src="min/myjs.js"></script>

i tried the same , using the <?php bloginfo('template_directory'); ?>

<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/min/myjs.js"></script>

Help?

9
  • you checked where is the actual path of your js and css file Commented Jul 31, 2013 at 10:09
  • What errors does it cause? Commented Jul 31, 2013 at 10:16
  • the Path is correct, because its working in the HTML version. Errors i see is in the dreamweaver.. it goes red. Means not good right?? Commented Jul 31, 2013 at 10:21
  • The way you're closing your php tags looks odd to me. What happens if you remove the space between the ? and > (so ?>)? Commented Jul 31, 2013 at 10:24
  • removed space. still doesnt work.. :-( Commented Jul 31, 2013 at 10:26

2 Answers 2

5

Don't include scripts and stylesheets this way. Use wp_enqueue_scripts. Assuming this is a custom theme, add the code to the functions.php file.

add_action( 'wp_enqueue_scripts', 'theme_scripts_styles' );

function theme_scripts_styles() {

  // Enqueue scripts and styles here

}

In this function use wp_enqueue_script and wp_enqueue_style to queue the files (should be obvious which to use based on the file type!)

Also bloginfo( 'template_directory' ) is not best practice.

Use get_stylesheet_directory_uri(), or get_template_directory_uri() for parent themes. You can drop the _uri part to get the server paths.

So all together it looks something like

add_action( 'wp_enqueue_scripts', 'theme_scripts_styles' );

function theme_scripts_styles() {

  wp_enqueue_style( 'my-styles', get_stylesheet_directory_uri() . '/min/mycss.css', array(), '1.0', 'all' );
  wp_enqueue_script( 'my-scripts', get_stylesheet_directory_uri() . '/min/myjs.js', array(), '1.0', true );

}

Be sure to read the Codex on the enqueue functions so you know what all the parameters do.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow! thanks,,, I guess i have to read this codex.wordpress.org/Function_Reference/wp_enqueue_script
Awesome! its working now.. anyway i just read some tutorails.. some of them registers the path first then enqueue's them .. is it the same on what you did??
You can use wp_register_script, which in turn allows you to enqueue them just using the slug (first parameter). Useful if you want to enqueue scripts conditionally. The method above is simplest where you just want to include a script/stylesheet on every page.
1

Please use this code to add your JavaScript link to your wordpress site.

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/path_to/your_script.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.