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

what-is-wordpress-ver-2

Beginner’s Guide to: What Is WordPress?

Author: Cristian Antohe
Last Updated: February 15th, 2017

Ever now and again the question arises with new clients that aren't really tech savvy: "What Is WordPress?" What I'm hoping to achieve with this post is to drop the technical jargon for a minute and explain in down to earth words what is WordPress, how can it help you, what is WordPress.com, what's a […]

Continue Reading

How to Eliminate WordPress Spam Registrations (Step-by-Step Guide)

Author: Adrian Spiac
Last Updated: December 11th, 2019

Looking for a way to cut down on WordPress spam registrations? If your WordPress site is set to allow user registration (like a membership site or WooCommerce store), then it's probably vulnerable to user registration spam from spam-bots. Finding a way to eliminate, or at least reduce, WordPress spam registrations is important so that you can focus your efforts and resources on your real users. No matter what type of site you're running, there are some tried-and-true tactics that you can apply to stop spam registrations in their tracks, and they're all available in one single plugin.

Continue Reading

WordPress User Registration

Author: Cristian Antohe
Last Updated: February 24th, 2020

Have you hit a road block when it comes to WordPress user registration? You probably would like to have new users register before being able to take certain actions (for example, posting reviews or commenting) but do not want them to have access to the WordPress Dashboard? Truth is, WordPress User Registration doesn't have to […]

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.