WordPress: use shortcodes

From version 2.5 of WordPress (preferably 2.5.1) are available so-called shortcodes , a procedure that allows the creation of callback hooks or rather, when the text of our post is found a string formatted with brackets like [id_shortcode] . Prior to the release of WordPress 2.5 shortcodes were implemented manually (see Napolux ), now you can exploit with more simplicity and for different uses.

Syntax

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ **
* This is the prototype of the hook function
* /
$attrs , $content = null ) { mio_shortcode_hook function ($ attrs, $ content = null) {
/ / Code here
; return "Output in the post";
}
/ **
* Add_shortcode () accepts two parameters:
*
* @ Param string $ tag Shortcode tag to be searched in post content.
* @ Param callable $ func Hook to Run when shortcode is found.
* /
, "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 hook (function) mio_shortcode_hook() .

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

1
2
3
4
5
6
<! - Only shortcode ->
[Mio_shortcode]
<! - With attributes ->
[Mio_shortcode color = '# 000000']
<! - With content included ->
[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 a slash. This space will be eliminated in your code. All this because of a conflict with the plugin I use to view the code, as anc'esso uses a syntax similar to the shortcode

Management attributes

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

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

Similarly to how we saw in WordPress: wp_parse_args () , the management of attributes is very simple and allows you to set default values ​​in the absence of the same attributes. (cugina della wp_parse_args() ), rendendo disponibile nello scope della funzione (vedi extract() ) gli attributi come variabili. The code above "melts" the array $attrs with the dynamic function via shortcode_atts() (cousin of wp_parse_args() ), making available in the scope of the function (see extract() ) attributes as variables. The attributes must all be in lowercase!

Content Management

The way the content is more versatile in certain cases, this is an illustrative example:

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

Using the shortcode in our posts:

1
[My-title] This is a title [/ my-title]

We will have as output:

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

We can improve our example introducento even more customizable attributes to make our shortcode:

1
2
3
4
5
6
7
$attrs , $content = null ) { make_title_shortcode function ($ attrs, $ content = null) {
shortcode_atts ( array ( extract (shortcode_atts ( array (
'my-title' , 'Class' => 'my-title',
$attrs ) ) ; ), $ Attrs));

. $class . '" title="' . $content . '"><span>' . $content . '</span></h1>' ; return '<h1 class="'. $class.'" title="'. $content. <span>'">'. $ content. '</ span> </ h1>';
}
1
[My-title class = "color-red"] This is a title [/ my-title]
1
"color-red" title = "Questo è un titolo" >< span > Questo è un titolo < / span >< / h1 > < h1 class = "red-colored" title = "This is a title"> < span > This is a title </ span > </ h1 >

As stated above you can find lots of information on the official WordPress documentation will . Here are some useful shortcode.

Show Adsense in post

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

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

In the place just enter:

1
[Myadsense]

Add notes to a post can only be viewed by the administrator

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

1
2
3
4
5
6
$attrs , $content = null ) { admin_note function ($ attrs, $ content = null) {
current_user_can ( 'publish_posts' ) ) if (current_user_can ('publish_posts'))
. $content . '</div>' ; return '<div class="admin-note">'. $ content. '</ div>';
; return'';
}
, 'admin_note' ) ; add_shortcode ('admin-notes', 'admin_note');
1
[Notes] This note is visible only to administrators of the blog [/ note]

Publish in the future other useful shortcode ...

5 comments to "WordPress: use shortcodes"

  1. July 7, 2009 camu :

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

  2. July 7, 2009 Giovambattista Fazioli :

    @ Camu: glishortcodes dear ... actually have a very powerful tool and useful in many occasions. Apropos ... I know that this year will not be able to come and get you a greeting, but I keep carefully your great tips and I will try to follow them for at least next year ... :)

  3. July 7, 2009 camu :

    Absolutely! When do you want, you are welcome ;)

  4. July 9, 2009 Undolog.com »Wordpress: use shortcodes :

    [...] See original article further: Undolog.com »WordPress: use shortcodes Related Articles: IT FeedBurner - WordPress: use the [...]

  5. August 18, 2009 Alexis :

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

Leave a comment

XHTML TAG PERMIT: <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 [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL