This tutorial is still in progress…
This is an article in the reference of the tutorial conditional tags in WordPress and Genesis.
Make sure to give a read to Conditional Tags in WordPress and Genesis to have a better understanding of return early Code tip.
While that article is a good fit for newbies, it is recommended to follow this article while writing any code. This ensures that we have a clean and well-structured code that is easy to understand and works faster.
What are Conditional Tags?
Conditional tags are the rule that provides the facility to control ‘Doing the stuff for different contents’.
Here, Doing the STUFF refers to…
1. Adding or removing a filter
2. Adding or removing an action
more…
and not limited to.
Different Contents could be
1. Posts or Pages includes Frontpage, Homepage aka post page and Custom Post Types
2. Archive, Categories includes Author archive and Custom taxonomy.
3. By user role i.e Logged, Unregistered, Admin
and more
and not limited to.
Not limited to means there are more STUFF and CONTENTS present in WordPress and Genesis that can be done using conditional tags.
You can read more about Conditional Tags here.
Return Early
Return early simplify the work for the processor for doing the work which helps to quickly act for the specific command.
REMEMBER => Return early works opposite of what we do in the old way. While we are restricting the condition using ! in old way the new way use it to show on the condition which it is attached to.
OLD WAY
is_front_page() => The STUFF will be done on front page only.
!is_front_page() => The STUFF will not done on Front page but will on OTHER pages.
! is_front_page() => The STUFF will done for Front page only.
1st and 3rd work similarly.
RECOMMENDED WAY
when we use Return early.
is_front_page() => The STUFF will be NOT done on front page.
!is_front_page() => The STUFF will ONLY done on Front page and NOT on OTHER pages.
! is_front_page() => The STUFF will ONLY done on Front page and NOT on OTHER pages.
2nd and 3rd work similarly.
Sample:-
Let’s what we do in the OLD WAY
1.
if ( is_front_page() ) { echo 'You are viewing Front Page'; }
2.
if ( !is_front_page() ) { echo 'You are NOT on Front Page'; }
3.
if ( ! is_front_page() ) { echo 'You are viewing Front Page'; }
RECOMMENDED WAY
1.
if ( is_front_page() ) { return; } echo 'You are NOT on Front Page';
2.
if ( !is_front_page() ) { return; } echo 'You are viewing Front Page';
3.
if ( ! is_front_page() ) { return; } echo 'You are viewing Front Page';
Example:-
Let’s how we can use it in Genesis site using WordPress hooks and Genesis hooks location with a custom function.
OLD WAY
Add only one of the example from the following in child-theme/functions.php
//* Example 1. add_action( 'genesis_before_content', 'old_conditional_snippet' ); function old_conditional_snippet() { if ( is_front_page() ) { //Hey I am here echo 'Some STUFF to do for front page using OLD WAY'; } } //* Example 2. add_action( 'genesis_before_content', 'old_conditional_snippet' ); function old_conditional_snippet() { if ( !is_front_page() ) { //Hey I am here echo 'Some STUFF to do NOT for front page using OLD WAY'; } } //* Example 3. add_action( 'genesis_before_content', 'old_conditional_snippet' ); function old_conditional_snippet() { if ( ! is_front_page() ) { //Hey I am here echo 'Some STUFF for front page using OLD WAY'; } }
RECOMMENDED WAY
Add only one of the example from the following in child-theme/functions.php
//* Example 1 add_action( 'genesis_before_content', 'new_conditional_snippet' ); function new_conditional_snippet() { if ( is_front_page() ) { //Hey I am here return; } echo 'Some STUFF to do NOT for Front Page using RECOMMENDED WAY'; } //* Example 2 add_action( 'genesis_before_content', 'new_conditional_snippet' ); function new_conditional_snippet() { if ( ! is_front_page() ) { //Hey I am here return; } echo 'Some STUFF to do for Front Page using RECOMMENDED WAY'; } //* Example 3 add_action( 'genesis_before_content', 'new_conditional_snippet' ); function new_conditional_snippet() { if ( ! is_front_page() ) { //Hey I am here return; } echo 'Some STUFF to do for Front Page using RECOMMENDED WAY'; }
Multi Conditions and return early works same opposite of the old way.
Generally what we have used in old way.
is_home() || is_paged() => work on example.com and example.com/page/2*
is_home() && is_paged() => work on example.com/page/2*
is_home() || !is_paged() => work on example.com only.
Here, && = Additional condition to be met.
|| = work on other condition too.
The new way.
!is_home() || !is_paged() => work on example.com and example.com/page/2*
!is_home() && is_paged() => work on example.com.
is_home() || !is_paged() => work on example.com/page/2*
Here, && = work on other condition too.
|| = Additional condition to be met.
Remember:- However, this will Totally depend on using ! with the conditions.
Real-life Examples:
You can found one here in Show WP-PostRatings Plugin Ratings in Genesis
Example 1. When you not want to to the stuff on front page and post page using return early function.
if ( is_front_page() || is_home() ) { //here || = && in old way with ! required. return; }
Example 2. When I don’t want to show facetwp dropdown on paging what i have used.
if ( ! is_home() || is_paged() ) {//* Don't show on /page/2* return; }
find here. Facetwp Dropdown in Genesis.
Example 3. When I have to show the widgets only if the sidebar is active or not.
if (! ( is_active_sidebar('utility-bar-left') || is_active_sidebar('utility-bar-right') ) ) { return; }
Find here. Utility Bar in Genesis and Utility bar in Genesis Sample.
Widget Example.
Show widget on the front page only and check Whether a sidebar is in use. If not, return early.
if (! is_front_page() || ! is_active_sidebar('hero-section') ) { return; }
or can be also done using this.
if ( ! is_front_page() && is_active_sidebar('hero-section') ) { return; }
This will work too.
if ( ! ( is_front_page() && is_active_sidebar('hero-section') ) ) { return; }
Show Globally ( to do stuff for other pages too.)
if ( ! ( is_front_page() || is_active_sidebar('hero-section') ) ) { return; }
What have you learned?
When done, return. Otherwise, continue processing.
Your turn.
Go, launch a test site, Grab the old codes from old conditional snippets and make it work using the return early function and share your result with me.
Sometimes, it seems to be confusing but keeps practicing it. This post will update soon with more examples and explanations.