Skip to content

Instantly share code, notes, and snippets.

@tedgeving
Last active February 26, 2020 18:50
Show Gist options
  • Save tedgeving/1f238d923f5b37a697845f8f4a6f5887 to your computer and use it in GitHub Desktop.
Save tedgeving/1f238d923f5b37a697845f8f4a6f5887 to your computer and use it in GitHub Desktop.
Useful WordPress Snippets
<?php
/**
* WordPress snippets, add to functions.php
* 1.) Enqueue Google Fonts
* 2.) Disbale emoji support
* 3.) Enqueue style sheets in a specific order
* 4.) Enqueue a single Javascript file correctly
* 5.) Enqueue jQuery correctly
*/
/**
* 1) Enqueue Google Fonts
*/
function google_fonts()
{
$query_args = array(
'family' => 'Josefin+Sans:600|Overpass:400,700,800,900',
'subset' => 'latin'
);
wp_enqueue_style('google_fonts', add_query_arg($query_args, "//fonts.googleapis.com/css"), array(), null);
}
add_action('wp_enqueue_scripts', 'google_fonts');
/**
* 2.) Disable emoji support
*/
function disable_emojis()
{
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
// Remove from TinyMCE
add_filter('tiny_mce_plugins', 'disable_emojis_tinymce');
}
add_action('init', 'disable_emojis');
/**
* 3.) Enque Style Sheets in a specific order
* https://developer.wordpress.org/reference/functions/wp_enqueue_script/
*/
function theme_scripts()
{
wp_enqueue_style('my-style', get_stylesheet_uri());
wp_enqueue_style('my-style-custom', get_template_directory_uri() . '/src/css/styles.css', array('my-style'));
}
add_action('wp_enqueue_scripts', 'theme_scripts');
/**
* 4.) Enqueue a single Javascript file correctly
*/
function js_scripts()
{
wp_enqueue_script('script-name', get_template_directory_uri() . '/js/example.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'js_scripts');
/**
* 5.) Enqueue a Javascript fill with a dependacy using WordPress's copy of jQuery
* and additional call a specific version of jQuery in noconflict mode
*/
function juery_scripts()
{
// Using WordPress's copy of jquery
wp_enqueue_script('consensus-theme', get_template_directory_uri() . '/js/theme.min.js', array('jquery'), null, true);
//Load JQuery 3.2.1
wp_register_script('jquery3.2.1', 'https://code.jquery.com/jquery-3.2.1.min.js');
wp_add_inline_script('jquery3.2.1', 'var jQuery3_2_1 = $.noConflict(true);');
// call it in a JS file
/**
* (function ($) {
*
* }(jQuery3_2_1));
*/
}
add_action('wp_enqueue_scripts', 'js_scripts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment