Awesome WordPress Shortcode Tips

Wordpress shortcode

Have you ever now, that WordPress support shortcode? With shortcode we can easy insert some code or function on the post. Example we can put some Advertisement on the post without write a lot of bloated code. We can save a lot of time with shortcode.

The shortcode syntax varies, here are some examples:

  • [shortcode]
  • [shortcode atribute=”value”]
  • [shortcode]Text[/shortcode]

Shortcodes can be created to perform a handful of useful functions. To use any of the shortcodes in this post I recommend adding the functions to your functions.php theme file (create it if it does not exist!).

If the shortcode has been declared on the plugins or themes. When  the content displayed on the post. The [shortcode] will be change depend on the declaration on the shortcode.

Blue Anvil has been write some tips and trick useful shortcode for WordPress fans. And here some of my favorite.

Integrate some Adsense Ads

If you want to output some adsense ads in posts.

function adsense_shortcode( $atts ) {
'format' => '1',
), $atts));

switch ($format) {
case 1 :
$ad = '<script type="text/javascript"><!--
google_ad_client = "pub-1151940779041240";
/* 234x60, created 16/09/08 */
google_ad_slot = "0834408702";
google_ad_width = 234;
google_ad_height = 60;
<script type="text/javascript"
return $ad;
add_shortcode('adsense', 'adsense_shortcode');

How to use the code, just use like this sample [adsense] like this example


Add additional ad formats to the switch statement and you can output other ad formats using, for example, [adsense format="2"].

Show related posts

How about showing some related posts in your current post based on it’s tags? Lets do it!

function related_posts_shortcode( $atts ) {

'limit' => '5',
), $atts));

global $wpdb, $post, $table_prefix;

if ($post->ID) {

$retval = '

// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
$tagslist = implode(',', $tagsarray);

// Do the query
$q = "
SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p
WHERE tt.taxonomy ='post_tag'
AND tt.term_taxonomy_id = tr.term_taxonomy_id
AND tr.object_id  = p.ID
AND tt.term_id IN ($tagslist)
AND p.ID != $post->ID
AND p.post_status = 'publish'
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";

$related = $wpdb->get_results($q);

if ( $related ) {
foreach($related as $r) {
$retval .= '
<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>
} else {
$retval .= '
<li>No related posts found</li>
$retval .= '</ul>
return $retval;
add_shortcode('related_posts', 'related_posts_shortcode');

To display related post, just use this code [related_posts], like this example below


Have a nice day and Take your WordPress to the next level ;)

7 thoughts on “Awesome WordPress Shortcode Tips

  1. Permalink  ⋅ Balas

    web design bangladesh

    April 13, 2011 at 12:01am

    This is too complex for beginners as I am. But I am reading other posts of you and learning a lot.


  2. Permalink  ⋅ Balas


    Februari 1, 2011 at 5:49pm

    Can you please offer an example of how i might turn a simple php variable into shortcode?

    I have included a list of variables in /includes/ – If for instrance I want to post a variable such as $variable1 into a post , I would like to be able to do this with a shorcode like [variable1]. It seems like this will work , however I do not get the syntax!

  3. Permalink  ⋅ Balas


    September 13, 2010 at 4:01pm

    Can you please help me with making a shortcode from this script.
    And where should I put it? In the existing custom.function.php in my template folder? Or elsewhere?

    function related_mess() {
    global $wp_query;
    $exclude = $wp_query->post->ID;
    $relation = get_the_term_list( $post->ID, 'relations',' ', ', ', '' );

    if(!empty($relation)) { ?>

    $relation, 'showposts' =>10, 'post__not_in' => array($exclude)));
    while ( $rmquery->have_posts()) : $rmquery->the_post(); ?>

    <a href="">

    <?php }

    Thank you!

  4. Permalink  ⋅ Balas


    Mei 17, 2009 at 5:28am

    Thanks for the shortcodes, I somehow thought it would be more technical but you explain it so easy. ;)

  5. Permalink  ⋅ Balas


    April 10, 2009 at 9:55pm

    This is such complicated stuff!

  6. Permalink  ⋅ Balas


    April 10, 2009 at 6:13pm

    mas salam kenal yah, wah jarang-2 nich ada master WordPress :-\” .

Tinggalkan Balasan

Your email will not be published. Name and Email fields are required.

Anda dapat menggunakan tag dan atribut HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>