Reading Progress:

How to Create WordPress Shortcodes and Use

Writen by SATISH KUMAR

28 Jun, 2019

Reading Time: ( Word Count: )

Wordpress Shotcode

How to create WordPress Shortcodes and Use them

WordPress is a free and open source CMS (content management system) that allows you to build websites easily. It has millions of users worldwide and is the most widely used blogging software.

Shortcodes allow you to add custom code snippets into posts or pages. They provide a quick way to insert data from other sources such as databases, APIs, and external services. In this tutorial, I’ll show you how to install and configure WordPress Shortcodes.

Shortcode Basics:

A shortcode is a special type of PHP function that can be inserted into any post or page on your site. The basic idea behind a shortcode is to make it easy to add content from an outside source without having to manually copy and paste it in. For example, if you wanted to display some text from another website, you could simply create a shortcode that would do that for you.

The first thing you need to know about creating a shortcode is that it requires two things:

1. A unique identifier called a callback

2. An optional parameter

The callback is what will tell WordPress where to send the information you want to use inside the shortcode. When adding a shortcode, you must declare its callback using the tag. This tells WordPress exactly where to look for the data you want to insert. If no callback is given, then the default one is used.

For example, let’s say we have a database table named “posts” with columns “title”, “body”, and “date”. We also have a separate database table named “categories” with columns “id” and “name”. To insert the title of a category into our post, we’d write something like this:

This is the body

In this case, the shortcode declares that it wants to pull the value from the column “name” in the categories table.

Now, there are three different ways to pass variables to a shortcode. You can either pass them directly through the URL, by putting them in the query string, or by setting them as global variables. Let’s go over each method.

Passing Variables Through the URL

These are known as query strings because they’re added to the end of the URL after the? character.

To pass variables through the URL, all you have to do is add the variable name between the brackets . So, for example, if we wanted to display the value of the variable $category_name, we could put the following in our URL:

You can use multiple variables by separating them with commas. For example, if we wanted both the name and slug of a category, we could enter these values separated by commas:

You can also use double quotes instead of single quotes when declaring variables.

Using Global Variables

Global variables are set at the beginning of the script. These variables can then be accessed throughout the entire script.

To access global variables, just prefix their names with $GLOBALS . For example, to get the name of the current category, you could use this line of code:

$currentCategory $GLOBALS ;

If you want to store a variable permanently, you can assign it to the $GLOBAL array via the register_globals option in wp-config.php.

So far, we’ve only talked about passing variables to shortcodes. But there’s more! There’s actually a second kind of shortcode called a filter hook. Filters hooks are similar to callbacks except that they don’t require a specific ID. Instead, they take whatever parameters are passed to them and perform some action based on those parameters.

Let’s say we have a list of posts that we want to show on a particular page. We might want to sort them by date. To accomplish this, we could define a filter that accepts two arguments:

The first argument is the number of days since the start of the year. The second argument is the name of the month. In this case, we’ll use the month name (January) and subtract 365 from today’s date. Then we’ll divide that result by 30 to convert it back to an integer. Finally, we’ll compare the resulting integer to 0.

The output would look something like this:

As you can see, we created a simple function that takes two arguments and returns a number. We then registered this function as a filter hook. Now whenever someone calls the_month(1), it will run the function defined in the above snippet.

Filters Hooks vs Callback Functions

A callback function is very similar to a filter hook in that it doesn’t need an ID. However, unlike filters, which are used to modify data, callback functions are used to manipulate how data is displayed.

For example, let’s say we want to create a custom archive page. We could do this using a filter hook. This means that the function must exist outside of any post type. It should also accept one parameter, which is the name of the post type. After calling the function, we’d return the HTML markup needed to render the desired page.

In contrast, a callback function exists within a post type. That means it can be used anywhere inside that post type. For example, we could create a callback function that adds a class to every post link on the site.

When creating your own plugin or theme, you may find yourself needing to write either a filter or a callback function. If you’re not sure whether you need one, here are a few guidelines to help you decide:

Filter Hooks

• Filter hooks are useful for modifying data in a way that won’t affect other parts of the site. For example, if you wanted to add a “read” button next to each comment in your blog, you could easily create a filter hook that does so by adding a new parameter to the comments loop.

• You can pass multiple arguments to a filter hook. For example, the following code creates a filter hook that accepts three arguments: 1. A string containing the value of the current URL path; 2. An array consisting of all the values of the query vars; 3. A boolean indicating whether or not the current user has read permissions.

• Filter hooks are good for modifying data because they allow you to make changes without affecting anything else on the site. They’re also useful when you want to apply different rules depending on what page you’re currently viewing.

Callbacks

• Callback functions are useful for manipulating how data is displayed. For example, you could create a callback function to display a specific message after every post link on your site.

• Because callbacks are limited to working with the data associated with a single post type, you don’t have access to the global variables available to filters.

• You cannot pass more than one argument to a callback function.

If you’re still unsure about whether you need a filter hook or a callback function, here’s another guideline:

Conclusion

The difference between a filter hook and a callback function is subtle but important. While both can be used to alter how data is presented, a filter hook allows you to change data while leaving everything else unchanged. In contrast, a callback function lets you only work with the data associated with one particular post type, making it perfect for adding classes to links, hiding certain elements, etc.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join

Work With Me