2

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,

1

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hi there thank you for that! I just followed a tutorial but it just seems to out put my function on the website and not the content! This is my functions.php: include( get_stylesheet_directory() . '/my_custom_shortcodes.php' ); add_shortcode( 'helloworld', 'torque_hello_world_shortcode' ); And this is the custom .php file function torque_hello_world_shortcode( $atts ) { $a = shortcode_atts( array( 'name' => 'world' ), $atts ); return 'Hello ' . $a['name'] . !'; } I just get that function printed out for some reason! am I doing something stupid?
@trailrunner89 Where do you have your function visit_info_shortcode( $atts , $content = null ) ? Add your add_shortcode directly before that.
@trailrunner89 ok, then add it there instead of your functions.php. It goes wherever your shortcode function is
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.