This plugin hasn’t been tested with the latest three major releases of WordPress. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.

Conditional Custom Fields Shortcode

Description

A series of shortcodes for using custom field values in pages or posts (or in sidebar widgets – via Section Widget!), which would then allows you to take store the actual data in custom fields while storing the presentation in the page/post body. This transforms WordPress into an awesome CMS. Keep reading for more inspirations.

Basic Usage

</code></pre>

<p>Example:</p>

<pre><code>Picture of the day:
&lt;img src='[cf "picture-link"]' /&gt;
</code></pre>

<p>This would output something like <code>&lt;img src='http://thesite.com/something.jpg' /&gt;</code>, assume the post/page has the custom field "picture-link" and its value is "http://thesite.com/something.jpg". That way, you (or your clients!) would never accidentally take out a div when updating your page because you won't have to touch the page/post body anymore!</p>

<h4>Default Values</h4>

<pre><code>[cf "custom field name" default="display this if the CF is undefined"]
</code></pre>

<p>Example:</p>

<pre><code>Contact [cf "support-email" default="support@mycompany.com"] if you have questions.
</code></pre>

<p>support@mycompany.com will be displayed there if the custom field "support-email" is not present in that post/page.</p>

<h4>Simple Template</h4>

<pre><code>[cf "custom field name" default="default value"]...template...

Example:

Price: 
 USD$%value% 

If you cannot use %value% as your placeholder text for some reason, you can change it by specifying the placeholder attribute.

Example:

Price: 
 USD$__value__ 

Multiple Values

It’s smart enough to do the right thing. If your post has the following custom fields:

  • concert-date: Jan 14th
  • concert-date: Jan 28th
  • concert-date: Feb 2nd

Then

</code> will output <code>Jan 14th,Jan 28th,Feb2nd</code>, exactly what you would expect. You can also optionally provide a separator to replace the default (,).</p>

<p>Example:</p>

<pre><code>Come to one of our concerts on the following dates: [cf "concert-date" separator=" / "]
</code></pre>

<p>(=&gt; Jan 14th / Jan 28th / Feb 2nd)</p>

<h4>Sorting</h4>

<pre><code>[cf "custom field name" sort="nosort|asc|dsc|random" type="string|int|integer|float|bool|boolean|date|auto"]
</code></pre>

<p>You can control how it sort the items (when there's more than one) using the sort attribute (default is <code>nosort</code>). By default, it would compare them using PHP's comparison operator (i.e. try to cast them into numeric values, or compare them as strings if that failed). However, you can force a type cast by specifying the type attribute (default is <code>auto</code>).</p>

<h4>Display Single Value</h4>

<p>If display multiple values is not what you want it to do, you can use <code>[cf single="true"]</code> to force it to display only the first value. Combining what we have learned so far, you can display a single random value by <code>[cf single="true" sort="random"]</code>.</p>

<h4>Summary</h4>

<p>Syntax for <code>[cf]</code> shortcode: (default values in parenthesis)</p>

<pre><code>[cf "field-name" default="" placeholder="%value%" single="(false)|true" separator="," sort="(nosort)|asc|dsc|random" type="string|int|integer|float|bool|boolean|date|(auto)"](OPTIONAL) template

Conditionals

Conditionals tags is what makes this plugin unique. Here is a list of them:

[if-cf-def "custom field name"]
  Do this when "custom field name" is defined for this page/post
[/if-cf-def]

[if-cf-ndef "custom field name"]
  Do this when "custom field name" is NOT defined for this page/post
[/if-cf-ndef]

[if-cf-eq "custom field name" "value"]
  Do this when "custom field name" == "value"
[/if-cf-eq]

[if-cf-neq "custom field name" "value"]
  Do this when "custom field name" != "value"
[/if-cf-neq]

[if-cf-lt "custom field name" "value"]
  Do this when "custom field name" < "value"
[/if-cf-lt]

[if-cf-gt "custom field name" "value"]
  Do this when "custom field name" > "value"
[/if-cf-gt]

[if-cf-let "custom field name" "value"]
  Do this when "custom field name" <= "value"
[/if-cf-let]

[if-cf-get "custom field name" "value"]
  Do this when "custom field name" >= "value"
[/if-cf-get]

More detailed documentations will be published soon. In the mean time, check out the following examples and the comments in the PHP code to get a rough idea.

Examples

[if-cf-get "deadline" "today" type="date"]
    Submit your application <a href='
'&gt;here&lt;/a&gt;.
[/if-cf-get]

[if-cf-lt "deadline" "today" type="date"]
    Sorry we regret that we cannot take anymore applications.
[/if-cf-lt]

[if-cf-def "discounted-price" "discount-expires" logic="and"]
    This product is on discount, get it now for [cf "discounted-price" /] [cf "discount-expires" default="for a limited time"]before %value%

!
[/if-cf-def]

Please note: It seems that there is a serious bug in WordPress’ shortcode parser that prevents shortcodes from functioning correctly in certain occasions. The developers are hard at work to get this fixed before the 2.8.3 release. (See #10082) Therefore, please do not report any parser related bugs for the moment. (e.g. the shortcode is displayed on the actual page)

Installation

  1. Extract the zip file and drop the contents in the wp-content/plugins/ directory of your WordPress installation
  2. Activate the Plugin from Plugins page

Reviews

There are no reviews for this plugin.

Contributors and Developers

“Conditional Custom Fields Shortcode” is open source software. The following people have contributed to this plugin.

Contributors