Blog / Tutorials / WordPress Shortcode – Compare Values

WordPress Shortcode – Compare Values

Cristian Antohe
Last Updated: 26/04/23

icon-256x256

With both Profile Builder and WordPress Creation Kit plugins using Mustache as their template system for things like User Listing and Swift Templates, we need at times the possibility to compares different values.

This however, is not possible in Mustache. So a shortcode to compare values comes to the rescue.

Compare Shortcode

Download Compare Shortcode

How to use

1
2
3
4
5
6
7
8
[compare val1="test" val2="test" operator="=="]
    List if the values is the same?
[/compare]
 
// Inside Profile Builder or WordPress Creation Kit you'll probably use it like so:
[compare val1="{{my-custom-field-2}}" val2="Yes" operator="=="]
    List if the values is the same?
[/compare]

Code wise, it’s fairly straight forward. You can also see what operators are supported:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * Compare shortcode. Tags: shortcode, compare,
 * Usage:
	[compare val1="test" val2="test" operator="=="]
		List if the values is the same?
	[/compare]
 */
add_shortcode( 'compare', 'wppbc_compare_shortcode' );
function wppbc_compare_shortcode( $atts, $content ){
	extract(
		$out = shortcode_atts(	array( 'val1' => '', 'val2' => '', 'operator' => ''), $atts )
	);
 
	// quick fix for WordPress bug: https://core.trac.wordpress.org/ticket/29658
	foreach($out as $key => $value){
		$out[$key] = str_replace('”', '', $value );
	}
 
	$l = $out['val1'];
	$r = $out['val2'];
 
	$operators = array(
		'=='    => create_function('$l, $r', 'return $l == $r;'),
		'==='   => create_function('$l, $r', 'return $l === $r;'),
		'!='    => create_function('$l, $r', 'return $l != $r;'),
		'<'     => create_function('$l, $r', 'return $l < $r;'),
		'>'     => create_function('$l, $r', 'return $l > $r;'),
		'<='    => create_function('$l, $r', 'return $l <= $r;'),
		'<='    => create_function('$l, $r', 'return $l <= $r;'),
		'>='    => create_function('$l, $r', 'return $l >= $r;'),
		''      => create_function('$l, $r', 'return $l == $r;'),
	);
	if ( !array_key_exists($out['operator'], $operators ) ){
		return '<p>The compare operator <strong style="padding:0 10px;">' . $out["operator"] . '</strong> is not recognized. Please try: == , ===, !=, <, >, <=, >=';
	}
 
	$bool = $operators[$out['operator']]($l, $r);
	if( $bool ) {
		return $content;
	}
}
From the blog

Related Articles

roundup wordpress ecosystem january2

Roundup of WordPress ecosystem #1 – January 2017

Author: Patricia Borlovan
Last Updated: February 9th, 2017

After writing the article "Overview of the WordPress Community in 2016" and getting feedback for the article on various platforms, I decided to continue writing them, but I changed its name into "Roundup of WordPress ecosystem". This is the first article from a monthly series that will showcase what happened around the whole ecosystem in […]

Continue Reading
feb_mar_roundup_wp_ecosystem

Roundup of WordPress ecosystem #2 – February & March 2017

Author: Patricia Borlovan
Last Updated: April 11th, 2017

There were two interesting months within the WordPress ecosystem with events that are here to impact the course and development of the platform. We saw WordPress getting important security updates and a brand new design for the plugin repository and also witnessed to a significant acquisition. With this being said, let's dive into the summary […]

Continue Reading

How to Password Protect Content, Posts, and Categories in WordPress

Author: Rishi Lodha
Last Updated: July 9th, 2024

There are many use cases for password-protected content in WordPress. For example, you might be a content creator who wants to monetize premium content in the form of subscriptions or memberships. As with everything related to WordPress, password-protecting posts doesn’t have to be difficult. If you’re wondering how to password-protect WordPress content, posts, and even […]

Continue Reading

3 thoughts on “WordPress Shortcode – Compare Values

    A great addition to WCK. I would love to see AND / OR added to the capability, so

    [compare val1=”{{abc}}” val2=”” operator=”!=” AND val1=”{{def}} val2=”” operator=”!=”]

    Also, is nesting supported? I can’t see any reason why not but in my (limited) testing it didn’t seem to.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.