Creating shortcodes – lesson 30

There are several easy ways to create shortcodes, the easiest way is to use special plugins like Shortcode Exec PHP (now this plugin has found a vulnerability and had to abandon it), you can of course use a lot of other. Since our task is to make a theme that a simple person can install and configure with minimal knowledge, without installing additional plugins, then we will not use plugins to create shortcodes. Let's start learning the Shortcode API.

Shortcode API in WordPress in your own words

Shortcodes can be written both in plugins and in the functions.php file of the theme we are creating., as we create our own theme, then we will use the second method. For readability of the code, we will move it to a separate file..

Let's go to the file functions.php and include another file called shortcode.php where our shortcodes will be stored.

require(‘functions/shortcode/shortcode.php’);

Let's create a file in the right place.

Подключение шорткодов в wordpress

Example 1
First you need to understand how shortcodes work., for this we will make a simple code and place it in shortcode.php add the function code add_shortcode().

After adding this simple code, we can visit the page Shortcodes which we added in previous lessons and add our shortcode [multiplication var=”5″].

Let's look at the result of the function that multiplies the var variable by 2 on the site:

The result of processing the generated shortcode

 

It is important to understand, since you can’t write code in the text of articles in WordPress, it is for these purposes that shortcodes were invented. Shortcodes can perform complex functions, by setting just a few parameters.

Example 2

Let's continue learning with examples and complicate our example in a new shortcode and a new function. In the new function, we will multiply 2 variables one to another, what if the variables were not set, then substitute the default values.

Paste the new code below multiplication_func() in shortcode.php file

To test the new shortcode, in the content of any page of your site, insert 2 in the shortcode.

[multiplication_calc]

[multiplication_calc var=”2″ var2=”2″]

What happened:

Multiplication result (0 * 0) = 0

Multiplication result (2 * 2) = 4

Example 3

Using Paired Shortcodes in WordPress

To create a paired shortcode, let's add a simple example after the multiplication_calc_func function() in shortcode.php file.

To test the new shortcode, paste it in the content of any page on your site.

[wp-admin]wp-admin.com.ua – wordpress lessons[/wp-admin]

Essentially this example shows how to use, what is inside the paired shortcode on the output will be displayed just text. Try using the generated shortcode.

shortcode wordpress create

I hope the lesson was interesting and useful to you., in the next tutorial we will learn how to create more complex shortcodes. Good luck in developing convenient and high-quality sites.

Download the example file

zip wordpress theme archive


Buy WordPress hosting
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

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.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal