Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Wordpress ShortCodes Guide

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 131
    Comment on it

    Hello readers !
    Today we discuss about "WordPress Short Codes".

    Short-codes:- a short-code is a shortcut to put things simply. short-codes are WordPress-specific pieces of code that do things that would otherwise require technical knowledge or which is possibly large amounts of code and this result to allow you to save time. By adding short-codes to your themes or plugins, you can make using it so much easier and intuitive. For example, to display most popular post on your WordPress site type a short-code [most_popular_post] exactly same.
    If you do this manually then you need to write some HTML code, as well as have to change it manually and when the popularity of posts changed. A short-code takes away the need for this, and simplifies things for you.

    Short-codes created :- Short-codes are created using the WordPress short-code API. Many people do the job nearly the same as WordPress filtration system. You should determine a new handler functionality that will parses the short-code and also earnings several productivity. Afterward you register the short-code using the add_shortcut() functionality.

    The shortcut handler functionality takes nearly three arguments, they are as following:

    1. $atts: An array of any short-code attributes
    2. $content: Any content that the short-code may enclose
    3. $code: The short-code name (used usually when the same handler is used for multiple short-code

    Create your first short-code :- At this point that you've figured out the basic principles from the short-code API, you can go ahead and generate your own very first short-code. Here I have used TwentyEleven theme incorporated with WordPress, and screen-shots will probably mirror this kind of, however you can use virtually any theme of your decision, as well as produce a new plugins.

    To begin, open up the functions.php file of the theme, and add this line of code to the top:

    <?php     
            include 'shortcodes.php';
    

    Up coming, create the file that you simply referenced - shortcodes.php. For making issues less difficult, most our own short-codes is going to be within this data file. To start with, i will be making an easy to use short-code in which creates a web link in order to Twitter the article WEB ADDRESS. Create a function wptuts_first_shortcode() along with fill up it within on this signal:

    <?php 
    
    function wptuts_first_shortcode($atts, $content=null){ 
    
        $post_url = get_permalink($post->ID); 
        $post_title = get_the_title($post->ID); 
        $tweet = '<a href="http://twitter.com/home/?status=Read ' . $post_title . ' at ' . $post_url . '">Share on Twitter</a>'; 
    
        return $tweet; 
      } 
    
      add_shortcode('twitter', 'wptuts_first_shortcode'); 
    
    ?> 
    

    Your first shortcode is now ready to go! You can try it out by typing

    <?php echo  do_shortcode('[twitter]'); ?>
    

    --> into a post. Below an example of output:


    alt text

    Shortcode for youtube-

    <?php do_shortcode('[youtube id="_ggWarwhv9M" ]Check out this video![/youtube]'); ?>
    


    Now, paste the below code in shortcode.php for youtube embded shortcode.

    <?php
    function wptuts_youtube($atts, $content=null){
    
        extract(shortcode_atts( array('id' => ''), $atts));
    
        $return = $content;
        if($content)
            $return .= "<br /><br />";
    
        $return .= '<iframe width="560" height="349" src="http://www.youtube.com/embed/' . $id . '" frameborder="0" allowfullscreen></iframe>';
    
        return $return;
    }
    add_shortcode('youtube', 'wptuts_youtube');
    ?>
    


    alt text

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: