Practice on developing a full-fledged shortcode – lesson 31

For an example of developing a shortcode and fixing the material covered on WordPress, we will take a collection of effects for links Creative Link Effects and develop our own shortcode for it in the future premium theme.

The work of the style collection is to, to include styles and arrange classes, where needed. Here is an example of a customized structure for one effect:

add class creative link effect

In general, the set offers 21 website link style. A demonstration of how different link styles work can be see here.

Embedding Styles in WordPress and Adding a Shortcode

To get started, download the ready-made collection style file from my the site.

In my archive, there are already slightly prepared styles for our template.

Go to the downloaded archive and transfer the folder creative-link-effects at the root of our topic.

link effect wordpress upload folder

Next, we need to connect the style file to our theme., to do this, go to the file /wp-content/themes/my_theme_tmp/functions/custom-header-script.php and add connection code.

add class link wordpress

So that the code can be copied to my file, I posted an additional fragment.

After connecting the style file, we need to create a new shortcode in the file created in the previous lessons:

wp-content/themes/my_theme_tmp/functions/shortcode/shortcode.php

Since the lesson is to consolidate knowledge, I think it would be right to lay out the finished code and tell how to use it.

zip wordpress theme archive

Consider the shortcode itself and the code snippet:

In the shortcode, you can specify which effect you are interested in effect=”1″ and specify links and texts for links (anchors) separated by commas. I pay attention to, that the number of links and their anchors should be the same. You can also specify an attribute target=”blank” if you want the link to open in a new window.

Consider the code of the shortcode itself:

At the beginning, we define all attributes as variables and form arrays from some. In this part, we also do various checks to exclude situations when the user did not enter the required parameters or indicated the wrong number of them.. Exceptions will display an error message instead of links. If you have already developed php, then it will not be difficult for you to read this code.

Choosing a shortcode effect and generating an output template

Next, we check if the effect number is correct., which should be from 1 before 21 “ if($effect>0 && $effect<22 ) { “. After checking with the conditional construction “switch case” we make the program check the variable $effect and select the desired block of code. The construct finds the desired block of code for the specified effect and executes it.

In a block of executable code:

into a variable $res we put a piece of code <nav class=”cl-effect-1″ id=”cl-effect-1″> -> check if the variable is $links mass -> using a cycle foreach iterate through all the elements of the array and dock (concatenation .=) them into a variable $res -> close the tag </nav> -> return a variable with return $res, all the contents of the variable will be displayed on the site page. The same can be done for other effects., note the pattern structure of different effects is different, so be careful.

Here's how it turned out to display different shortcodes on my test site:

create shortcodes wordpress lesson 31

If you didn't succeed, then I would advise you to just copy my code from the file shortcode.php and trace line by line what happens there. You can also read the previous lesson “Creating shortcodes - lesson 30“. Good luck developing the right website templates, according to the rules of the WordPress API.

The code of the entire developed template at this stage of training

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