Create Shortcodes in WordPress by Web Design London

In the Plugins shorcode mostly used and these are very easy to use to the user point of view. User can easily embed the plugin different functionality using simple shortcode. The WordPress features a Shortcode API. The Shortcodes are basically text macro codes that can be inserted into a page, post, or custom post type in WordPress. Below is an example of shortcode API please see it:

<?php
add_shortcode( ‘webdirectorstwitter’, ‘wsc_twitter’ );
function wsc _twitter() {
return ‘<a href=”https://twitter.com/webdirectorsuk”>@webdirectorsuk</a>’;
}
?>

After this code written you can use the [webdirectorstwitter] shortcode any time in your content, what will do? It will be replaced with an HTML link to WebDirectors Twitter account when displayed in the browser. Many plugins are taking advantage of this feature of wordpress, and this is very power full feature of WordPress, many plugins out there currently take advantage of, mostly inserting small pieces of JavaScript to place a button or advertisement in the specific spot in a post. The Shortcodes can also be configured to accept attributes. The shortcodes are very useful for passing arguments to your custom functions, altering the output of the shortcode based on those arguments. Now modify your shortcode function to accept a site parameter:

<?php
add_shortcode( ‘ webdirectorstwitter ‘, ‘wsc_twitter’ );
function wsc_twitter( $atts, $content = null ) {
extract( shortcode_atts( array(‘person’ => ‘john’ // set attribute default), $atts ) );
if ( $person == ‘john’ ) {
return ‘<a href=”http://twitter.com/johnwebdesigner”>@johnwebdesigner</a>’;
}elseif ( $person == ‘william’ ) {
return ‘<a href=”http://twitter.com/williamwebdeveloper”>@williamwebdeveloper</a>’;
}elseif ( $person == ‘roony’ ) {
return ‘<a href=”http://twitter.com/roonyseo”>@roonyseo</a>’;
}
}
?>

The above code is the same shortcode as before we see, but in this code you are defining an attribute called person. By this attribute, specify that which person you want to display a Twitter link for. So you use the shortcode[webdirectorstwitter person=”john”] for display the Twitter URL for john. Other, you can also easily display the Twitter URL for Roony like so: [mytwitter person=”roony”]. The shortcodes can also accept the multiple attributes from the array set in your shortcode function.