0

I want to add some new attributes into existing shortocode using some functions. What about below code,

function btn_shortcode( $atts, $content = null ) {
$a = shortcode_atts( array(
    'class' => 'button',
    'href'  =>  '#'
), $atts );

return '<a class="' . esc_attr($a['class']) . '" href="' . esc_attr($a['href']) . '">' . $content . '</a>';
}
add_shortcode( 'button', 'btn_shortcode' );

1 Answer 1

1

All you need to do is modify the $a-variable, for example, to add a target to this link, you would add it to the array making up the $a-var, and printing it later.

add_shortcode( 'button', 'btn_shortcode' );
function btn_shortcode($atts, $content = null) {
    $a = shortcode_atts(array(
        'class' => 'button',
        'href'  =>  '#',
        'target' => '_blank'
    ), $atts);

    return '<a target="' . esc_attr($a['target']) . '" class="' . esc_attr($a['class']) . '" href="' . esc_attr($a['href']) . '">' . $content . '</a>';
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.