Using the Functions.php file in WordPress

I have been trying so hard to make my new custom WordPress theme the RIGHT way. And I am hitting a wall.

Below is the code, which I am quite confident is correct. But it is not registering with my local site.

<?php

// adding my stylesheet filef

unction digitaldewtour_style() {

wp_enqueue_style(‘main_css’, get_template_directory_uri() . ‘style.css’ );
}

// calling the function i just defined above

add_action( ‘wp_enqueue_scripts’, ‘digitaldewtour_style’ );

// adding my 2 javascript files

function digitaldewtour_js() {

wp_enqueue_script( ‘slider_js’, get_template_directory_uri(). ‘/js/slider.js’, array(‘jquery’), ”, false );

wp_enqueue_script( ‘script_js’, get_template_directory_uri(). ‘/js/script.js’, array(‘jquery’), ”, false );

}

 

// calling my new function
add_action( ‘wp_enqueue_scripts’, ‘digitaldewtour_js’ );

?>

I added a few notes in the actually code, but I just want to point out of few things for posterity.

First of all the process for using your functions.php file to call for your stylesheets or your javascript files works like this. First you create a function which “enqueues” the thing you want to link to. THEN you call that function. That call says “Hey, website. Add digitaldewtour_js to this page.” Your website/computer then looks for the function you are calling and then that functions defines exactly what it is looking for and going to call. NEAT. I just need to figure out why it doesn’t work for me.

  1. The function name is not necessarily important (ex. “digitaldewtour_style”). It just needs to unique enough that you don’t already have a function with the same name. WordPress has a lot of its own functions (so do random plug ins), so make it unique enough for you. Other than that the function name can be whatever you want.
  2. get_template_directory_uri() is a WordPress function that dynamically finds your sites theme directory. This is powerful because you don’t have to look up the url each time and for every website/theme. It will find it itself. Also the period is important it uses the path that the WordPress functions pulls up and ADDS it to whatever you put in quotations (‘style.css’).
  3. Creating a function for a javascript file is a little more complex than a style sheet. It starts the same: name the function, find the url where the page is, and then you have more options. The next field is where you can include another part, like another javascript library. I typically use jQuery (because I haven’t branched out yet… ) and WordPress already has jquery included. So I just include array(‘jquery’) and my javascript file is run with jQuery. I forget what the next field is. But the last one is asking “Do you want to call this file in your footer?” so I put false.
  4. ONE MORE IMPORTANT THING I FORGOT AND WAS TRYING TO REMEMBER… WordPress does not like “$” in jQuery. It uses other libraries that use “$”s so it’s TOO CONFUSING. If you are including some javascript with jQuery just make sure to either add a function that converts all of those “$”s or use “jQuery” in place of said “$”s.

No comments

Leave a Comment

Your email address will not be published. Required fields are marked *