What is Wordpress shortcode, how to use shortcode ?

Kenan YAMAN
3 min readJul 20, 2021

--

Hello everyone, you know that wordpress is a personal publishing system licensed in GPL, written using php and mysql. Ready blog script. It actually allows you to make a very different website not only for the blog. I want to tell you a beautiful feature of wordpress today and reinforce the subject with a few examples. Let’s get started.

What is Wordpress shortcode ?

It is a system that allows us to call php functions that we write to fonction.php in Wordpress, with the short codes we define where we want it. For example, google adsense will type and create ads you publish on your site with a short code that you will create, and you can call the ad on any page or anywhere on your website to make it appear where it was made. you can.

It’s hard to understand in writing, let’s look at some codes.

How to create Wordpress shortcode ?

We used google adsense as an example, so let’s continue with the google adsense example.

First, open the functions.php file of your theme.

You can do this from the 'View --> theme editor' section under the wordpress management panel, or you can also edit by connecting to the server with ftp and downloading the file.

After you open functions.php, type the following code at the bottom of the page:

function google_adsense_yazi_ici_link_reklamlari() {
return '<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Site içi link reklamları -->
<ins class="adsbygoogle" class="adsbygoogle"></ins class="adsbygoogle">
style="display:block"
data-ad-client="ca-pub-1535235970479431"
data-ad-slot="2934730818"
data-ad-format="link"
data-full-width-responsive="true">
<script></script>
(adsbygoogle = window.adsbygoogle || []). push({});
'; }
add_shortcode ('adsense_link', 'google_adsense_yazi_ici_link_reklamlari');

[adsense_link]

Let’s explain this code. First of all, we started with function and identified a function called google_adsense_yazi_ici_link_reklamlari(). I gave that name because I just want to run link ads. You can duplicate and use it with a name you want as much as you want. Among the fancy brackets, we wrote the code for these link ads that I created with Google Adsense.

Before I forget, google adsense codes written in this section belong to me, so you need to replace them with your own codes.

Then we started with add_shortcode and wrote adsense_link and google_adsense_yazi_ici_link_reklamlari writings between parentheses with quotation marks. The first adsense_link we wrote will be our shortcode name, and the second is the name we define for the adsense code. I mean, the function name.

Now create a post or point to the widget insertion screen and switch to the html section. As Shortcode you will write thi[adsense_link]s where you type google adsense link ads will appear.

Did this sample come a little complicated? Then let’s make an easy example.

I get the same structure, remove the google adsense code in between and write ‘Hello World’. Now I’ll define this po[merhaba]st as shortcode. Now let’s call this on our page and try it.

function hello() {
return 'hello world'; }
add_shortcode ('hello', 'hello');

[merhaba]

Now let’s write this code in the html code section and try it.

These examples can be replicated. That’s how Wordpress shortcode logic works.

We define a function and use that function on a page we want with a short code.

This article was first published on the website www.kenanyaman.com on 16.01.2020

--

--