• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar
  • Home
  • Projects
  • Products
  • Themes
  • Tools
  • Request for Quote

Vengala Vinay

Having 12+ Years of Experience in Software Development

  • Home
  • WordPress
  • PHP
    • Codeigniter
  • Django
  • Magento
  • Selenium
  • Server
Home » Most Useful Shortcodes in WordPress

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:&#116;&#101;&#115;&#116;&#64;&#103;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;">&#116;&#101;&#115;&#116;&#64;&#103;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;</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”]

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

A little about the Author

Having 12+ Years of Experience in Software Development, Vinay is a principal software architect, senior systems engineer, and elite technical consultant. He specializes in bespoke PHP/WordPress development, high-performance Magento 2 & Shopify architectures, custom plugin/theme development from scratch, and legacy code modernization (including VB6, VB.NET, PyQt, and Crystal Reports). Known for solving complex database bottlenecks, speed optimization (Core Web Vitals), and advanced security code auditing, Vinay engineers production-ready systems designed to scale under heavy concurrent load conditions.



Chat on WhatsApp

Recent Posts

  • From Monolith to Microservices: A Deep Dive into Laravel’s Evolution with Docker Swarm and AWS Lambda for Scalable PHP Applications
  • Leveraging PHP 8.3 JIT and Laravel Octane for Blazing-Fast Microservices: A Performance Deep Dive
  • Kubernetes-Native PHP 8+ Deployments: Orchestrating Microservices with Laravel Octane and Docker Swarm
  • Beyond the Basics: Mastering Multi-Stage Docker Builds for Optimized Laravel Deployments with CI/CD Integration
  • Orchestrating Microservices with Laravel, Docker, and AWS ECS: A High-Availability Architecture for Modern Web Applications

Categories

  • apache (1)
  • Business & Monetization (390)
  • Centos (4)
  • Comparisons & Decision Making (55)
  • Debian (2)
  • Debugging & Troubleshooting (664)
  • Desktop Applications (14)
  • DevOps (9)
  • DevOps & Cloud Scaling (962)
  • Django (1)
  • Laravel (6)
  • Migration & Architecture (192)
  • Mobile Applications (24)
  • MySQL (1)
  • Performance & Optimization (873)
  • PHP (7)
  • PHP Development (49)
  • Plugins & Themes (244)
  • Programming Languages (10)
  • Python (20)
  • Ruby on Rails (1)
  • Security & Compliance (650)
  • SEO & Growth (492)
  • Server (118)
  • Softwares (1)
  • Ubuntu (9)
  • Uncategorized (6)
  • VB6 & VB.NET (8)
  • Web Applications & Frontend (19)
  • Web Assembly (Wasm) (2)
  • WordPress (22)
  • WordPress Plugin Development (728)
  • WordPress Theme Development (357)

Recent Posts

  • From Monolith to Microservices: A Deep Dive into Laravel's Evolution with Docker Swarm and AWS Lambda for Scalable PHP Applications
  • Leveraging PHP 8.3 JIT and Laravel Octane for Blazing-Fast Microservices: A Performance Deep Dive
  • Kubernetes-Native PHP 8+ Deployments: Orchestrating Microservices with Laravel Octane and Docker Swarm

Top Categories

  • DevOps & Cloud Scaling (962)
  • Performance & Optimization (873)
  • WordPress Plugin Development (728)
  • Debugging & Troubleshooting (664)
  • Security & Compliance (650)
  • SEO & Growth (492)

Our Products

  • ERP & LMS Systems (4)
  • Directories & Marketplaces (4)
  • Healthcare Portals (3)
  • Point of Sale (POS) (2)
  • E-Commerce Engines (2)

Our Services

  • E-Commerce Development (10)
  • WordPress Development (8)
  • Python & Desktop GUI (7)
  • General Consulting (7)
  • Legacy Modernization (5)
  • Mobile App Development (4)

Copyright © 2026 · Vinay Vengala