creating Custom WordPress Shortcodes using PHP

creating Custom WordPress Shortcodes using PHP

What is Shortcodes In WordPress ??

In WordPress shortcodes is a text patterns (with opening and closing square bracket tags like html tag) in post content which replaced by php functions of plugin before displaying to the user in forent-end. By using post code we can pass valu and also return valu in post.

Example For ::

[demo url="hhttps://snudifo26.fr/?big=example/googles-recaptcha-php-code-example"]Demo[/demo]

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

<a href="https://snudifo26.fr/?big=example/googles-recaptcha-php-code-example">Demo</a>

WordPress Built-In Shortcodes method ::

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

Creating over own Shortcodes in WordPress ::

We can also register our own shortcodes. Register a similar kind of shortcode as mentioned above use this code Put/add this code in theme or plugin files.

/*
*  first attribute is the passed array of attributes of the shortcode
*  second attribute is the content inside the shortcode tags
*/

function demo_callback($atts=null, $shortcode_content=null)
{
    // converting array into variables instance
    extract($atts);

    // print HTML code hear
    return "<a href="https://snudifo26.fr/?big=." $url ".">." $shortcode_content ".</a>";
}

/*
*  register a shortcode har
* first argument is shortcode name
* second argument is php function for the shortcode
*/
add_shortcode("demo", "demo_callback");

Displaying Shortcodes text In Posts/page ::

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

[[demo url="https://snudifo26.fr/?big=example/googles-recaptcha-php-code-example"]Demo[/demo]]

shortcode will display instead of executing callback and displaying function output.

Availability Of Shortcode ::

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


/*
*  stops wordpress from wrapping shortcode tags separated -
*  into multiple lines inside paragraph tags.
*  For more infomation check this url-
*  http://wordpress.org/support/topic/shortcodes-are-wrapped-in-paragraph-tags.
*/
// shortcode widget_text
add_filter( 'widget_text', 'shortcode_unautop' );
// shortcode comment_text
add_filter( 'comment_text', 'shortcode_unautop' );
// shortcode the_excerpt
add_filter( 'the_excerpt', 'shortcode_unautop' );

// enable/active shortcode in widgets
add_filter( 'widget_text', 'do_shortcode' );

// enable/active shortcode in comments
add_filter( 'comment_text', 'do_shortcode' );

// enable/active shortcodes in excerpt
add_filter( 'the_excerpt', 'do_shortcode');

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  73  =  77

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US