Skip to main content

Posts

Showing posts with the label Shortcodes

Create Custom Shortcode in WordPress Post, Page and Plugin

WordPress introduced Shortcode API in version 2.5. Shortcode is a set of functions for creating macro codes. In our previous WordPress tutorial we have discussed about custom WordPress plugin creation. Now we will discuss how can you add extra functionality to your custom plugin. When a shortcode inserted into page or post, it is replaced with the other content. Means, we instruct WordPress to find the macro code under square brackets ([]) and replace it with the dynamic content. Shortcode is one of the most useful features in WordPress and you can use shortcode into post, page and plugin. The usage of shortcode is very easy and programming skills are not required. In this tutorial we will fetch recently added posts from the database and display using shortcode. Our shortcode will instruct how many post will be retrieved. Also it will be provide heading of the posts. A simple shortcode is look like this: [latestposts] Shortcode can be used with additional attributes as the following: [...

Create WordPress Shortcodes

WordPress shortcodes are super handy, especially when handing off a WordPress-based website to a client. The alternative to using shortcodes is creating complicated templates, and even then, you cannot adequately replace what shortcodes can do. I recently needed to implement a new shortcode for the Mozilla Hacks blog and was happy to learn how simple the WordPress API makes creating new shortcodes! Let me show you how to create your own WordPress shortcodes! First you start by creating a function within your theme's functions.php file. The function should return a string which replaces the shortcode within the rendered content. Your function can accept an $args argument which is a key=>value array of parameters passed in. function my_shortcode_routine($args) { // $args = array('key1' => 'value1', 'key2' => 'value2') $return = ''; // a bunch of logic and string-building here // return the result return $return; } Remember tha...