Shortcode for button with custom style

I have already added an link in my widget
eg:

 <a target="_blank" href="google.com" class="a-button">Learn More</a>

I need an shortcode like this

Read More
[button link="google.com" value="Learn More"]

If i paste this shortcode in widget, page and post, link must appear

Style must be same as above tag

Current Code:

function button_shortcode($atts, $content = null) {
    extract( shortcode_atts( array( 'url' => '#' ), $atts ) );
    return '<a href="'.$url.'" class="a-button">' . do_shortcode($content) . '</a>';
}
add_shortcode('button', 'button_shortcode');

How can i do this ?

Related posts

Leave a Reply

3 comments

  1. Basic shortcode will look like this:

    function a_button_shortcode( $atts, $content = null ) {
       extract($atts);
       return '<a target="_blank" href="' . esc_attr($link) . '" class="a-button">' . esc_attr($value) .   '</a>';
    }
    add_shortcode( 'button', 'a_button_shortcode' );
    

    You can read more on shortcode api on: http://codex.wordpress.org/Shortcode_API

    In order to make your widget with shortcodes should use do_shortcode( $content ) function inside update method of your widget.

    Like this:

    function update( $old_instance, $new_instance) {
       $new_instance['content'] = do_shortcode($new_instance['content']);
       return $new_instance;
    }
    

    Or use a plugin that will make it for default widgets like this https://wordpress.org/plugins/shortcodes-in-sidebar-widgets/

  2. add_shortcode("init" ,"add_custom_shortcode");
    
    function add_custom_shortcode()
    {
         add_shortcode('button', 'buttonShortcode');
    }
    
    function buttonShortcode($atts, $text='') {
        extract( shortcode_atts( array( 'url' => '#' ), $atts ) );
        return '<a href="'.$url.'" class="a-button">' . do_shortcode($text) . '</a>';
    }
    
  3. thank you all, i tried out this one

    function button_shortcode($atts, $content = null) {
     extract( shortcode_atts( array(
              'url' => '#'
    ), $atts ) );
    return '<a href="'.$url.'" class="a-button">' . do_shortcode($content) . '</a>';
    }
    add_shortcode('button', 'button_shortcode');
    

    For shortcode support in widget paste below line in functions.php

    add_filter('widget_text', 'do_shortcode');
    

    created shortcode:

    [button url="google.com"]Download[/button]