Loading 0

My Blog

Scroll Down

Programming in WordPress and creating Plugins (by Nicolas Lagios)

Programming in WordPress and creating Plugins (by Nicolas Lagios)

Hello, this article has been posted a bit strangely because it was transferred from Word to a WordPress article. Will fix its design in time, when the WordPress Rest Api information is added.

Author of the article:

Nikolaos Lagios - H/Y & Mobile Website and Application Developer.

The article is the copyright of the author and any reproduction on other websites or media without the author's consent is prohibited.

The article is registered and protected by proof services


Programming in WordPress and creating Plugins

Let's start with the simple ones.


What is a programming language?

A programming language is the language with which humans can communicate and give commands in machine language (to the computer).

Because the PC understands binary code, 0 and 1 (i.e. there is no current '0' and there is current '1'), we should talk to it like that, because it is too difficult to write in binary code to communicate with the computer, because this requires an enormous amount of time and concentration in order not to make mistakes, other techniques have been invented. But let's look at the word good morning binary code for example:

11001110 10011010 11001110 10110001 11001110 10111011 11001110 10110111 11001110 10111100 11001110 10101101 11001111 10000001 11001110 10110001


Man therefore invented other systems more comprehensible to him where he can write commands and then the compiler (some mechanism) undertakes to compile these commands into machine language for the computer to see and vice versa.


If you deal with WordPress, you should already know that it also consists of some programming language.

You can refer below to the section "How we use them in WordPress" to understand more (from the sources) about programming languages.

If you don't know any programming languages, it's very easy to learn within 2 weeks for each of the Links I list.


What are functions?

Functions, or otherwise functions, are pieces of code that undertake to carry out a task that we want, without having to write new code every time when we want to deal with new data.


When do we use them?

Basically all the time.

WordPress is many pieces of such code, which run all the time of our browsing, either when we see the site from the front or from the back.

Add-ons, themes, etc. of wordpress also are new pieces of code that we install.


How can I make a function?

Functions if you have even a little knowledge of a programming language or math (for the meaning of their purpose) are used so that you don't have to go crazy every time and change a text or value to something that the visitor sees . That is, if you have 3 numbers and you want to multiply them by 2, you don't need to write 1*2 and 8*2 and 14*2 crazy pieces of code. You simply create a function like in the following example:

function pollaplasiasmos($number) {

$result = $number * 2;

return $result;


So when you say in your code: pollaplasiasmos(14); essentially you call the function pollaplasiasmos for the number 14 (you give it as an argument i.e. 14) and the result will be 28.


How do we use them in WordPress?

First let's say that WordPress is a CMS (Content Management System) platform like Droopal, Joomla and other platforms.

WordPress consists of

Now when you just want to change the appearance or behavior of some wordpress elements, you can do it very simply with html, css, javascript by writing your own pieces of code and either you pass these pieces to the files of the theme you use to run, or you use a plugin that allows you to run these tracks, such as the plugin: https://el.wordpress.org/plugins/custom-css-js/ (there are also plugins that allow you to run a piece of code only on a page or part of the site and not on the entire site).


When you want to deal with the functionality of the site (basic functions and data), etc., you use the PHP language (and if necessary, SQL when a database is involved in what you want to build) and you can put your functions either in theme you use (functions.php file), or using a plugin, such as: https://el.wordpress.org/plugins/my-custom-functions/ , or write your own plugin, which you and others can easily install on wordpress.


Specialty in Custom Functions (functions)

Unfortunately or fortunately, when you haven't written a website from scratch, but use a ready-made platform, such as WordPress, there are some peculiarities to make your own personal (custom) functions work.

You should use hooks, where they hook into the system and the system can see and use them.


What I need before proceeding below:

The following are recommended:


What are hooks?

Hooks in the general sense in php are a type of functions that you can hook into an existing system to extend its capabilities.

They are also called Plugins, add-ons, extensions.

The difference is that with the name hooks or simply function or script it usually refers to something small that you pass as code and not as a plug and play complete solution (installable package) to call it a plugin or addon or extension.


How are these hooks (or better functions) implemented in WordPress?

When you write your own function you can implement it in two ways

  • Either using the "add_action" function
  • Either by using the "add_filter" function


What are Actions and Filters in WordPress?

These are also functions, where they can be parameterized either by Plugins or by Themes.


What are Actions?

They are functions (or add_filter function) where they are only executed when a specific event occurs in WordPress. Only when it happens.

We have two types of Action:

  • add_action -> We use it when we want to run code while something else is happening in WordPress

In the example we saw, we used:

The above example assumes that when you press the save button on a post, it writes to a file created by us 'katagrafh_post.txt' which is inside the theme we use, adds a line with: 'Post Title' saved just now

But so that it doesn't save 3 times to the file (because it also saves the autosave, but also previous post versions), we use an if at the beginning. Immediately below (the next if) is general php to open the txt file and save in.

  • do_action -> We use it when we want to create our own custom actions.

Here we see that there is a code where if a user goes to a secret page, it does not let him see it if he is not logged in and redirects him to the home page of the site.

The line with yellow color, however, takes over this whole piece to create it in a new action of ours.

It could not exist and the code would still work, but we would have to rewrite it for other cases.

Now we can simply call the new action below:

add_action(‘user_redirected’, ‘new function’);

Result after that:

Whoever goes to the address http://www.dieyhtynsh.com/mystikh_selida and is not logged in, it immediately returns him to the home page http://www.dieythynsh.com


What are Filters?

It's functions (or add_action function) where it allows you to parameterize an existing function. Filters, in contrast to actions, allow you to change the data before it is even sent to the database and the browser, which is why they are universal in modification. That is, a function is hooked and changes the behavior forever, while in action it does so temporarily only when an action occurs.

Simply put they configure WordPress data before it is saved and executed.

Example 1 (we will change the wording of a post's short description blog), (no colors will be used, since it is already understood what is what):

In the example we saw, we used:


Example 2: We will now see how Actions and Filters are combined and how you actually create your own filter. We will do this by using and changing the 1st example from the actions in collaboration with a new filter.

What did we see in the result? That both the action function and the filter function worked together in cooperation and created this line that we read.

What was the meaning of the above example?

Since the action function is now considered an asset of our site (as if it already existed), we go with filter action and change it (as we said at the beginning for every other wordpress function. That is, we configure the wordpress data before it is saved and executed. We did the same here. It doesn't matter if we did it in an action we wrote. Let's say it already existed.)


Example 3

Here we will see how we configure WordPress plugins and functions with filters:

Let's look at the yoast seo add-on for example, which also has its own actions and filters: http://hookr.io/plugins/yoast-seo/4.6/filters/#index=a or https://developer.yoast.com/

If you look at the code of a page (let's say the front page), you will see that yoast seo adds some meta tags to the head of the html and also adds a very small script tag named 'application/ld+json' with some javascript commands easily readable, right below the meta tags.

Let's say you don't want the json potential action key that goes in there (which basically tells google and other search engines that search is allowed on your site) and you want to remove it.

Either you'll do it the baccalistic way, finding the responsible file and deleting what you don't want.

You will either use hook action or filter depending on what is there and remove/stop it.

The filter responsible for this is 'wpseo_json_ld_outpout'.

So first let's see what the json looks like:

So we're going to change that information before it's even printed on the screen.

(see changes in comments in green and result in yellow fill)

As we have seen we have changed the name of the name key in the json from 'Websites' to 'Vangelis' and also all the potentialAction key has disappeared.


So once we are sure, we change the final function to:

this so that our page will return normally (so that we can see it) and the changes will be made at the code level.

If we inspect the code we will see that the changes were made normally.


Additional explanation for the Filters:

Explanation Code Result
If you run the following by itself
If we now want to make this piece of data extensible and available to other pieces of our application, we will use a filter on the new filter (as we saw in the previous examples).

Same result

And now let's bring something extra to fruition
Let's now put in our apply_filters and 3rd argument and in the 2nd argument of our filter which is a pinned function, add inside this function and 2nd argument, we will have the following:

Error because there are rules we didn't follow.

If we have installed the prerequisites mentioned above (vs code , extension , xampp), we can easily click on the add_filter function and see its requirements in arguments, if not, we can Link it with the filters we listed above and see more about her. With a check done, I found that it needs changes in terms of arguments:

So based on the arguments we saw, we find that the problem is in accepted_args which concerns how many arguments the function we put in our filter can take. Since we've given it two arguments and haven't put in the optional filter arguments to say we're going to change 1 to 2, that's where the problem comes in.

So we'll do that, pass all the arguments to add_filter and make the necessary change.

Now let's try to put the same Filter further down, let's say the first one is of the Plugin and the 2nd of the theme. What will happen is that the 2nd doesn't need two arguments to the function because what it pulls from the 1st is like one argument, like a line of text. So the 2nd filter, as a clone of the 1st, takes the text of the 1st and adds whatever else we want to add that happens in the 2nd.
Now if we put all the arguments in the 2nd filter and change the priority argument we will have the following:



Run the 2nd filter (clone) first and then the other. But because the 2nd was taking text from the 1st, the paradox we saw was created, which is explained in the following priority image:

This is because the main filter has two arguments in it. So in this case, the clone keeps only the 1st argument from the main filter.

Now, if we give a higher priority to the 1st filter and a lower priority to the 2nd one (ie the second one will be the last one to run, so it will override the 1st one) and change the text to something completely different, the result will be:  

The latter prevailed.

Now if we make a 2nd clone, i.e. a 3rd same filter and prioritize it last over all, the following will happen:




Creating Plugin for WordPress

And the time has come for all this tiring time we wasted to write infinite lines of code, to compile it into our own Plugin and if we want to sell it or make it available for free open source and always with the help of our developer friends on github, in order to make our Plugin even more powerful.


First we create a folder with the name of our Plugin, which will contain the following:


and will have the following code inside:

In yellow are the elements of our plugin, so that wordpress can recognize it as a Plugin. We change them as we like.


This article continues shortly to complete the general presentation of wordpress programming. We will introduce the WordPress REST API soon after

This post is also available in: Ελληνικά

Leave a Reply

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


Here you can see all the services I provide

Registration and management of domain names (website address such as www.nicolaslagios.com)

Also management of dns records (e.g. connecting the domain to a specific server, fixing email spam problems, etc.)

Also ssl renewals etc

Installation and management of web & mail server in ubuntu vps with virtualmin, plesk, cpanel

Also studying and fixing server problems.

Necessary condition, the target server meets the conditions

At the moment for new wordpress websites you can choose from ready-made themes and we change the content (no custom changes). You can buy with a fixed price by clicking here!

My team and I undertake any data bridging implementation for Wordpress, Prestashop, Opencart, Joomla platforms.

We can connect data from any source, as long as the structure is stable and there is proper documentation and briefing.

We undertake the creation, regulation and enrichment of pages for social networks: Facebook, Linkedin, Instagram (profile), Twitter (profile), Tiktok (profile).

We also undertake the first boost of your pages for quick results in followers.

We undertake the repair and maintenance of your existing wordpress website.

For more information about the services, you can read the following and return here to schedule a meeting with me: https://maxservices.gr/en/internet-services/website-services-blank/additional-website-services/