How to Link a Script In WordPress

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..

Read More
<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?

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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>