Wordpress: Using the shortcodes

Friday, July 3, 2009

Since version 2.5 of Wordpress (better than 2.5.1) are available so-called shortcodes, a procedure that allows the creation of callback, or better than the hook, when our place in the text it finds a string formatted with brackets like [id_shortcode] . Before the release 2.5 of the WordPress shortcodes were implemented manually (Napolux), now you can exploit them more easily and for many different uses.

Syntax

To use shortcodes is sufficient to create a hook function, with parameters and set the standard shortcodes through add_shortcode()

PHP:
  1. / **
  2. * This is the prototype of the hook function
  3. * /
  4. $attrs , $content = null ) { mio_shortcode_hook function ($ attrs, $ content = null) (
  5. / / Code here
  6. ; return "Output in the post";
  7. )
  8. / **
  9. * Add_shortcode () takes two parameters:
  10. *
  11. * @ Param string $ tag shortcode tags to be searched in post content.
  12. * @ Param callable $ func Hook to run when shortcode is found.
  13. * /
  14. , "mio_shortcodes_hook" ) ; add_shortcode (mio_shortcode "," mio_shortcodes_hook ");

This code can be placed in the file functions.php When writing your post by including mio_shortcode brackets will be executed as code dell'hook (the function) mio_shortcode_hook()

The hook function, and our shortcode can be used in various ways as needed:

HTML:
  1. <! - Only shortcode ->
  2. [mio_shortcode]
  3. <! - With attributes ->
  4. [mio_shortcode color = '# 000000']
  5. <! - With content included ->
  6. [mio_shortcode color = '# 000000'] I am content [/ mio_shortcode]

Note: the above code I had to insert a space between the shortcode closing the opening of the bracket and the slash. This area will be eliminated in your code. All this because of a conflict with the plugin that I use to see the code, as anc'esso uses a syntax similar to the shortcode

Management attributes

The attributes included in a shortcode are easy to handle and you can set the default value. Here, for example how to create a shortcode that operates a title specially formatted:

PHP:
  1. $attrs , $content = null ) { mio_shortcode_hook function ($ attrs, $ content = null) (
  2. shortcode_atts ( array ( extract (shortcode_atts (array (
  3. 'attributo 1 default' , 'attr_1' => 'attribute 1 default',
  4. 'attributo 2 default' , 'attr_2' => 'attribute 2 default',
  5. / / ... Etc
  6. $attrs ) ) ; ), $ Attrs));
  7. / / ...
  8. )

Similarly to how we saw Wordpress: wp_parse_args (), the management of attributes is very simple and allows you to set default values in the absence of the attributes themselves. The code above "melts" the array $attrs with that dynamic through the function shortcode_atts() (cousin of wp_parse_args() making available in the scope of the function (see extract() attributes as variables. Attributes must be all lowercase!

Content Management

The way the content is the most versatile in specific cases, here is an illustrative example:

PHP:
  1. $attrs , $content = null ) { make_title_shortcode function ($ attrs, $ content = null) (
  2. . $content . '"><span>' . $content . '</span></h1>' ; return '<h1 title="'. $content.'"> <span>'. $ content. '</ span> </ h1>';
  3. )
  4. , 'make_title_shortcode' ) ; add_shortcode ( 'my-title', 'make_title_shortcode');

Using the shortcode in our post:

HTML:
  1. [my-title] This is a title [/ my-title]

We will have as output:

HTML:
  1. "Questo è un titolo" ><span> Questo è un titolo < / span>< / h1> <h1 title = "Questo is a titolo"> <span> This is a title </ span> </ h1>

We can improve our model by introducing even more customizable attributes to make our shortcode:

PHP:
  1. $attrs , $content = null ) { make_title_shortcode function ($ attrs, $ content = null) (
  2. shortcode_atts ( array ( extract (shortcode_atts (array (
  3. 'my-title' , 'class' => 'my-title',
  4. $attrs ) ) ; ), $ Attrs));
  5. . $class . '" title="' . $content . '"><span>' . $content . '</span></h1>' ; return '<h1 class="'. $class.'" title="'. $content.'"> <span>'. $ content. '</ span> </ h1>';
  6. )

HTML:
  1. [my-title class = "color-red"] This is a title [/ my-title]

HTML:
  1. "color-red" title = "Questo è un titolo" ><span> Questo è un titolo < / span>< / h1> <h1 class = "color-red" title = "Questo is a titolo"> <span> This is a title </ span> </ h1>

As already mentioned you will find extensive information on documentazine official WordPress. Here are some useful shortcode.

Show AdSense in posts

Enter the code below into your file functions.php Change the Google AdSense code with your own.

PHP:
  1. show_adsense function () (
  2. return '<script type="text/javascript"> <! --
  3. google_ad_client = "pub-9877654123213210";
  4. google_ad_slot = "9876543210";
  5. google_ad_width = 468;
  6. google_ad_height = 60;
  7. //-->
  8. </ script>
  9. <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </ script>
  10. ';
  11. )
  12. , 'adsense' ) ; add_shortcode ( 'myadsense', 'adsense');

In the place just enter:

HTML:
  1. [myadsense]

Add notes to a post visible only by the administrator

This shortcode allows you to add notes to a post visible only to an administrator.

PHP:
  1. $attrs , $content = null ) { admin_note function ($ attrs, $ content = null) (
  2. current_user_can ( 'publish_posts' ) ) if (current_user_can ( 'publish_posts'))
  3. . $content . '</div>' ; return '<div class="admin-note">'. $ content. '</ div>';
  4. ; return'';
  5. )
  6. , 'admin_note' ) ; add_shortcode ( 'admin-notes', 'admin_note');

HTML:
  1. [note] This note is visible only to administrators of the blog [/ note]

Publish in future other shortcode useful ...

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

5 comments to "Wordpress: Using the shortcodes"

  1. getAvatar 1.0
    07 lug, 2009 camu:

    As they say here in America ... Awesome! I did not know this thing of short codes in wordpress, I've always done "by hand" with the old method. Thank you.

  2. getAvatar 1.0
    07 lug, 2009 Giovambattista Fazioli:

    @ camu: dear ... glishortcodes actually are a very powerful and useful in many occasions. Apropos ... I know that this year will not be able to come and get you a greeting, but kept carefully your very useful suggestions and I will try to follow them for at least the next year ... :)

  3. getAvatar 1.0
    07 lug, 2009 camu:

    Absolutely! You want, you are welcome ;)

  4. getAvatar 1.0
    09 lug, 2009 Undolog.com »Wordpress: Using the shortcodes:

    [...] Further consult original article: Undolog.com »Wordpress: Using the shortcodes Related articles: Pligg - WordPress: Using the [...]

  5. getAvatar 1.0
    August 18, 2009 Alessio:

    Excellent :) or so I wrote a small plugin bbcode :)

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL