Custom shortcode in WordPress

Viewed 138

I am a bit stuck and would please like some help if possible:

I have this circle button link with two bits of text in that I wish to make a shortcode:

<a href="">
<div class="find-out-btn alignright">
<h3>Header Text</h3>
<p>Paragraph text</p>
</div>
</a>

But at the moment I cannot call the function to create it as an actual shortcode - any ideas where I am going wrong?

This is my php so far:

// Add Shortcode
function visit_info_shortcode( $atts , $content = null ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'link' => ‘',
            ’title’ => ‘',
            ’subtitle’ => ‘',
        ),
        $atts
    );

    $link = $atts['link'];
    $target = $atts['target’];
    $title = $atts['title’];
    $subtitle = $atts['subtitle’];

if ($link) {
        return '<a class="visitinfo" href="'. $link .'"><div class="find-out-btn alignright"><h3>' . $title . '</h3><p>' . $subtitle . '</p></div></a>';
    } else {
        '<a class="visitinfo" href="'. $link .'"><div class="find-out-btn alignright"><h3>"Visit"</h3><p>"Find out More"</p></div></a>';
    }
}

Any ideas how I link it up to WordPress?

Many thanks,

2 Answers

You need to call the add_shortcode function to set up the shortcode tag you want to use in the editor and link it to your shortcode callback function above, e.g.

add_shortcode( 'show_visit_info', 'visit_info_shortcode' );

You can add this just before or after your callback function in your functions.php (or plugin file) This will create the shortcode tag to use in the page editor as [show_visit_info]

Reference: WP documentation for add_shortcode

function bartag_func( $atts ) {
    $a = shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts );

    return "foo = {$a['foo']}";
}
//register
add_shortcode( 'bartag', 'bartag_func' );

echo do_shortcode('[bartag]'); //return foo = something

You should read documentation

Related