So here is my button shortcode in my widget.

 [button title="EDIT"  href="<?php ech edit_product_url( $post->ID ); ?>"]

As you can guess the href url does not work.

What would be the best way to add php in the shortcode?



2 Answers 2


To be honest, if you see yourself in the need of adding executable PHP code into a shortcode attribute, or in content editor in general, you are doing something wrong. You should think again about what, why and how do it. There is a high security risk accepting executable PHP code form user inputs.

Instead, you should pass the post ID in shortcode attributes and get the permalink when parsing the shortcode.

For example, the PHP function that parse the shortcode could be something like this:

add_shortcode( 'button' , 'render_button' );
function render_button( $atts, $content = null ) {

    $atts = shortcode_atts( array(
        'postID' => 0,
        'href'   => '',
        'title'  => ''
    ), $atts );

    //You may need sanitize title, not sure what your needs are
    //change this if you need
    $atts['title'] = sanitize_text_field( $atts['title'] );

    if( (int) $atts['postID'] > 0 ) {

        //If postID is set and greater thant zero, get post permalink for that ID
        //and override href
        $atts['href'] = edit_product_url( $atts['postID'] );
        //For standard posts uncomment next linke
        //$atts['href'] = get_permalink( $atts['postID'] );


    //Example output
    $output = '<a class="button" href="' . esc_url( $atts['href'] ) . '">' . $atts['title'] . '</a>';

    return $output;


Now you get the functionality without the need of include executable PHP in shortcode attributes. Also, you can use the render_button function directly if you need.

You can use the shortcode in content editor or wherever shortcodes are parsed, for example:

[button title="EDIT" postID="1542"]

You can also render buttons by direct call to the function:

 $button = render_button( array( 'postID' => 4578 ) );
 echo $button;

And if you need a specific href, you can use this shortcode:

 [button title="EDIT" href="http://www.example.com"]

Or with direct function call:

 $button = render_button( array( 'href' => 'http://www.example.com' ) );
 echo $button;

 //Or shorter
 echo render_button( array( 'href' => 'http://www.example.com' ) );
  • yeah, i figured there is no easy way doing it by just adding onto the shortcode. I guess I wanted to check and see. Well thanks for the info tho! =)
    – Steve Kim
    Mar 20, 2015 at 17:30
  • There is a easy way to do it, but not recommended at all in the case.
    – cybmeta
    Mar 20, 2015 at 17:38
  • Yeah I think I am just going to avoid putting any PHP in the short code. Thanks guys.
    – Steve Kim
    Mar 20, 2015 at 20:34
  • Added code example what I consider the best way to do what you are looking for.
    – cybmeta
    Mar 20, 2015 at 21:02

I would change this to the following

do_shortcode('[button title="EDIT" href="' . edit_product_url( $post->ID ) . '"]');
  • yeah, i guess I can directly add into code. Thanks!
    – Steve Kim
    Mar 20, 2015 at 17:41
  • The best way would actually be to call the underlying function for which the shortcode is a wrapper.
    – kaiser
    Mar 20, 2015 at 18:46
  • 1
    This is a solution, no doubt, but I also think is far way to be the best.
    – cybmeta
    Mar 20, 2015 at 21:14

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.