Most Useful Shortcodes in WordPress
- Calculate Years of experience using WordPress Shortcode
- Get Current Website name using WordPress Shortcode
- Get Current Website tagline using WordPress shortcode
- Get Current Date using WordPress shortcode
- Get Current Day’s year using WordPress shortcode
- Get Current Date’s Day using WordPress shortcode
- Get Current Post’s Publish Date using WordPress Shortcode
- Get Current Post’s Author Name using WordPress Shortcode
- Get Admin Email using WordPress Shortcode
- Get Top 10 latest posts using WordPress shortCode
- Get latest posts by date using WordPress shortcode
- Get latest posts by category using WordPress shortcode
- Get latest posts by tag using WordPress shortcode
- Embedded External or Internal Images by height and width using WordPress shortcode
- Embedded a snapshot of any website using WordPress shortcode
- Add a Paypal donation link easily using WordPress shortcode
- Obfuscate email address using WordPress shortcode
- Create a private content using WordPress shortcode
- Embed a pdf in the iframe using WordPress shortcode
- Display content in feed-only using WordPress shortcode
- Embed an RSS Feed
- Display Adsense ad using WordPress shortcode
- Get published posts count using WordPress shortcode
- Get drafted posts count using WordPress Shortcode
- Get total approved comments counts using WordPress shortcode
- Get total pending comments counts using WordPress shortcode
- Get total published items by custom post type using WordPress shortcode
Calculate Years of experience using WordPress Shortcode
function yearsofexperience( $atts ) { $args = shortcode_atts( array( 'from' => '01-06-2013', // [date-month-year] 'to' => 'now', //20-08-2020 ), $atts ); $datetime1 = new DateTime($args['from']); $datetime2 = new DateTime($args['to']); $interval = $datetime1->diff($datetime2); $exp_years = $interval->format("%y"); if($exp_years == 0){ $exp_years.=".".$interval->format("%m")."+"; }else if( $interval->format("%m") > 0 || $interval->format("%d") > 0){ $exp_years.="+"; } return $exp_years; } add_shortcode( 'yearsofexperience', 'yearsofexperience' );
[yearsofexperience] = 7.2+
[yearsofexperience from=”01-05-2015″] = 5.3+
[yearsofexperience from=”01-05-2015″ to=”01-05-2016″] = 1
Get Current Website name using WordPress Shortcode
function website_name(){ return get_bloginfo('name'); } add_shortcode('website_name','website_name');
[website_name]
Get Current Website tagline using WordPress shortcode
function website_tagline(){ return get_bloginfo('description'); } add_shortcode('website_tagline','website_tagline');
[website_tagline]
Get Current Date using WordPress shortcode
function current_date(){ return date('j-m-Y'); } add_shortcode('current_date','current_date');
[current_date]
Get Current Day’s year using WordPress shortcode
function current_year(){ return date('Y'); } add_shortcode('current_year','current_year');
[current_year]
Get Current Date’s Day using WordPress shortcode
function current_day(){ return date('j'); } add_shortcode('current_day','current_day');
[current_day]
Get Current Post’s Publish Date using WordPress Shortcode
function current_post_date(){ return get_the_date( 'dS M Y'); } add_shortcode('current_post_date','current_post_date');
[current_post_date]
Get Current Post’s Author Name using WordPress Shortcode
function current_post_author_name(){ return get_the_author_meta( 'display_name'); } add_shortcode('current_post_author_name','current_post_author_name');
[current_post_author_name]
Get Admin Email using WordPress Shortcode
function website_admin_email(){ return get_bloginfo('admin_email'); } add_shortcode('website_admin_email','website_admin_email');
[website_admin_email]
Get latest 10 posts using WordPress shortCode
function latestposts($atts){ $args = shortcode_atts( array( 'limit' => '10', 'post_status' =>'publish' ), $atts ); $recent_posts = wp_get_recent_posts(array( 'numberposts' => $args['limit'], // Number of recent posts thumbnails to display 'post_status' => $args['post_status']// Show only the published posts )); $data_return = "<ul>"; foreach($recent_posts as $post) : $data_return.= "<li><a href='".get_permalink($post['ID'])."'>".$post['post_title']."</a></li>"; endforeach; $data_return .="<ul>"; return $data_return; } add_shortcode('latestposts','latestposts');
[latestposts]
[latestposts limit=”5″]
[latestposts limit=”8″ post_status=”draft”]
Get latest posts by date using WordPress shortcode
function latestpostsbydate($atts){ $args = shortcode_atts( array( 'year' => date('Y'), 'month' => date('M'), 'day' => date('j'), ), $atts ); $query_args = array( 'date_query' => array( array( 'year' => $args['year'], 'month' => $args['month'], 'day' => $args['day'], ), ), ); $query = new WP_Query( $args ); $data_return = "<ul>"; while ( $query->have_posts() ) { $query->the_post(); $data_return.= "<li><a href='".get_permalink()."'>".get_the_title()."</a></li>"; } $data_return .="<ul>"; return $data_return; } add_shortcode('latestpostsbydate','latestpostsbydate');
[latestpostsbydate]
[latestpostsbydate year=”2020″ month=”08″ day=”21″]
Get latest posts by category using WordPress shortcode
function latestpostsbycategory($atts){ $args = shortcode_atts( array( 'cat' => 1, // uncategorized ), $atts ); $query_args = null; if(is_int(intval($args['cat']))){ $query_args = array( 'cat' => $args['cat'], ); }elseif(is_string(strval($args['cat']))){ $query_args = array( 'category_name' => $args['cat'], ); } $query = new WP_Query( $query_args ); $data_return = "<ul>"; while ( $query->have_posts() ) { $query->the_post(); $data_return.= "<li><a href='".get_permalink()."'>".get_the_title()."</a></li>"; } $data_return .="<ul>"; return $data_return; } add_shortcode('latestpostsbycategory','latestpostsbycategory');
[latestpostsbycategory]
[latestpostsbycategory cat=”10″]
[latestpostsbycategory cat=”php”]
Get latest posts by tag using WordPress shortcode
function latestpostsbytag($atts){ $args = shortcode_atts( array( 'tag' => 1, // uncategorized ), $atts ); $query_args = null; if(is_int(intval($args['tag']))){ $query_args = array( 'tag_id' => $args['tag'], ); }elseif(is_string(strval($args['tag']))){ $query_args = array( 'tag' => $args['tag'], ); } $query = new WP_Query( $query_args ); $data_return = "<ul>"; while ( $query->have_posts() ) { $query->the_post(); $data_return.= "<li><a href='".get_permalink()."'>".get_the_title()."</a></li>"; } $data_return .="<ul>"; return $data_return; } add_shortcode('latestpostsbytag','latestpostsbytag');
[latestpostsbytag]
[latestpostsbytag tag=”25″]
[latestpostsbytag tag=”custom-coding”]
Embedded External or Internal Images by height and width using WordPress shortcode
function embedimage($atts){ $args = shortcode_atts( array( 'domain' => site_url(), //http://vengalavinay.com 'path' => '', 'alt' => "Vengalavinay.com", 'width' => "250", 'height' => "250", ), $atts ); $image_src = rtrim($args['domain'],"/\\")."/".$args['path']; // <img src="" alt="" width="" height="" /> return '<img src="'.$image_src.'" alt="'.$args['alt'].'" width="'.$args['width'].'" height="'.$args['height'].'" />'; } add_shortcode('embedimage','embedimage');
[embedimage path=”wp-content/uploads/2020/07/sample.png”]
[embedimage domain =”http://cdn.vengalavinay.com” path=”wp-content/uploads/2020/07/sample.png”]
[embedimage domain =”http://cdn.vengalavinay.com” path=”wp-content/uploads/2020/07/sample.png” alt=”sample text”]
[embedimage domain =”http://cdn.vengalavinay.com” path=”wp-content/uploads/2020/07/sample.png” alt=”sample text” width=”300″ height=”300″]
Embedded a snapshot of any website using WordPress shortcode
function embedsnapshot($atts) { $args =shortcode_atts(array( "snap" => 'http://s.wordpress.com/mshots/v1/', "url" => 'https://www.vengalavinay.com', "alt" => 'vengalavinay.com', "width" => '400', // width "height" => '300' // height ), $atts); $img = '<img src="' . $args['snap'] . '' . urlencode($args['url']) . '?w=' . $args['width'] . '&h=' . $args['height'] . '" alt="' . $args['alt'] . '"/>'; return $img; } add_shortcode("embedsnapshot", "embedsnapshot");
[embedsnapshot]
[embedsnapshot url=”https://www.vengalavinay.com”]
[embedsnapshot url=”https://www.vengalavinay.com” width=”250″ height=’250″]
Add a Paypal donation link easily using WordPress shortcode
function paypaldonation( $atts ) { $args =shortcode_atts(array( 'text' => 'Make a donation', 'account' => 'your id', 'for' => '', ), $atts); global $post; if (!$args['for']) $for = str_replace(" ","+",$post->post_title); return '<a class="donateButton" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$args['account'].'&item_name=Donation+for+'.$args['for'].'">'.$args['text'].'</a>'; } add_shortcode('donate', 'paypaldonation');
[donate]
[donate account=”[email protected]” for=”product”]
Obfuscate email address using WordPress shortcode
function emailid( $atts ) { $args =shortcode_atts(array( 'mail' => '[email protected]', ), $atts); for ($i = 0; $i < strlen($args['mail']); $i++) $encodedmail .= "&#" . ord($args['mail'][$i]) . ';'; return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>'; } add_shortcode('emailid', 'emailid');
[emailid]
[emailid mail=”[email protected]”]
<a href="mailto:test@gmail.com">test@gmail.com</a>
Create a private content using WordPress shortcode
function onlyloggedin( $atts, $content = null ) { if ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) return $content; return ''; } add_shortcode( 'onlyloggedin', 'onlyloggedin' );
[onlyloggedin] private content [/onlyloggedin]
Embed a pdf in the iframe using WordPress shortcode
function embedpdf($atts) { $args =shortcode_atts(array( 'pdf' => 'pdfhere', 'width' =>'600', 'height' => '800' ), $atts); return '<iframe src="http://docs.google.com/viewer?url=' . $args['pdf'] . '&embedded=true" style="width:' .$args['width']. '; height:' .$args['height']. ';" frameborder="0">Your browser should support iFrame to view this PDF document</iframe>'; } add_shortcode('embedpdf', 'embedpdf');
[embedpdf]
[embedpdf pdf=”pdf path”]
[embedpdf pdf=”pdf path” width=”300″ height=”600″]
Display content in feed-only using WordPress shortcode
function infeedonly( $atts, $content = null) { if (!is_feed()) return ""; return $content; } add_shortcode('infeedonly', 'infeedonly');
[infeedonly] content here [/infeedonly]
Embed an RSS Feed
function embedrss( $atts, $content = null) { include_once(ABSPATH.WPINC.'/rss.php'); $args =shortcode_atts(array( 'feed' => 'http://', 'num' =>'1', ), $atts); return wp_rss($args['feed'], $args['num']); } add_shortcode('embedrss', 'embedrss');
[embedrss feed=”feed URL” num=”15″]
Display Adsense ad using WordPress shortcode
function adsenseblock($atts){ /* <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="CLIENT ID" data-ad-slot="SLOT ID" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> * */ $args =shortcode_atts(array( 'client' => '00000', 'slot' =>'111111', ), $atts); return '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="'.$args['client'].'" data-ad-slot="'.$args['slot'].'" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>'; } add_shortcode('adsenseblock','adsenseblock');
[adsenseblock client=”00000000″ slot=”00000000″]
Get published posts count using WordPress shortcode
function getpostcount($atts){ $args =shortcode_atts(array( 'type' => 'post', 'status' => 'publish', ), $atts); $postsCount = wp_count_posts($args['type']); if(isset($postsCount->{$args['status']})){ return $postsCount->{$args['status']}; } } add_shortcode('getpostcount','getpostcount');
[getpostcount]
Get drafted posts count using WordPress Shortcode
function getpostcount($atts){ $args =shortcode_atts(array( 'type' => 'post', 'status' => 'publish', ), $atts); $postsCount = wp_count_posts($args['type']); if(isset($postsCount->{$args['status']})){ return $postsCount->{$args['status']}; } } add_shortcode('getpostcount','getpostcount');
[getpostcount status=”draft”]
Get total approved comments counts using WordPress shortcode
function getcommentcount($atts){ $args =shortcode_atts(array( 'status' => 'approved', ), $atts); $commentsCount = wp_count_comments(); if(isset($commentsCount->{$args['status']})){ return $commentsCount->{$args['status']}; } } add_shortcode('getcommentcount','getcommentcount');
[getcommentcount]
Get total pending comments counts using WordPress shortcode
function getcommentcount($atts){ $args =shortcode_atts(array( 'status' => 'approved', ), $atts); $commentsCount = wp_count_comments(); if(isset($commentsCount->{$args['status']})){ return $commentsCount->{$args['status']}; } } add_shortcode('getcommentcount','getcommentcount');
[getcommentcount status=”moderated”]
Get total published items by custom post type using WordPress shortcode
function getpostcount($atts){ $args =shortcode_atts(array( 'type' => 'post', 'status' => 'publish', ), $atts); $postsCount = wp_count_posts($args['type']); if(isset($postsCount->{$args['status']})){ return $postsCount->{$args['status']}; } } add_shortcode('getpostcount','getpostcount');
[getpostcount type=”questions”]
Get total draft items by custom post type using WordPress shortcode
function getpostcount($atts){ $args =shortcode_atts(array( 'type' => 'post', 'status' => 'publish', ), $atts); $postsCount = wp_count_posts($args['type']); if(isset($postsCount->{$args['status']})){ return $postsCount->{$args['status']}; } } add_shortcode('getpostcount','getpostcount');
[getpostcount type=”questions” status=”draft”]
Leave a Reply
You must be logged in to post a comment.