📜 ⬆️ ⬇️

10 Practical WordPress Shortcodes

Shortcodes are custom functions that save time when writing a post in WordPress. Today I want to introduce you to the 10 most used WordPress shortcodes.

Show a screenshot of any site


Do you want to take a screenshot of any site and show it on your site? This cool shortcode will help you with this. Copy this code into the functions.php file:
function wpr_snap( $atts, $content = null ) { extract(shortcode_atts(array( 'snap' => 'http://s.wordpress.com/mshots/v1/', 'url' => 'http://www.habrahabr.ru/', 'alt' => '', 'w' => '400', 'h' => '300' ), $atts)); $img = '<img src="' . $snap . '' . urlencode($url) . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '"/>'; return $img; } add_shortcode('snap', 'wpr_snap'); 

That's all. Now to see a screenshot of Habr, add a post with the following content:
 [snap url="http://www.habrahabr.ru/" alt="" w="400" h="300"] 


Add Paypal Donation Link


Many bloggers post a similar link on their blog and ask readers to help them financially. If, nevertheless, PayPal enables money to be received for Russia, you can add this shortcut to your functions.php:
 function cwc_donate_shortcode( $atts ) { extract(shortcode_atts(array( 'text' => ' ?', 'account' => 'REPLACE ME', 'for' => 'Habr', ), $atts)); global $post; if (!$for) $for = str_replace(" ","+",$post->post_title); return '<a class="donateLink" href="https://www.paypal.com/cgi-bin/webscr?cmd=cxlick&business='.$account.'&item_name=+'.$for.'"> '.$text.' </a>'; } add_shortcode('donate', 'cwc_donate_shortcode'); 

And in the right place
 [donate] 


Bringing email addresses to clickable


Any WordPress blog is of course associated with spam. If you show users email addresses or your favorite mail on your website, you can make life a little harder for spammers to collect mail. Just add this code to functions.php:
 function cwc_mail_shortcode( $atts , $content=null ) { for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';'; return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>'; } add_shortcode('mailto', 'cwc_mail_shortcode'); 

And you can not worry about your soap in the bases of African billionaires, who bequeathed you all that they have. Just wrap soap in tags.
 [mailto]email@yourdomain.ru[/mailto] 

')

Creating private content


You always want to not copy-paste content from the site. You can make showing certain content only registered. Add this to functions.php:
 function cwc_member_check_shortcode( $atts, $content = null ) { if ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) return $content; return ''; } add_shortcode( 'member', 'cwc_member_check_shortcode' ); 

And wrapping the content in tags
 [member]      -.[/member] 


PDF display in frame


There is a good way to show the content of the pdf file on the site. Google docs comes to the rescue. Add a new function to functions.php:
 function cwc_viewpdf($attr, $url) { return '<iframe src="http://docs.google.com/viewer?url=' . $url . '&embedded=true" style="width:' .$attr['width']. '; height:' .$attr['height']. ';" frameborder="0">    </iframe>'; } add_shortcode('embedpdf', 'cwc_viewpdf'); 

And in the content for inserting pdf use this tag:
 [embedpdf width="600px" height="500px"]http://infolab.stanford.edu/pub/papers/google.pdf[/embedpdf] 


“Feed only” content


This shortcut allows you to show certain content only to RSS-subscribers. And again we add a function to functions.php:
 function cwc_feedonly_shortcode( $atts, $content = null) { if (!is_feed()) return ""; return $content; } add_shortcode('feedonly', 'cwc_feedonly_shortcode'); 

And wrapping content that we want to show to subscribers in tags
 [feedonly] -,    .[/feedonly] 


Link “Retweet”


Many people know that twitter is a traffic generator for a blog. And retweets increase traffic to the site even more.
 function tweetmeme(){ return '<div class="tweetmeme"> <script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script> </div>'; } add_shortcode('retweet', 'tweetmeme'); 

And in the right place
 [retweet] 


Display attached images to post


This short can show the last image attached to the post. Add this shortcut to functions.php:
 function cwc_postimage($atts, $content = null) { extract(shortcode_atts(array( "size" => 'thumbnail', "float" => 'none' ), $atts)); $images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() ); foreach( $images as $imageID => $imagePost ) { $fullimage = wp_get_attachment_image($imageID, $size, false); $imagedata = wp_get_attachment_image_src($imageID, $size, false); $width = ($imagedata[1]+2); $height = ($imagedata[2]+2); return '<div class="postimage" style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';"> '.$fullimage.' </div>'; } } add_shortcode("postimage", "cwc_postimage"); 

And in the post add a tag
 [postimage] 


Youtube Videos


If you want to post a video from Youtube on your blog, then add a function to functions.php:
 function cwc_youtube($atts) { extract(shortcode_atts(array( "value" => '', "width" => '475', "height" => '350', "name"=> 'movie', "allowFullScreen" => 'true', "allowScriptAccess"=>'always', ), $atts)); return '<object style="height: '.$height.'px; width: '.$width.'px"> <param name="'.$name.'" value="'.$value.'"> <param name="allowFullScreen" value="'.$allowFullScreen.'"> <param name="allowScriptAccess" value="'.$allowScriptAccess.'"> <embed src="'.$value.'" type="application/x-shockwave-flash" allowfullscreen="'.$allowFullScreen.'" allowScriptAccess="'.$allowScriptAccess.'" width="'.$width.'" height="'.$height.'"> </embed> </object>'; } add_shortcode("youtube", "cwc_youtube"); 

And in the post you can use the tag
 [youtube value="http://www.youtube.com/watch?v=1aBSPn2P9bg"] 


RSS feed display


And lastly, you can show the RSS feed in post using the function
 include_once(ABSPATH.WPINC.'/rss.php'); function cwc_readRss($atts) { extract(shortcode_atts(array( "feed" => '', "num" => '1', ), $atts)); return wp_rss($feed, $num); } add_shortcode('rss', 'cwc_readRss'); 

and tag
 [rss feed="http://feeds.feedburner.com/catswhocode" num="5"] 


Original: css.dzone.com

Source: https://habr.com/ru/post/129998/


All Articles