A shorcode is nothing but a simple set of function for creating macro code for use in post content. You need to add the shortcode to your functions.php file. In this example, you are going to create a shortcode called [foo] that will add a text "This is a test".
functions.php
You need to use functions.php a functions file, which resides in the theme subdirectory. This file acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization.
Create wordpress function
Edit functions.php, enter:
$ vi functions.php
Create a function called demo and append:
//[foo] function foo_demo( $atts ){ return "This is a demo."; } add_shortcode( 'foo', 'foo_demo' );
Save and close the file.
Test it
Edit or create a new post and add the following shortcode (in the post/page content):
[foo]
Save/publish post. Click on preview button to see your shotcode in action.
How do I pass parameter or attribute to shortcode?
The Shortcode API makes it easy to create shortcodes that support parameters like this:
[movie genre="animation" rating="PG"] [movie genre="comedy" rating="G"]
Add the following to your functions.php:
// shortcode [movie] function show_movie( $atts ){ $movie_details=""; // get attibutes and set defaults extract(shortcode_atts(array( 'gener' => 'Sci-Fi', 'rating' => 'G', 'date' => 0 ), $atts)); // Display info $movie_details = '<div class="quickinfo"><ul>'; $movie_details .= '<li>Genre: ' .$gener. '</li>'; $movie_details .= '<li>Rating: ' .$rating. '</li>'; $movie_details .= '<li>Release date: ' .$date. '</li>'; $movie_details .= '</ul></div>'; return $movie_details; } //add our shortcode movie add_shortcode('movie', 'show_movie'); add_action( 'init', 'register_shortcodes');
You can now insert it as follows in your post:
[movie genre="Animation" rating="PG" date="2012"]
The output would be:
<div class="quickinfo"><ul><li>Genre: Sci-Fi</li><li>Rating: PG</li><li>Release date: 1920</li></ul></div>
References:
- Wordprees shortcode API documentation.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 0 comments… add one now }