Shortcode as an value in wordpress

I have a button shortcode on my website which I will be using in all of my posts.
What I want is, If I modified the button value the changes should be applied to all the button on the website.So that I don’t have to modify each and every post.

I want to change the buttons Caption, Link and maybe rel

Read More

For example:

[button rel="[btn-rel]" url="[btn-url]"] [btn-cap] [/button] 

I googled the problem and added following code to Function.php to change the button caption.

function btn_cap() {  
    return 'More Freebies!';
}
add_shortcode('btn-cap', 'btn_cap');

and added the Shortcode to the button shortcode like this:

[button][btn-cap][/button]

Which didn’t worked out 🙁 The caption of the button is “[btn-cap]” not “More freebies”

I really want this thing to work as it will save me alot of time and work.

Thanks in advance…

Related posts

1 comment

  1. You cannot use shortcodes as variables/placeholders as in your example. You can however write your own button shortcode with the default values you require.

    Add the following to functions.php

    function freebie_button( $atts, $content = null) {
        // Default values
        $atts = shortcode_atts( array(
                'rel'   => 'nofollow',
                'url'   => '/',
                'cap'   => 'More Freebies!',
            ), $atts );
    
        $rel = 'rel="' . $atts['rel'] . '" ';
        // remove 'rel' attribute if it's empty
        if($rel === ''){
            $rel = '';
        }
    
        return '<a ' . $rel . 'href="' . $atts['url'] . '">' . $atts['cap'] . do_shortcode($content) . '</a>';
    }
    add_shortcode('freebutton', 'freebie_button');
    

    Uses

    [freebutton]
    [freebutton rel="" url="#" cap="Buy this"]
    [freebutton cap="Read "]More[/freebutton]
    

    Output

    <a rel="nofollow" href="/">More Freebies!</a>
    <a href="#">Buy this</a>
    <a rel="nofollow" href="/">Read More</a>
    

    Hopefully this code will get you started.

Comments are closed.