⬆️ ⬇️

WordPress shortcuts

Today we will talk about abbreviations that were introduced in WordPress 2.5 and for some reason the Russians do not write about this at all.



But so many people use WordPress, but WordPress abbreviations are unpopular with us.



What is WordPress shortcuts?



')

This is when you typed for example adsense and everything in this place you will have your adsense code. Conveniently?



Still would. Go. 8) Ready to start using wordpress cuts?



The use is very simple, start a new post, go to HTML mode and write:



[showcase]



Attributes can be used in abbreviations.



[showcase id="5"]



You can include content in the abbreviation:



[url href="http://www.smashingmagazine.com"]Smashing Magazine[/url]



Abbreviations work so that after sending a post, your post is parsed and abbreviations using the Shortcode API are converted into what was intended.

Create a simple wordpress cut



Let's start with HelloWorld.



Abbreviations are easily created using PHP, but you don’t need to be scared, nothing complicated.



1. Open function.php in your theme, if there is no file, create it.

2. Write a function returning HelloWorldfunction

hello() {

return 'Hello, World!';

}


3. Now assign the function to the abbreviation like this: add_shortcode('hw', 'hello'); The first parameter is the name of the abbreviation, the second is the name of the function.

4. Now the abbreviation has been created and we can use it to the fullest in posts and anywhere on the pages simply by writing: [hw]



Of course, this is a simple WordPress abbreviation, but this is only the beginning, isn't it?

Making WordPress more abbreviated



Let's see how we use attributes. Create an abbreviation of the url function myUrl:



add_shortcode("url", "myUrl");



Function:



function myUrl($atts, $content = null) {

extract(shortcode_atts(array(

"href" => 'http://'

), $atts));

return ''.$content.'';

}


The abbreviation is created ... now we can write like this:



[url href="http://ajaxed.ru"]Ajax - [/url]



As a result, it turns out that the 'Ajax - mod' link pointing to ajaxed.ru will be written



What happened in the function?



The function takes 2 parameters: attr and content. attr is an abbreviation attribute. We have an attribute called href and contained a link. content is content enclosed within an abbreviation tag. We can also specify a default value.



Creating a Post on Twitter





Without going into the Twitter API, I’ll immediately show that everything is very simple:



function twitt() {

return '-';

}

add_shortcode('twitter', 'twitt');




To use a feature you need to write in a post:



[twitter]



And you will have a link by clicking which, your post will fall into Twitter

Subscribe to RSS



Similarly, everything is very simple and fast:



function subscribeRss() {

return 'Enjoyed this post? Subscribe to my RSS feeds!';

}



add_shortcode('subscribe', 'subscribeRss');




To color the rss-box to the heap:



.rss-box{

background:#F2F8F2;

border:2px #D5E9D5 solid;

font-weight:bold;

padding:10px;

}




Ponder wherever you can Adsense reduction





Absolutely nothing is difficult, you need to return a static piece of code issued by Google:



function showads() {

return '



<script type="text/javascript"

src="http://127.0.0.1/images/ajaxed/http://pagead2.googlesyndication.com/pagead/show_ads.js">

';

}



add_shortcode ('adsense', 'showads');



Now just write [adsense] and there is advertising



I could still tell a few examples, but they are a bit far-fetched, if anyone is interested, be sure to read the original:



Smashingmagazine



The text is in English, there are links to resources on WordPress abbreviations.



Now tell your friends about the abbreviations and you pour a little more on WordPress.

Author ajaxed.ru

Source: https://habr.com/ru/post/51027/



All Articles