Including multiple files

There are different ways to include multiple files.

The functions file can fairly fast become very long with a lot of code. One can then split off the code into logical files and then add the file to the functions file.

PHP:
require_once( get_stylesheet_directory() . '/inc/widgets.php' );

Options:
get_stylesheet_directory = To get the absolute path to the child theme directory.
get_stylesheet_uri = looking through the child theme directory (is short for get_stylesheet_directory_uri()). Is used to retrieve the style.css file.

get_template_directory = looks to the parent theme.
get_template_directory_uri = looks to parent directory to retrieve the style.css file.

https://codex.wordpress.org/Function_Reference/get_template_directory

Examples of enqueuing js and css files

// Example from: https://developer.wordpress.org/themes/basics/including-css-javascript/
wp_enqueue_script( 'script', get_template_directory_uri() . 
'/js/script.js', array ( 'jquery' ), 1.1, true);
//* Enqueue Google fonts - https://my.studiopress.com/tutorials/load-google-fonts/
add_action( 'wp_enqueue_scripts', 'load_google_fonts' );
function load_google_fonts() {
 wp_enqueue_style( 'google-font-oswald', '//fonts.googleapis.com/css?family=Oswald:400', array(), 1.0 );
};


//* Enqueue sticky menu script
add_action( 'wp_enqueue_scripts', 'custom_enqueue_script' );
function custom_enqueue_script() {
 wp_enqueue_script( 'sticky-menu', get_stylesheet_directory_uri() . 'assets/js/sticky-menu.js', array( 'jquery' ), '', true );
}


//* Loading Font Awesome - https://sridharkatakam.com/using-font-awesome-wordpress/
add_action( 'wp_enqueue_scripts', 'enqueue_font_awesome' );
function enqueue_font_awesome() {
 wp_enqueue_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.css' );
}

 

Resources:
https://developer.wordpress.org/themes/basics/including-css-javascript/
https://community.getbeans.io/discussion/how-paste-a-code-in-the-head-of-the-site/
https://community.getbeans.io/discussion/extracting-code-from-functions-php-to-external-php-files/
https://www.wpexplorer.com/javascript-wordpress/

No comment yet, add your voice below!


Add a Comment

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

Comment *
Name *
Email *
Website