Common changes with a few lines of code (Child theme recipes)
Short, tested snippets for the changes site owners ask for most - a banner above the header, the page title area, excerpts, the member menu, your own CSS, one template part, turning a feature off and adding to the footer.
Also called: code snippets, functions.php recipes, child theme examples, customise with code, Reign hooks examples
Each recipe below makes one common change. Copy the snippet, paste it into your child theme, and save. Every snippet was tested on Reign 8.1.0 with the official child theme.
Before you start
- Install and activate the official child theme. See Build on Reign with a child theme.
- Open the child theme’s
functions.php. Paste each snippet at the very end, below the line that says “Your custom code goes below this line”. - Edit files through your host’s file manager or SFTP. If a snippet has a typo, the site can show an error. You can then fix the file the same way.
Try each change on a staging copy first if your site is busy.
Add a banner above the header
This puts a thin strip of text above your logo and menu, on every page. Use it for a sale, a notice or a phone number.
add_action( 'reign_before_masthead', function () {
echo '<div class="my-top-banner">Free delivery on every order this week.</div>';
} );
Give it a colour in the child theme’s style.css.
.my-top-banner {
padding: 6px 16px;
text-align: center;
background: var(--reign-site-button-bg-color);
color: var(--reign-site-button-text-color);
}
The two var(--reign-site-button-...) colours are your button colours, so the banner follows your palette and dark mode.
You’ll know it worked when the strip shows above the header on every page.
Hide or change the page title area
The page title area is the part under the header that shows the page name. It can sit inside the content or in a banner (the sub header).
Hide it on every normal page. The title stays in the page for screen readers and search engines.
add_filter( 'reign_title_context', function ( $context ) {
if ( is_page() ) {
$context['style'] = 'hidden';
}
return $context;
} );
The style value can be content, banner or hidden. Check other screens with WordPress conditionals such as is_singular( 'post' ) or is_archive().
Change the words in the banner instead. This one renames category pages.
add_filter( 'reign_page_header_section_title', function ( $title ) {
if ( is_category() ) {
return 'Articles about ' . single_cat_title( '', false );
}
return $title;
} );
You’ll know it worked when your pages show no visible title, or a category page reads “Articles about News”. The second snippet only shows when that screen uses the banner title style.
Change the excerpt length and the “read more” text
On blog lists, Reign cuts each post to a short summary (an excerpt). This sets it to 12 words and changes the text at the end.
add_filter( 'reign_excerpt_length', function () {
return 12;
} );
add_filter( 'reign_excerpt_more', function () {
return ' ... keep reading';
} );
These filters work while Customize > Blog > Advanced Excerpt is on, which is the default. The number in the snippet wins over the Excerpt Length setting.
You’ll know it worked when every post on the blog list stops after 12 words and ends with ”… keep reading”.
Add a link to the member menu
The member menu opens when a logged-in member clicks their photo in the header. This adds a “Help centre” link at the top of it. It needs BuddyPress or BuddyBoss.
add_action( 'reign_before_user_menu_item', function () {
printf(
'<li class="menu-item"><a href="%s"><span class="menu-title">%s</span></a></li>',
esc_url( home_url( '/help/' ) ),
esc_html__( 'Help centre', 'reign-child' )
);
} );
Use reign_after_user_menu_item to put the link at the bottom, after Log Out.
You’ll know it worked when “Help centre” is the first item in the member menu.
Load your own CSS file after Reign’s
The child theme’s style.css already loads after Reign. Use this when you want a separate file, for example css/my-site.css inside the child theme.
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_style(
'my-site-extra',
get_stylesheet_directory_uri() . '/css/my-site.css',
array( 'reign_main_style' ),
'1.0.0'
);
}, 6000 );
The 6000 and the reign_main_style dependency make your file print after Reign’s main stylesheet. Your rules then win when they are equally specific. Raise the 1.0.0 version when you change the file, so browsers fetch the new copy.
You’ll know it worked when your file appears after reign_main_style-css in the page source.
Change one template part
A template part is a small file that builds one piece of a page. This example changes the message on a search with no results.
-
Copy
reign-theme/template-parts/content-none.php. -
Paste it into your child theme at
reign-child-theme/template-parts/content-none.php. Create thetemplate-partsfolder if it is missing. -
In your copy, find this line.
esc_html__( 'No results for “%s”. Try a different keyword or check your spelling.', 'reign' ), -
Change it to your own words, with your child theme’s text domain.
esc_html__( 'We could not find “%s”. Try a shorter word, or browse our latest posts.', 'reign-child' ), -
Save the file.
You’ll know it worked when a search for a word that is not on the site shows your new message.
Keep the do_action() lines when you copy a template, so Reign and other plugins can still add to it. After a Reign update, compare your copy with the new original. See Override Reign templates for every folder.
Turn a Reign feature off
Many Reign features have an on/off filter. This one stops breadcrumbs (the “Home > Blog > Post” trail) on every screen, whatever the Customizer says.
add_filter( 'reign_show_breadcrumbs', '__return_false' );
Other switches that work the same way:
| Filter | Return false to |
|---|---|
reign_search_federation | Remove the plugin result sections from the search page |
reign_customize_classic_widgets | Use block widgets in the Customizer again |
reign_alter_display_right_sidebar | Leave the right sidebar out |
You’ll know it worked when no breadcrumb trail shows on any page.
Print Reign member or group cards in your own widget
reign_bp_card_grid() (added in 8.1.1) prints the same cards the Members and Groups directories show, for a widget, shortcode or block that lists members or groups.
reign_bp_card_grid( 'members', array(
'query' => array( 'type' => 'newest', 'per_page' => 6 ),
'classes' => 'three',
) );
The first argument is members or groups. In $args, layout is one of the four directory layouts (for example wbtm-member-directory-type-2; the default is the layout set for your site), query is passed to bp_has_members() or bp_has_groups(), and classes adds classes to the list, such as a column count. It returns false and prints nothing on BuddyBoss, with a BuddyPress template pack other than Nouveau, or when the component is off. Check the result if you need a fallback.
To change the avatar size in the BuddyPress Members, Groups and Friends widgets in a widget area, set --reign-widget-avatar-size in your child theme’s CSS (default 44px).
:root { --reign-widget-avatar-size: 56px; }
You’ll know it worked when the cards show where you called the function.
Give your own widget the Classic and Refreshed switch
Every Reign community widget has a Style setting: Classic keeps the look from before 8.1, Refreshed uses the one card design built from your theme colours (added in 8.1.2). A widget from your own plugin or child theme can offer the same switch with four helpers from widgets/reign-widget-style.php. Reign loads the file on every request, so you can call them from any widget class.
class My_Members_Widget extends WP_Widget {
public function form( $instance ) {
reign_widget_style_field( $this, $instance ); // The Style dropdown.
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['reign_style'] = reign_widget_style_update( $new_instance, $old_instance );
return $instance;
}
public function widget( $args, $instance ) {
echo reign_widget_before( $args, $instance ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- before_widget from the theme's own sidebar.
if ( 'refreshed' === reign_widget_style( $instance ) ) {
// Print the card markup.
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
reign_widget_style( $instance ) returns classic or refreshed; an instance with no saved value is classic, so widgets already on a site keep their look. reign_widget_before( $args, $instance ) adds the reign-widget--refreshed class to the widget wrapper. reign_widget_style_field( $widget, $instance ) prints the Style dropdown, set to Refreshed when the widget is being added. reign_widget_style_update( $new_instance, $old_instance ) returns the value to save.
You’ll know it worked when the widget form shows a Style dropdown and the wrapper carries reign-widget--refreshed after you pick Refreshed.
Show a date in the site’s timezone
Reign shows dates in one timezone everywhere: the footer year, Joined and Member since on profiles, the note on a job application and the licence date. By default that is the timezone set in Settings > General. Two functions in inc/time.php do the work, and your code can use them to match.
// A stored UTC time, shown the way Reign shows it.
echo esc_html( reign_date_label( $row->created_gmt, __( 'Never', 'my-plugin' ), 'F j, Y' ) );
// Show each logged-in member's own timezone.
add_filter( 'reign_display_timezone', function ( $timezone, $user_id ) {
$name = get_user_meta( $user_id, 'my_timezone', true );
return $name ? new DateTimeZone( $name ) : $timezone;
}, 10, 2 );
reign_date_label( $utc, $fallback = '', $format = null, $user_id = 0 ) takes a UTC Y-m-d H:i:s string (a string with no zone is read as UTC) or a timestamp. It returns $fallback when the value is empty or not a date, uses the site’s date format when $format is null, and formats the time in the display timezone. reign_display_timezone( $user_id = 0 ) returns that timezone as a DateTimeZone; 0 means the current user. The reign_display_timezone filter receives the site’s timezone from wp_timezone() and the user ID, and must return a DateTimeZone; anything else falls back to the site’s timezone.
You’ll know it worked when a date shows the hour of the timezone you chose, not the server’s.
Print your own inline CSS or JavaScript the way Reign does
Reign prints its inline CSS and JavaScript through WordPress’s own functions, never as a hand-written <style> or <script> tag. That lets a strict Content Security Policy allow them, because core adds the nonce or attributes your security plugin expects. Reign’s helpers are in inc/inline-assets.php.
reign_print_inline_style( 'my-banner', '.my-banner{background:var(--reign-colors-theme);}' );
reign_print_inline_script( 'my-banner-js', 'document.body.classList.add("has-banner");', array() );
reign_template_open( 'tmpl-my-card' );
?>
<div class="my-card">{{ data.title }}</div>
<?php
reign_template_close();
reign_print_inline_style( $handle, $css ) prints the CSS now, as the element {handle}-inline-css. Pass CSS without the style tag. reign_print_inline_script( $handle, $js, $deps = array( 'jquery' ) ) prints the JavaScript now, as {handle}-js-after; pass an empty array when it does not need jQuery. Printing the same handle twice gives the second block its own suffix instead of overwriting the first. reign_template_open( $id ) and reign_template_close() wrap an Underscore or Backbone template: everything printed between them becomes one <script type="text/html"> template with that ID.
You’ll know it worked when the block shows in the page source with a nonce, if your site sends one, and your style or script takes effect.
Remove the sidebar from blog posts
This makes every single blog post full width, with no sidebar. Pages and the blog list keep their layout.
add_filter( 'reign_sidebar_layout', function ( $layout ) {
if ( is_singular( 'post' ) ) {
return 'full_width';
}
return $layout;
} );
Other values are right_sidebar, left_sidebar, both_sidebar and stretched_view.
You’ll know it worked when a blog post fills the content width and shows no widgets beside it.
Add something before or after the footer
These print a block just above the footer, or right below it, on every page.
add_action( 'reign_before_footer', function () {
echo '<div class="my-before-footer">Join our newsletter.</div>';
} );
add_action( 'reign_after_footer', function () {
echo '<div class="my-after-footer">Site built by Example Studio.</div>';
} );
To change the copyright line itself, use the reign_footer_copyright_text filter. See Hooks reference.
You’ll know it worked when your two lines show above and below the footer.
Good to know
My snippet broke the site. What do I do?
Open functions.php again through your host’s file manager or SFTP. Remove the snippet you just added and save. The site comes back straight away.
Can I use a code snippets plugin instead?
Yes. The PHP snippets work the same in a snippets plugin. The template part recipe needs the child theme, because it is a file.
Where do I find more hooks?
Every hook Reign offers is in the Hooks reference.