QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Creating Custom WordPress Shortcodes using PHP

Creating Custom WordPress Shortcodes using PHP

This post is a part 5 of Creating a WordPress Admin Theme post series.

What is WordPress Shortcodes

WordPress shortcodes are a text patterns(with opening and closing square bracket tags) in post content which are replaced by php functions before displaying to the user.

For example:

[download url="http://qnimate.com/download.exe"]Download Now[/download]

The above shortcode is converted to something like this by a PHP function

<a href="http://qnimate.com/download.exe">Download Now</a>

WordPress Built-In Shortcodes

WordPress by default provides a lot of shortcodes which you can use. Here is a list of wordpress built-in shortcodes.

Creating your own Shortcodes

We can also register our own shortcodes. To register a similar kind of shortcode as mentioned above use this code

Put this code in theme or plugin file.

<?php
    //first attribute is the passed array of attributes of the shortcode
    //second attribute is the content inside the shortcode tags
    function download_callback($atts=null, $content=null)
    {
        //convert array into instance of variables
        extract($atts);

        //echo HTML code
        return "<a href='." $url ".'>." $content ".</a>";
    }

    //register a shortcode
    //first argument is shortcode name
    //second argument is php function for the shortcode
    add_shortcode("download", "download_callback");
?>

Displaying Shortcodes text In Posts

Sometimes we might want to display the shortcode text itself in the post. In that case we can wrap the shortcode inside square brackets.

[[download url="http://qnimate.com/download.exe"]Download Now[/download]]

Here the shortcode itself will be dislayed instead of executing callback and displaying function output.

Shortcode Availability

Shortcode by default cannot be used in widgets(text widget), comments(on front end or backend comments editor) and excerpts. We can enable usage of shortcode in those areas also by adding this code:

<?php
    //stops wordpress from wrapping shortcode tags separated into multiple lines inside paragraph tags.
    //more info check this http://wordpress.org/support/topic/shortcodes-are-wrapped-in-paragraph-tags
    add_filter( 'widget_text', 'shortcode_unautop' );
    add_filter( 'comment_text', 'shortcode_unautop' );
    add_filter( 'the_excerpt', 'shortcode_unautop' );
   
    //enable shortcode in widgets
    add_filter( 'widget_text', 'do_shortcode' );

    //enable shortcode in comments
    add_filter( 'comment_text', 'do_shortcode' );
   
    //enable shortcodes in excerpt
    add_filter( 'the_excerpt', 'do_shortcode');
?>

Aug 12, 2014Narayan Prusty
Advanced Guidelines To WordPress Theme DevelopmentScheduling Events In WordPress Using Cron Jobs
Comments: 0

    Leave a Reply Cancel reply

    To create code blocks or other preformatted text, indent by four spaces:

        This will be displayed in a monospaced font. The first four
        spaces will be stripped off, but all other whitespace
        will be preserved.
        
        Markdown is turned off in code blocks:
         [This is not a link](http://example.com)
    

    To create not a block, but an inline code span, use backticks:

    Here is some inline `code`.

    For more help see http://daringfireball.net/projects/markdown/syntax

    Narayan Prusty

    I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

    8 years ago 2 Comments WordPress
    Share this
    0
    GooglePlus
    0
    Facebook
    0
    Twitter
    0
    Linkedin
    • What is WordPress Shortcodes
    • WordPress Built-In Shortcodes
    • Creating your own Shortcodes
    • Displaying Shortcodes text In Posts
    • Shortcode Availability
    Related Articles
    • WordPress Custom PHP, JS and CSS Plugin
    • Changing WordPress Admin Footer Text
    • Remove HTML Tags from String using PHP
    • WordPress Remove Avatar from Comments Only
    • Advanced Guidelines To WordPress Theme Development
    Our Sponsor
    My Books

    2014 - 2015 © QNimate
    All tutorials MIT license