How-to add features and functionality to your WordPress theme by using code snippets?
At the core of any WordPress theme, there is a file named functions.php and themes created with Pinegrow or Pinegrow Theme Converter for WordPress are no exceptions.
For WordPress developers, customising functions.php is one way to change the default behaviours of WordPress. The functions file behaves like a WordPress Plugin, adding features and functionality to a WordPress site.
When you create a theme with Pinegrow or Pinegrow Theme Converter for WordPress, the functions.php file is automatically created in your project folder, then, when you export the theme, it is exported in the theme’s subdirectory in wp-content/themes.
Note: The file executes only when in the currently activated theme’s directory and applies only to that theme. If the Theme is changed, the functionality is lost.
Important: Any modification made to the functions.php file located in your project folder will be exported to your theme folder, but the opposite is not valid. If you modify the functions.php file directly from your theme folder, your modifications will be destroyed during the next export.
How-to proceed with Pinegrow or Pinegrow Theme Converter
The most simple way is to add your code snippets into the functions.php file available in the project folder and save the changes.
The changes will be exported to the theme during the export process.
Your custom code must be added after the following line:
/* Pinegrow generated Include Resources End */
Although effective and perfectly fine, this method can however suggest that the code already available in functions.php and which is automatically generated by Pinegrow can be tweaked/modified, which is definitely not recommended because the theme export procedure will always erase all the changes.
To simplify and make the process a bit more secure, you can do the following:
- Add the following snippet at the very end of your functions.php file so in addition to executing its contents, functions.php will load and execute – once – the contents of the
inc/custom.php
file.
/* Don't add custom your custom snippets here, but use inc/custom.php */
require_once "inc/custom.php";
2. Create both the inc
folder and inside this folder, create the custom.php
file.
Edit the file and add the following code. (so you will remember what it’s about in the future…)
<?php
/*
This file is included into functions.php
Use this file to add custom PHP code to functions.php
*/
?>
From now on, basically, if you want to add extra code snippets to your WordPress theme, you just have to EDIT the file custom.php, save it and export your theme.
From custom.php, you can call functions, both PHP and built-in WordPress, and to define your own functions. Just like you would do by adding code in functions.php.
Code snippets examples
Here are a few code snippets examples from the custom.php of the Pinegrow Starter Theme 2.
The description of each snippet is available in the corresponding comment.
//* Add the ability to use shortcodes in widgets
add_filter('widget_text', 'do_shortcode');
//* Prevent WordPress from compressing images
add_filter('jpeg_quality', function($arg){return 100;});
//* Disable any and all mention of emoji's
//* Source code credit: http://ottopress.com/
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 items from the <head> section
remove_action('wp_head', 'wp_generator'); //* Remove WP Version number
remove_action('wp_head', 'wlwmanifest_link'); //* Remove wlwmanifest_link
remove_action('wp_head', 'rsd_link'); //* Remove rsd_link
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); //* Remove shortlink
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); //* Remove previous/next post links
Recommendations
Here are two important recommendations to make the process run as smoothly as possible:
- Once you have edited the
custom.php
file, save it. As long as the * sign remains visible in the tab of the file being edited, it means that it has not been saved.
You can also use an external editor (Atom, Visual Studio Code, Sublime …) to edit the file.
- Don’t forget to export your theme after your changes whether you use Pinegrow or an external editor to edit the custom.php file.
Summary
If you want to add extra features to your theme:
- You should never modify the code automatically generated by Pinegrow in functions.php as your changes will always be overwritten automatically each time you will export your theme.
- Avoid using functions.php directly and add all your code snippets to
inc/custom.php
. - After you edit the custom.php file, always verify that the file is saved before exporting the theme.
- Don’t forget to export the changes to your theme folder.
One more thing …
With the very next version of Pinegrow coming shortly, both the inc
folder and the custom.php
file will be automatically created by Pinegrow, so you will just have to focus on adding awesome functions to your theme.
Happy customising!