الجمعة، 29 مارس 2013

Shortcodes for your WordPress – how to insert in the post any php code or function

Shortcodes for your WordPress – how to insert in the post any php code or function 

 

Hello, friends. Today I want to tell you about one chip, which recently applied himself on his blog. Let us create something such as their shortcodes that can be used directly in the posts on the blog.wp-images
With the help of these codes you can display any content, dynamic or static – it does not matter, the main thing that this case can be added to any place in writing the post, and later make changes only in one place and not have to edit a bunch of posts.
Let’s just show you live examples to make it clear what is at stake. Here are a couple links to my modules for DLE: LinkEnso PRO, TagFace and CatFace.
What do these three different pages? Then, of course, that there is a question of the modules and DLE, in all these pages there’s such a wonderful unit with images and links:
Example block output shortcode
This block is nothing like a normal html when you view the source code, and is no different from a simple content inserted in the writing position. All true, except that the output is all that stuff with a special code, which I have come up with and put in the post to a convenient location. And to me something to correct or add to this block, I have to edit only one function in the file functions.php.
I think this is kapets as comfortable and I simply must share this topic with you. I’m not the first, and I thought of this, regular expressions come up before I found out what the Internet :)
Write a function and create a shortcode in WordPress
Decided not to roll the long wool, and immediately set to work. So we go into the editor appearance of his skin and find there the file functions.php. Below I will give an example of my own function to generate what you see in the screenshot above.
/*** List the modules anywhere fast ***/
    function my_modules() {
        return '<div style="margin:10px 180px;"><table cellpadding="0" cellspacing="0" border="0"><tr><th colspan="5" style="padding-bottom:10px;">Команда АлаичЪ\'а трудится непокладая рук и вот что у нас есть для вас:</th>
    </tr><tr>
        <td><img src="http://yasserdz.com/wp-content/uploads/2011/03/sitemap.png" alt="HTML SiteMap" title="HTML SiteMap" width="100" height="100"></td>
        <td><img src="http://yasserdz.com/wp-content/uploads/2011/05/catface.png" alt="CatFace" title="CatFace" width="100" height="100"></td>
        <td><img src="http://yasserdz.com/wp-content/uploads/2011/11/snvote-v.1.1.png" alt="SNVote" title="SNVote" width="100" height="100"></td>
        <td><img src="http://yasserdz.com/wp-content/uploads/2012/03/linkenso.png" alt="LinkEnso" title="LinkEnso" width="100" height="100"></td>
        <td><img src="http://yasserdz.com/wp-content/uploads/2012/06/tagface.png" alt="TagFace" title="TagFace" width="100" height="100"></td>
    </tr><tr>
        <th><a href="http://yasserdz.com/post/1974">SiteMap</a></th>
        <th><a href="http://yasserdz.com/post/2086">CatFace</a></th>
        <th><a href="http://yasserdz.com/post/2435">SNVote Free</a><br /><a href="http://yasserdz.com/post/2836">SNVote PRO</a></th>
        <th><a href="http://yasserdz.com/post/3322">LinkEnso Free</a><br /><a href="http://yasserdz.com/post/3982">LinkEnso PRO</a></th>
        <th><a href="http://yasserdz.com/post-name.html">TagFace</a></th>
    </tr></table></div>';
    }
    add_shortcode('DLEmod', 'my_modules');
Bit analyze what is written. For some, it is quite incomprehensible gibberish :)
my_modules – is the name of the function, I made it up myself, you can use any name here, what your heart desires.
return ‘…’; – proper html-content that we want to bring in a post on the site of the shortcode, ie our block. It is important to know that if somewhere in your content inside will return a single quote, as I have “Alaich \ ‘a’, then they must be escaped with a backslash, in the example it shows.
add_shortcode (‘DLEmod’, ‘my_modules’); – a feature that will create for us a shortcode. This uses the WordPress Shortcode API, contains a set of functions to work with the regular season and not just … well, it’s not important to you. In general, the use of this command will create for us a shortcode named [DLEmod] (with no spaces between the name and the brackets) and will be displayed on its site content are my_modules.
My example is a very simple case, using no dynamics, but you can use any script in php. If you are interested, you can naguglit, I just wanted to tell you about the possibility of a little easier on yourself.
If you want, in the future I will consider the possibility of working directly with regular expressions without using WordPress API.
Use of health, write comments and ask questions. Thank you!

 

ليست هناك تعليقات:

إرسال تعليق