WP Rig Toolkit offers numerous customization options. Some of these can be turned into components, while others are relatively small. During your project development, you may need to store your WP customizations somewhere. Writing a plugin may not always be necessary if your customization is small and/or cannot be separated from the theme.
WP Rig Toolkit offers many customizations, some of which can be transformed into components and others which are small. When working on a project, it is often necessary to store functions and customizations somewhere in WordPress.
While it is not always necessary to create a plugin for small customizations that cannot be detached from the theme, it is important to note that a large and complex functions.php file may not be the best option for a smooth development process. To address this, we have separated functions.php into multiple partial .php files located in the functions folder in the root directory.
π functions.php
Default content
<?php/** * Main Theme Functions and Definitions * * This file contains the core functions and hooks necessary to power the WP Rig theme. * * @link https://developer.wordpress.org/themes/basics/theme-functions/ * @package wp_rig *//** * WP Rig Init. */requireget_template_directory().'/functions/_functions-rig-init.php';/** * WordPress Customizations. */define('STOP_ADDING_P_TAGS',true); // Stop WordPress from adding <p> tags.define('DISABLE_VISUAL_EDITOR',false); // Disable Visual editor completely.define('DISABLE_VISUAL_EDITOR_ADMINS_ONLY',false); // Disable Visual editor only for admins.define('HEADERS_CLEAN_ARCHIVE_CATEGORY',true); // Clean page headers from "Archive:" and "Category:".define('ADD_PAGE_EXCERPTS_SUPPORT',true); // Add Post Excerpts Support for Pages.define('DISABLE_COMMENTS',true); // Disable Comments on the site.define('DISABLE_WP_ALL_THUMBNAILS_GENERATING',false); // Completely disable WordPress Image Resizing. You may decide that it's not needed in your project.define('DISABLE_WP_DEFAULT_THUMBNAILS_SIZES',true); // Disable only certain WordPress thumbnails sizes. Customize it if needed.define('DISABLE_WP_IMAGE_SCALING',false); // Disable WordPress Image Scaling. By default WP limits height for you images to 256px. This filter removes it.define('ADD_CUSTOM_HEADER_SUPPORT',true); // Register shortcode for custom h1 heading for post and pages and automatically render if custom h1 is set./** * Plugins Customizations. */define('CF7_DISABLE_DEFAULT_LOADING_JS_CSS',true); // Contact Form 7: Disable automatic loading of CF7 .js and .css.define('CF7_CLEAN_MARKUP',true); // Contact Form 7: Remove <p> and <br> from Contact Form 7.define('YOAST_BREADCRUMBS_ADD_SCHEMA',true); // YoastSEO: Add Schema to YoastSEO breadcrumb.define('YOAST_BREADCRUMBS_CLEAN_SPAN',true); // YoastSEO: Filter the output of Yoast breadcrumbs to remove <span> tags added by the plugin.define('YOAST_SEARCHACTION_JSON_DISABLE',true); // YoastSEO: Remove SearchAction from yoast-schema-graph JSON structured data. If you don't have a working search on your site (for example you have a generated a static site from your WP).define('ELEMENTOR_REMOVE_STANDART_FONTS',true); // Elementor: Remove default fonts from loading./** * Optimization and code cleaning. */define('DEFER_ALL_SCRIPTS',true); // Defer all scripts (exl. jquery and wp-admin scripts).define('DISABLE_SCRIPTS_STYLES_VERSIONS',false); // Disable versions in the end of scripts and styles links.define('DISABLE_TYPE_ATTRIBUTES',true); // Disable type attributes from scripts and styles.define('DISABLE_DEFAULT_BLOCK_STYLES',true); // Remove default block styles.define('DISABLE_WP_POLYFILL',true); // Remove WP Polyfill js./** * Components settings. *//** * Define Daynight default mode. * * Define your original version of website. * Acceptaple values: 'day', 'night'. * * https://docs.wprig.org/coming-soon */define('WP_RIG_DAYNIGHT_DEFAULT_MODE','day');/** * ============ * * Basic Customizations * * Must-have and not configurable through contsants above. * ============ */// Menu functions (registering new menus, menu walkers).requireget_template_directory().'/functions/_functions-menu.php';/** * ============ * * Registeting new types * * Register new thumbnails size, shortcodes etc. * ============ */// Register new thumbnail sizes.requireget_template_directory().'/functions/_functions-custom-thumbnails.php';// Register new ACF fields.if ( class_exists('ACF') ) {requireget_template_directory().'/functions/_functions-acf-fields.php';}// Register custom actions.requireget_template_directory().'/functions/_functions-custom-actions-filters.php';// Register custom shortcodes.requireget_template_directory().'/functions/_functions-custom-shortcodes.php';// Enqueue/dequeue custom files.requireget_template_directory().'/functions/_functions-custom-files.php';/** * ============ * * WordPress Customizations * ============ */requireget_template_directory().'/functions/_functions-wp-customizations.php';/** * ============ * * Plugins Customizations * ============ */requireget_template_directory().'/functions/_functions-plugins.php';/** * ============ * * Optimization and Cleaning * ============ */requireget_template_directory().'/functions/_functions-optimization.php';
All the function are placed in partials files in π /functions/ for better clarity and easier management.
π _functions-acf-fields.php
Sometimes you may want to create ACF fields automatically. WP Rig Toolkit also includes the creation of a custom_h1 field for use in core SEO optimizations. To learn more, read about the heading structure and custom headings.
Default content
<?php/** * Auto creation of ACF fields functions * * Sometimes you want to register ACF fields automatically so you can register them here * * @link https://docs.wprig.org/coming-soon * * @package wp_rig */// Check if ACF exists.if ( class_exists('ACF') ) {/** * Register field for custom H1. * * Adds a custom field for overriding the default H1 tag. */functionadd_custom_acf_post_and_page_options() {acf_add_local_field_group( array('key'=>'group_posts_and_pages_options','title'=>'Post & Pages Options','fields'=> array( array('key'=>'field_custom_h1','label'=>'Custom H1','name'=>'custom_h1','type'=>'text','instructions'=>'Fill this field if you want to override the default H1 tag (usually post or page name). Max length is 80 symbols but for SEO purposes it\'s better to use headers under 60-70.','maxlength'=>'80', ), ),'location'=> array( array( array('param'=>'post_type','operator'=>'==','value'=>'page', ), ), array( array('param'=>'post_type','operator'=>'==','value'=>'post', ), ), ), )); }add_action('acf/init','add_custom_acf_post_and_page_options');}
π _functions-custom-actions-filters.php
A place for custom actions and filters.
Default content
π _functions-custom-files.php
By default, it contains the enqueuing of global.min.js and elements-configuration.min.js. It can be useful when you want to enqueue or dequeue your own files using WP functions.
Default content
π _functions-custom-shortcodes.php
If you don't want to create a plugin for your shortcodes, you can place them here.
Default content
A place for registering custom thumbnail sizes in WordPress.
π _functions-menu.php
In this file, you can register new menus and customize them. By default, there are already some functions available
Default content
π _functions-optimization.php
Various optimizations aimed at providing cleaner code and faster loading speed.
Default content
π _functions-plugins.php
Various optional helper functions and extensions for popular plugins.
Default content
π _functions-rig-init.php
Initialization section of functions.php. You shouldn't touch it.
<?php
/**
* Custom actions and filters functions
*
* Here is the place for your custom actions and filters
*
* @link https://docs.wprig.org/coming-soon
*
* @package wp_rig
*/
<?php
/**
* Enqueue Custom Files
*
* Use this file to add functions for adding custom files in WordPress.
*
* For more information, visit: https://docs.wprig.org/coming-soon
*
* @package wp_rig
*/
/**
* Enqueue Global.js script
*
* Add the script file with global scripts.
* https://docs.wprig.org/coming-soon
*/
function enqueue_global_js() {
wp_enqueue_script(
'global-scripts',
get_theme_file_uri( '/assets/js/global.min.js' ),
array(),
wp_get_theme()->get( 'Version' ),
false
);
wp_script_add_data( 'global-scripts', 'defer', true );
wp_script_add_data( 'global-scripts', 'precache', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_global_js', 999 );
/**
* Enqueue Elements Configuration script
*
* Add the script file with all configurations for you .js plugins and other scripts.
* https://docs.wprig.org/coming-soon
*/
function enqueue_elements_configuration_js() {
wp_enqueue_script(
'elements-configuration',
get_theme_file_uri( '/assets/js/elements-configuration.min.js' ),
array(),
wp_get_theme()->get( 'Version' ),
true
);
wp_script_add_data( 'elements-configuration', 'defer', true );
wp_script_add_data( 'elements-configuration', 'precache', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_elements_configuration_js', 999 );
<?php
/**
* Custom shortcodes functions
*
* Here is the place for your custom shortcodes
*
* @link https://docs.wprig.org/coming-soon
*
* @package wp_rig
*/
<?php
/**
* Menu Customizations
*
* Place your functions here when you need to add new menus, add menu customizations and walkers
*
* @link https://docs.wprig.org/coming-soon
*
* @package wp_rig
*/
<?php
/**
* Optimization functions
*
* Here are placed different optimization functions which can be useful for production website.
*
* @link https://docs.wprig.org/coming-soon
*
* @package wp_rig
*/
/**
* Defer for all scripts.
*
* Add defer to all javascript files (exl. jquery and wp-admin scripts).
* https://kinsta.com/blog/defer-parsing-of-javascript/#4-defer-javascript-via-functionsphp-file
*/
if ( constant( 'DEFER_ALL_SCRIPTS' ) === true ) {
add_filter( 'script_loader_tag', 'defer_all_scripts', 10 );
/**
* Add defer attribute to script tags.
*
* @param string $url The URL of the enqueued script.
* @return string Modified script URL with defer attribute.
*/
function defer_all_scripts( $url ) {
if ( is_user_logged_in() ) {
return $url; // don't break WP Admin.
}
if ( false === strpos( $url, '.js' ) ) {
return $url;
}
if ( strpos( $url, 'jquery.min.js' ) ) {
return $url;
}
return str_replace( ' src', ' defer src', $url );
}
}
/**
* Remove version numbers from scripts and styles.
*
* When you update your site very rare it might be good to remove versions from html.
* https://docs.wprig.org/coming-soon
*/
if ( constant( 'DISABLE_SCRIPTS_STYLES_VERSIONS' ) === true ) {
add_filter( 'style_loader_src', 'switch_stylesheet_src', 10, 2 );
/**
* Remove version query string from stylesheets.
*
* @param string $src The source URL of the stylesheet.
* @param string $handle The handle of the stylesheet.
* @return string The modified source URL.
*/
function switch_stylesheet_src( $src, $handle ) {
$src = remove_query_arg( 'ver', $src );
return $src;
}
}
/**
* Remove type attribute.
*
* Usually not needed if your auditory use modern browsers.
* https://docs.wprig.org/coming-soon
*/
if ( constant( 'DISABLE_TYPE_ATTRIBUTES' ) === true ) {
add_action(
'after_setup_theme',
/**
* Enable HTML5 support for script and style tags.
*/
function() {
add_theme_support( 'html5', [ 'script', 'style' ] );
}
);
}
/**
* Remove default block styles.
*
* You can disable it if you don't use Gutenberg.
* https://docs.wprig.org/coming-soon
*/
if ( constant( 'DISABLE_DEFAULT_BLOCK_STYLES' ) === true ) {
add_action( 'wp_enqueue_scripts', 'remove_wp_block_library_css' );
/**
* Dequeue default block styles.
*/
function remove_wp_block_library_css() {
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme-css' );
}
}
/**
* Remove WP Polyfill.
*
* Usually not needed if your auditory use modern browsers.
* https://docs.wprig.org/coming-soon
*/
if ( constant( 'DISABLE_WP_POLYFILL' ) === true ) {
add_action( 'wp_enqueue_scripts', 'remove_wp_polyfill_js' );
/**
* Dequeue WP Polyfill scripts.
*/
function remove_wp_polyfill_js() {
wp_dequeue_script( 'wp-polyfill-js' );
wp_dequeue_script( 'wp-polyfill-js-after' );
}
}
<?php
/**
* Plugin functions
*
* Here you can find a lot of functions for the most popular plugins.
*
* @link https://docs.wprig.org/coming-soon
*
* @package wp_rig
*/
/**
* Contact Form 7: Disable automatic loading of CF7 .js and .css.
*
* Sometimes you want to postpone loading default CF7 styles and script
* and load them only where CF7 is persisted on the page.
*
* @link https://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/
*/
if ( constant( 'CF7_DISABLE_DEFAULT_LOADING_JS_CSS' ) === true ) {
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
}
/**
* Contact Form 7: Remove <p> and <br/>.
*
* When you want to have more control over CF7 form's code.
* It removes default <p> and <br/> tags from generated code.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'CF7_CLEAN_MARKUP' ) === true ) {
add_filter( 'wpcf7_autop_or_not', '__return_false' );
}
/**
* YoastSEO: Filter the output of Yoast breadcrumbs to remove <span> tags added by the plugin.
*
* Sometimes it can be really annoying when you are a fan of clean code :)
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'YOAST_BREADCRUMBS_CLEAN_SPAN' ) === true ) {
/**
* Filter the output of Yoast breadcrumbs to remove <span> tags added by the plugin.
*
* @param string $output The breadcrumb output.
* @return string Modified breadcrumb output.
*/
function doublee_filter_yoast_breadcrumb_output( $output ) {
$from = '<span>';
$to = '</span>';
$output = str_replace( $from, $to, $output );
return $output;
}
add_filter( 'wpseo_breadcrumb_output', 'doublee_filter_yoast_breadcrumb_output' );
}
/**
* YoastSEO: Remove SearchAction from yoast-schema-graph JSON structured data.
*
* Can be useful if you don't have a working search on your site (for example you have a generated static site).
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'YOAST_SEARCHACTION_JSON_DISABLE' ) === true ) {
add_filter( 'disable_wpseo_json_ld_search', '__return_true' );
}
/**
* Elementor: Remove loading standard Elementor fonts.
*
* You probably don't need Elementor if you use WP Rig.
* But if you use it, most likely you connect your fonts locally through Rig so no need to load default ones.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'ELEMENTOR_REMOVE_STANDART_FONTS' ) === true ) {
add_filter( 'elementor/frontend/print_google_fonts', '__return_false' );
}
<?php
/**
* WordPress customization functions
*
* Here are placed functions for WordPress adjustment.
*
* @link https://docs.wprig.org/coming-soon
*
* @package wp_rig
*/
/**
* Stop WordPress from adding <p> tags.
*
* By default, WordPress inserts paragraph tags for every new line.
* You may want to get rid of it for better code control.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'STOP_ADDING_P_TAGS' ) === true ) {
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
}
/**
* Disable Visual editor completely.
*
* Sometimes it is worth completely disabling the visual editor for better code control.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( ! constant( 'DISABLE_VISUAL_EDITOR' ) ) {
add_filter(
'user_can_richedit',
function() {
return false;
},
50
);
}
/**
* Disable Visual editor only for admins.
*
* Sometimes it is worth completely disabling the visual editor for administrators for better code control.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( ! constant( 'DISABLE_VISUAL_EDITOR_ADMINS_ONLY' ) ) {
if ( wp_get_current_user()->has_cap( 'administrator' ) ) {
add_filter(
'user_can_richedit',
function() {
return false;
},
50
);
}
}
/**
* Clean page headers from "Archive:" and "Category:".
*
* Removes default labels from archive and category headers.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'HEADERS_CLEAN_ARCHIVE_CATEGORY' ) === true ) {
add_filter(
'get_the_archive_title',
function ( $title ) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . esc_html( get_the_author() ) . '</span>';
} elseif ( is_tax() ) { // for custom post types.
$title = sprintf( __( '%1$s', 'wp-rig' ), single_term_title( '', false ) );
} elseif ( is_post_type_archive() ) {
$title = post_type_archive_title( '', false );
}
return $title;
}
);
}
/**
* Add Post Excerpts Support for Pages.
*
* By default, excerpts are enabled only for posts.
* This can be useful if you show excerpts in search results, for example.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'ADD_PAGE_EXCERPTS_SUPPORT' ) === true ) {
add_action( 'init', 'add_excerpt_support_for_pages' );
/**
* Add excerpt support for pages.
*/
function add_excerpt_support_for_pages() {
add_post_type_support( 'page', 'excerpt' );
}
}
/**
* Disable Comments.
*
* Disable comments if you don't need them on the website.
* Remember to remove existing comments if you have some.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'DISABLE_COMMENTS' ) === true ) {
add_action(
'admin_init',
function () {
// Redirect any user trying to access comments page.
global $pagenow;
if ( 'edit-comments.php' === $pagenow ) {
wp_redirect( admin_url() );
exit;
}
// Remove comments metabox from the dashboard.
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
// Disable support for comments and trackbacks in post types.
foreach ( get_post_types() as $post_type ) {
if ( post_type_supports( $post_type, 'comments' ) ) {
remove_post_type_support( $post_type, 'comments' );
remove_post_type_support( $post_type, 'trackbacks' );
}
}
}
);
// Close comments on the front-end.
add_filter( 'comments_open', '__return_false', 20, 2 );
add_filter( 'pings_open', '__return_false', 20, 2 );
// Hide existing comments.
add_filter( 'comments_array', '__return_empty_array', 10, 2 );
// Remove comments page in menu.
add_action(
'admin_menu',
function () {
remove_menu_page( 'edit-comments.php' );
}
);
// Remove comments links from admin bar.
add_action(
'init',
function () {
if ( is_admin_bar_showing() ) {
remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
}
}
);
}
/**
* Completely disable WordPress Image Resizing.
*
* In rare cases, you may want to disable image resizing.
* This is useful if you use only default image sizes or vector images.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'DISABLE_WP_ALL_THUMBNAILS_GENERATING' ) === true ) {
add_action( 'init', 'remove_all_image_sizes' );
/**
* Remove all intermediate image sizes.
*/
function remove_all_image_sizes() {
foreach ( get_intermediate_image_sizes() as $size ) {
remove_image_size( $size );
}
}
}
/**
* Disable default thumbnails sizes.
*
* If you have activated responsive images, you probably have custom thumbnail sizes.
* In this case, we recommend disabling original sizes as they are just a waste of your server space.
*
* @link https://docs.wprig.org/coming-soon
*/
if ( constant( 'DISABLE_WP_DEFAULT_THUMBNAILS_SIZES' ) === true ) {
add_filter( 'intermediate_image_sizes_advanced', 'add_image_insert_override' );
/**
* Remove default thumbnail sizes.
*
* @param array $sizes Array of default image sizes.
* @return array Modified array of image sizes.
*/
function add_image_insert_override( $sizes ) {
unset( $sizes['thumbnail'] );
unset( $sizes['medium'] );
unset( $sizes['medium-large'] );
unset( $sizes['large'] );
unset( $sizes['1536x1536'] );
unset( $sizes['2048x2048'] );
return $sizes;
}
}
/**
* Disable WordPress image scaling.
*
* If you want to serve original photos in high resolution or screenshots of web pages,
* you need to disable image scaling before uploading them.
*
* @link https://docs.wprig.org/coming-soon
* @link https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/
*/
if ( constant( 'DISABLE_WP_IMAGE_SCALING' ) === true ) {
add_filter( 'big_image_size_threshold', '__return_false' );
}