Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kingjmaningo/0d05544efbe56e055a0878f26ae48cd9 to your computer and use it in GitHub Desktop.
Save kingjmaningo/0d05544efbe56e055a0878f26ae48cd9 to your computer and use it in GitHub Desktop.
Enqueue styles and scripts in Wordpress
<?php
add_action( 'wp_enqueue_scripts', 'my_custom_enqueue' );
function my_custom_enqueue() {
// Custom scipt
------------------------//
// Child theme's function.php
wp_enqueue_script( 'custom-js', get_template_directory_uri() . '/path/to/script.js' );
// In your plugin's function.php
wp_enqueue_script( 'custom-js', plugin_dir_url( __FILE__ ) . '/path/to/script.js' );
// Custom styles
------------------------//
// Child theme's function.php
// Main stylesheet
wp_enqueue_style( 'custom-main-css', get_stylesheet_uri() . '/style.css'); );
// Other stylesheets
wp_enqueue_style( 'custom-other-css', get_template_directory_uri() . '/path/to/custom-style.css' );
// In your plugin's function.php
wp_enqueue_style( 'custom-other-css', plugin_dir_url( __FILE__ ) . '/path/to/custom-style.css' );
// Custom Ajax
------------------------//
// Child theme's function.php
wp_enqueue_script( 'custom-ajax-script', get_template_directory_uri() . '/path/to/my-ajax-script.js');
wp_localize_script( 'custom-ajax-script', 'my_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
// In your plugin's function.php
wp_enqueue_script( 'custom-ajax-script', plugin_dir_url( __FILE__ )) . '/path/to/my-ajax-script.js');
wp_localize_script( 'custom-ajax-script', 'my_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
// Font awesome
------------------------//
// Directly from website. Updated version go to https://origin.fontawesome.com/start
wp_enqueue_style('fa-custom-name', 'https://use.fontawesome.com/releases/v5.7.1/css/all.css');
// OR
// version hosted on Bootstrap CDN. (preferred) Updated version go to https://www.bootstrapcdn.com/fontawesome/
wp_enqueue_style('fa-custom-name-bs', 'https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment