Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using shortcode in plugin development

458 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Using shortcode in plugin development

  1. 1. USING SHORTCODEIN PLUGIN DEVELOPMENTGHANSHYAM KHANALPROUD MEMBER OF WORDPRESS NEPALNCOMSERVICES.COMIT-S, NEPAL
  2. 2. SHORTCODE API• Introduced in WordPress 2.5• It’s a tag that can dynamically insert content into your wordpress site. 1. [mycode] 2. [mycode foo="bar" id="123" color="red" something="data"] 3. [mycode]Some Content[/mycode] 4. [mycode]<p><a href="http://example.com/">HTML Content</a></p>[/mycode] 5. [mycode]Content [another-shotcode] more content[/mycode] 6. [mycode foo="bar" id="123"]Some Content[/mycode]
  3. 3. CREATING SHORTCODEadd_shortcode( $tag , $func );function wpnepal_shortcode ( ) { return "Hello WP Nepal Meetup!";}add_shortcode(wpnepal, wpnepal_shortcode);[wpnepal]
  4. 4. CREATING SHORTCODE…function wpnepal_shortcode ( $atts, $content) { return "{$atts[greet]} WP Nepal Meetup! {$content}";}add_shortcode(wpnepal, wpnepal_shortcode);[wpnepal greet=“Hello”]Welcome[/wpnepal]
  5. 5. A PROBLEMNeed to add excerpt of a post to anotherTHE SOLUTIONEmbed Post Pluginhttp://wordpress.org/extend/plugins/embed-post
  6. 6. EMBED POSTfunction embed_post_shortcode( $atts ){ extract( shortcode_atts( array( post_id => 0, type => excerpt’ ), $atts ) ); global $post; [embed_post post_id=‘12’] $post = get_post($post_id); if($post->post_status == "publish"){ setup_postdata( $post ); $excerpt = <div class="embed_post">; $excerpt .= get_the_excerpt(); $excerpt .= </div>; wp_reset_query(); return $excerpt; }}add_shortcode(embed_post, embed_post_shortcode);
  7. 7. MORE SHORTCODE• Run Shortcode anywhere in template files:do_shortcode([wpnepal]);• Run shortcode in a text widget:add_filter(widget_text, do_shortcode);
  8. 8. THANK YOU QUESTIONS?

×