In this post, we are adding very useful and important functions for WordPress.
We already have learned to create a child theme in WordPress. Now we can use these important WordPress functions to customize the child theme further.
Load Multiple CSS in WordPress Child Theme
In WordPress child theme, you may need to add more than one stylesheet. Like loading additional font icon using FontAwesome etc. We can include multiple CSS files to WordPress theme to customize it further.
Here is the correct way to do this -
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('parent-style') );
//load additional css file from the child theme directory
wp_enqueue_style( 'style-rwd', get_stylesheet_directory_uri().'/rwd.css',array());
//load additional css file from the child theme directory
wp_enqueue_style( 'style-form', get_stylesheet_directory_uri().'/form.css',array());
}
So this way we can add multiple CSS style sheets in WordPress Child Theme.
Enqueue JS in WordPress
In the child theme, we have added CSS but for enqueueing Scripts add this in your functions.php -
// JS Enqueue
function custom_js_enqueue() {
wp_enqueue_script(
'custom-script',
get_stylesheet_directory_uri() . '/owl-car/owl.carousel.min.js',
array( 'jquery' )
);
}
Adding Google fonts in WordPress
To add custom Google fonts in our theme we simply add the below code -
//Enqueue google fonts
function load_fonts() {
wp_register_style('googleFonts', 'https://fonts.googleapis.com/css?family=Oswald:400,700|Source+Sans+Pro:300,400,700|family=Open+Sans:300');
wp_enqueue_style( 'googleFonts');
}
add_action('wp_print_styles', 'load_fonts');
We have added three fonts 'Oswald, Source Sans Pro, and Open Sans' in one URL using the above function.
Add custom php in WordPress shortcode
Add custom php variables inside shortcode. For that we can prepare the data beforehand and then put it inside the shortcode -
<?php
$cat = get_term(12);
$post_slug = get_post_field( 'post_name', get_post());
$scroll_to_news_url = get_category_link($cat) .'#'. $post_slug;
// Now use variables inside shortcode
echo do_shortcode( '[addtoany url="' . $scroll_to_news_url . '"]' );
?>
Stop WordPress core and plugins update notification in Admin Dashboard
WordPress frequently releases updates from time to time and keeps us notifying in the admin Dashboard. This can be muted.
To disable all core updates in WordPress and updates notifications of plugins as well, we add the below code.
function remove_core_updates(){
global $wp_version;return(object) array('last_checked'=> time(),'version_checked'=> $wp_version,);
}
add_filter('pre_site_transient_update_core','remove_core_updates');
add_filter('pre_site_transient_update_plugins','remove_core_updates');
add_filter('pre_site_transient_update_themes','remove_core_updates');
Remove scripts from head to footer in WordPress
Scripts in the head may affect the site loading speed. It is always a good idea to add scripts in the footer. If you are using many plugins and scripts in your child theme the below function can be useful for you. This will speed up your site for sure. The function will move all the scripts from header to footer. But this may break your site while loading the page. To move all the Scripts from header to footer add the below code -
function footer_enqueue_scripts() {
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);
}
add_action('after_setup_theme', 'footer_enqueue_scripts');
Clean WordPress Head
If you check your WordPress Source code you will find lots of URL in the head section. Those links can be removed and won't affect the theme much.
To clean the head and speed up the load time simply use the below code in functions.php
// REMOVE JUNK FROM HEAD
remove_action('wp_head', 'rsd_link'); // remove really simple discovery link
remove_action('wp_head', 'wp_generator'); // remove wordpress version
remove_action('wp_head', 'feed_links', 2); // remove rss feed links (make sure you add them in yourself if youre using feedblitz or an rss service)
remove_action('wp_head', 'feed_links_extra', 3); // removes all extra rss feed links
remove_action('wp_head', 'index_rel_link'); // remove link to index page
remove_action('wp_head', 'wlwmanifest_link'); // remove wlwmanifest.xml (needed to support windows live writer)
remove_action('wp_head', 'start_post_rel_link', 10, 0); // remove random post link
remove_action('wp_head', 'parent_post_rel_link', 10, 0); // remove parent post link
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // remove the next and previous post links
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );
Disable 'autop' in WordPress
This one is the smallest one but can save you time. WordPress generates '<p></p>' in visual editor to create new lines. This is termed as 'wpautop'. To disable wpautop from WordPress put this in your functions.php -
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
Remove WordPress Version completely
It is better to hide the WordPress version for security. The below code will completely remove your WordPress version.
// Remove WP Version
function remove_wp_version() {
return '';
}
add_filter('the_generator', 'remove_wp_version');
Hide specific category from homepage or blog page
Sometimes we may want to remove some categories from blog page or homepage. Using below function, multiple categories can be removed separated by comma < , > eg; -4 and n is the category id.
function remove_category( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-4', 'n' );
}
return $query;
}
add_filter( 'pre_get_posts', 'remove_category' );
Disable 'continue reading' link to jump from WordPress post
Though this is a good feature, it may be annoying for some users. By default 'continue reading' button will take us to just next line of the post from where we left reading the excerpt of the post. This sometimes breaks the reading flow of the post. So, to remove the jump, simply add the below code in your WordPress functions.php -
function no_read_more_jump($link) {
$offset = strpos($link, '#more-');
if ($offset) { $end = strpos($link, '"',$offset); }
if ($end) { $link = substr_replace($link, '', $offset, $end-$offset); }
return $link;
}
add_filter('the_content_more_link', 'no_read_more_jump');
Echo WordPress shortcodes in the template
Shortcodes are very useful in WordPress. Whether we are using the contact form or any slider etc. a shortcode help us to display the content using a small line of code. But sometimes we need to use the WordPress shortcodes in template files. To echo WordPress shortcodes in php template use the below code -
<?php echo do_shortcode('[contact-form-7 id="715" title="subscribe"]');?>
Disable update notification for individual plugins
Sometimes you may need to stop the update notifications for any individual plugins in the plugin dashboard of WordPress. To do this -
function filter_plugin_updates( $value ) {
unset( $value->response['my-plugin/my-plugin.php'] );
return $value;
}
add_filter( 'site_transient_update_plugins', 'filter_plugin_updates' );
Just replace the 'my-plugin/my-plugin.php' with the plugin, you do not want to receive the update notification.