snipt

Ctrl+h for KB shortcuts

PHP

if custom field (WPAlchemy in this example) has value...

<?php if($new_product->get_the_value('banner_text')) {do something} ?>
https://snipt.net/embed/0d94d289e9224c2cec2301469061cfd0/
https://snipt.net/raw/0d94d289e9224c2cec2301469061cfd0/
0d94d289e9224c2cec2301469061cfd0
php
PHP
1
2016-09-22T15:17:46
True
False
False
/api/public/snipt/41985/
if-custom-field-wpalchemy-in-this-example-has-value
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="k">if</span><span class="p">(</span><span class="nv">$new_product</span><span class="o">-&gt;</span><span class="na">get_the_value</span><span class="p">(</span><span class="s1">&#39;banner_text&#39;</span><span class="p">))</span> <span class="p">{</span><span class="k">do</span> <span class="nx">something</span><span class="p">}</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"custom field", wordpress, wpalchemy

PHP

Allow shortcode in custom field

1
2
3
<?php echo apply_filters('the_content', get_post_meta($post->ID, 'Slide 1', true)); ?>

ref: http://wpsnipp.com/index.php/custom-field/enable-shortcode-in-custom-field/?u=rss
https://snipt.net/embed/f77c3b0273726061604bec2ea0b18669/
https://snipt.net/raw/f77c3b0273726061604bec2ea0b18669/
f77c3b0273726061604bec2ea0b18669
php
PHP
4
2016-09-22T15:17:54
True
False
False
/api/public/snipt/41949/
allow-shortcode-in-custom-field
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nx">apply_filters</span><span class="p">(</span><span class="s1">&#39;the_content&#39;</span><span class="p">,</span> <span class="nx">get_post_meta</span><span class="p">(</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">,</span> <span class="s1">&#39;Slide 1&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">));</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="x">ref: http://wpsnipp.com/index.php/custom-field/enable-shortcode-in-custom-field/?u=rss</span> </span></pre></div> </td></tr></table>
"custom field", wordpress

PHP

If custom field exist (has value) do something cool

1
2
3
4
5
6
7
<?php if ( get_post_meta($post->ID, 'Callout Box', true) ) { ?>
    <aside class="callout clearfix">
		<?php echo get_post_meta($post->ID, 'Callout Box', true); ?>
	</aside>
<?php } ?>

Ref: http://perishablepress.com/wordpress-custom-fields-tips-tricks/
https://snipt.net/embed/ee918b67a8d811804c63d75a5bad4e71/
https://snipt.net/raw/ee918b67a8d811804c63d75a5bad4e71/
ee918b67a8d811804c63d75a5bad4e71
php
PHP
7
2016-09-22T15:18:00
True
False
False
/api/public/snipt/41947/
if-custom-field-exist-has-value-do-something-cool
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="k">if</span> <span class="p">(</span> <span class="nx">get_post_meta</span><span class="p">(</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">,</span> <span class="s1">&#39;Callout Box&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">)</span> <span class="p">)</span> <span class="p">{</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-2"><a name="L-2"></a><span class="x"> &lt;aside class=&quot;callout clearfix&quot;&gt;</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nx">get_post_meta</span><span class="p">(</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">,</span> <span class="s1">&#39;Callout Box&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-4"><a name="L-4"></a><span class="x"> &lt;/aside&gt;</span> </span><span id="L-5"><a name="L-5"></a><span class="cp">&lt;?php</span> <span class="p">}</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="x">Ref: http://perishablepress.com/wordpress-custom-fields-tips-tricks/</span> </span></pre></div> </td></tr></table>
"custom field", wordpress

PHP

Run shortcode outside of a loop from a custom field

1
2
3
4
5
6
< ?php
    if (get_post_meta($post->ID, 'gallery', true)) {
    $shortcode = get_post_meta($post->ID, 'gallery', true);
    echo do_shortcode("$shortcode");
    } else { echo ''; }
?>
https://snipt.net/embed/e6e6073941b6d1e91bb89f00b0079296/
https://snipt.net/raw/e6e6073941b6d1e91bb89f00b0079296/
e6e6073941b6d1e91bb89f00b0079296
php
PHP
6
2016-09-29T16:01:52
True
False
False
/api/public/snipt/18491/
run-shortcode-outside-of-a-loop-from-a-custom-field
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="x">&lt; ?php</span> </span><span id="L-2"><a name="L-2"></a><span class="x"> if (get_post_meta($post-&gt;ID, &#39;gallery&#39;, true)) {</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> $shortcode = get_post_meta($post-&gt;ID, &#39;gallery&#39;, true);</span> </span><span id="L-4"><a name="L-4"></a><span class="x"> echo do_shortcode(&quot;$shortcode&quot;);</span> </span><span id="L-5"><a name="L-5"></a><span class="x"> } else { echo &#39;&#39;; }</span> </span><span id="L-6"><a name="L-6"></a><span class="x">?&gt;</span> </span></pre></div> </td></tr></table>
"custom field", shortcode, wordpress

PHP

Get Custom Field value

<?php $custom_field = get_post_meta($post->ID, 'meta_key_name', true); ?>
https://snipt.net/embed/2770ca5fb7daf7a103c80ad31fb10b08/
https://snipt.net/raw/2770ca5fb7daf7a103c80ad31fb10b08/
2770ca5fb7daf7a103c80ad31fb10b08
php
PHP
1
2016-09-25T15:50:55
True
False
False
/api/public/snipt/14501/
get-custom-field-value
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="nv">$custom_field</span> <span class="o">=</span> <span class="nx">get_post_meta</span><span class="p">(</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">,</span> <span class="s1">&#39;meta_key_name&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"custom field", meta, wordpress
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.