Sunday, 1 March 2015

How To create shortcode in Wordpress

How To create shortcode in Wordpress

The shortcode API in wordpress allows you to create your own shortcodes by adding functions to your theme functions.php template (this is located at

You can also create shortcode for plugin. it can be use in your wordpress page.

Here simple shortcode Example for Wordpress

Basic Example Of Shortcode

function example_shortcode() {{

return ‘Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.’;


add_shortcode('my_example', 'example_shortcode');

you can use “[my_example]” shortcode in your wordpress page ot post.

write [my_example] check it your wordpress.

Creating a Shortcode With Attributes in wordpress

function latest_news( $atts ){   

<?php $args = array( 'post_type' => post, 'posts_per_page' => 10 );
   $loop = new WP_Query( $args );    
   global $post;    
   $short_data = shortcode_atts(array(
      'show_post_title'  => 'true',
       'show_post_image'  => 'true'          
   ), $atts);    
   ob_start(); ?>
          while ( $loop->have_posts() ) {


<?php  if($show_image == "true"){ ?>
  <a href="<?php echo get_the_permalink();?>">
                              $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );                              
                              <img src="<?php echo $url;?>" title="<?php echo get_the_title();?>" alt="<?php echo get_the_title();?>">
                       <?php } ?>

<?php  if($show_post_title == "true"){ ?>
    <h4 class="news-widget-title"><a href="<?php the_permalink();?>"><?php echo get_the_title();?></a></h4>
    <?php } ?>

<?php }

add_shortcode( 'my_news', 'latest_news' ); ?>

Hello Here Create my_news short code for multiple option

if you write shortcode [my_news] then it can be see the image and title of post.

if you pass attributes  in shortcode and set it value. like [my_news show_post_title=”flase” show_post_image=”true”] it will display image of post but title not display.

No comments:

Post a Comment

Thank You For Comment