How to create wordpress shortcode? – Darko Gjorgjijoski

This article is about creating WordPress shortcode. If you want to get started you need to have at least some PHP knowledge

This is my first post and from now and so on i will use the blog for simple tutorials to teach you about web development, web design. In this post i will show you how to make your own custom shortcodes.

Shortcodes are some kind of code in wordpress that allow you to display something in easier way without custom fields, editing the template or adding bunch of html. Today i will show you what how to develop your wordpress shortcodes. So, let’s say you want to show your five recent posts with shortocde like this [recentposts number=”5″].

1. You need to create function that will be your shortcode. In the function you need to specify the attributes of the shortocde in this case they are like this

PHP functions have names and attributes(not the same as below, this is php function attributes and below are shortcode attributes.). The function take two attributes $atts and $content.

Realized what is the function name? Now we continue to develop the function. First we create array for the shrotcode attributes and the array have one attribute called number. The default value of number is 3. You can set it or leave like ”.

Then we need to create custom WordPress query. To query the recent posts ordered by date. We do it like this:

After we created the query, we should use it to call the posts.

Now we need to add action to shortcodes and register your shortocde with the following:

Congratulations! That’s all. You created your shortcode. If you don’t know what loops mean in php and wordpress. Please read here. Here is the full function that we developed.

To call the shortocode from your content editor. Simply use the following:

*You can change the style with css. I used html lists to display the recent posts. Feel free to style them or use different html elements.

Fonte: How to create wordpress shortcode? – Darko Gjorgjijoski

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão /  Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão /  Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão /  Alterar )

Connecting to %s