Title: Help creating functions &amp; shortcodes
Last modified: August 20, 2016

---

# Help creating functions & shortcodes

 *  [mayb101](https://wordpress.org/support/users/mayb101/)
 * (@mayb101)
 * [15 years ago](https://wordpress.org/support/topic/help-creating-functions-shortcodes/)
 * Hi!
 * Thank you in advance for checking out my post.
 * I am wondering if anyone can help me with creating a function and shortcode.
 * I set up a custom field in my posts called **smurl**.
 * This is what I would like to display in my loop.
 * `<div class="smurl_div"><input type="text" class="smurl_box" value="<?php $key
   ="smurl"; echo get_post_meta($post->ID, $key, true); ?>"></div>`
 * However, I would like it to only display if the field has a value.
    If it is 
   null, I would not like it to display. At present, if it is null, the div and 
   input still displays (obviously).
 * I would also like to be able to have code for the functions.php of my theme so
   that I can place the value of smurl in the loop using:
 * <?php get_smurl();?>
    and a shortcode[smurl] for within posts
 * And so I can place the code
 * `<div class="smurl_div"><input type="text" class="smurl_box" value="<?php $key
   ="smurl"; echo get_post_meta($post->ID, $key, true); ?>"></div>`
 * in the loop using:
 * <?php get_smurl_div();?>
    and a shortcode[smurl_box] for within posts
 * Is this possible? I am still learning so any help is greatly appreciated.
 * Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [superyetkin](https://wordpress.org/support/users/superyetkin/)
 * (@superyetkin)
 * [15 years ago](https://wordpress.org/support/topic/help-creating-functions-shortcodes/#post-2077344)
 * Use this:
 * `<div class="smurl_div"><input type="text" class="smurl_box" value="<?php $key
   ="smurl"; $res = get_post_meta($post->ID, $key, true); if ($res != "") echo $
   res; ?>"></div>`
 * [Donate](http://dev.univerpedi.com/donate/) if this helps you.
 *  Thread Starter [mayb101](https://wordpress.org/support/users/mayb101/)
 * (@mayb101)
 * [15 years ago](https://wordpress.org/support/topic/help-creating-functions-shortcodes/#post-2077407)
 * Thanks for replying. That may help. I’ll see what I can do with it.
 * But for the second instance, it’s the whole DIV itself that needs to be wrapped
   in an IF statement of some kind e.g.
 * if smurl is not null
    display <div>…</div> else don’t display anything
 * No problem donating something if I can get this to work.
 *  [thefatguy](https://wordpress.org/support/users/thefatguy/)
 * (@thefatguy)
 * [15 years ago](https://wordpress.org/support/topic/help-creating-functions-shortcodes/#post-2077434)
 * This is what I do to test for null values:
 *     ```
       <?php if ( !empty( $smurl ) ) : ?>
       ...do something, like <div> etc...
       <?php endif; ?>
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Help creating functions & shortcodes’ is closed to new replies.

## Tags

 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 3 participants
 * Last reply from: [thefatguy](https://wordpress.org/support/users/thefatguy/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/help-creating-functions-shortcodes/#post-2077434)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
