admin管理员组

文章数量:1391991

I'm trying to write a shortcode in function.php with a link to share the current page in Twitter. However this link contains some PHP in the href and I can't figure it out how to make it work. Here is the code:

add_shortcode ( "indice", "indice_output" );
function indice_output( $atts, $content="null" ) {

    extract( shortcode_atts( array(
        '' => ''
    ), $atts ));

    return '<div class="boxed max-width"><div class="indice-title"><span>' . $content . '</span><i class="fa fa-twitter" aria-hidden="true"></i></div></div>';

}

Thanks in advance for the help.

I'm trying to write a shortcode in function.php with a link to share the current page in Twitter. However this link contains some PHP in the href and I can't figure it out how to make it work. Here is the code:

add_shortcode ( "indice", "indice_output" );
function indice_output( $atts, $content="null" ) {

    extract( shortcode_atts( array(
        '' => ''
    ), $atts ));

    return '<div class="boxed max-width"><div class="indice-title"><span>' . $content . '</span><i class="fa fa-twitter" aria-hidden="true"></i></div></div>';

}

Thanks in advance for the help.

Share Improve this question edited Jul 19, 2017 at 19:07 Digvijayad 2911 silver badge7 bronze badges asked Jul 19, 2017 at 16:19 Mathieu PréaudMathieu Préaud 2035 silver badges18 bronze badges 3
  • 1 Can you add how you're using this shortcode? What are you putting between the shortcode tags? – dbeja Commented Jul 19, 2017 at 17:04
  • Thank you for responding! But Nuno Sarmento just found the answer with ob_start and ob_clean to print the HTML inside the shortcode. – Mathieu Préaud Commented Jul 20, 2017 at 7:56
  • What if I insert eval('Delete the entire WordPress installation'); into a link? Your website might have the potential to be hacked in 2 seconds. – Johansson Commented Jul 21, 2017 at 9:38
Add a comment  | 

1 Answer 1

Reset to default 2

You can use ob_start and ob_get_clean to print out your shortcode.

function shortcode_html()

     ob_start(); ?>

    <div class="boxed max-width">
        <div class="indice-title">
            <span><?php echo $content; ?></span>
            <i class="fa fa-twitter" aria-hidden="true"></i>
        </div>
    </div> <?php

    return ob_get_clean();
}
add_shortcode( 'print_shortcode', 'shortcode_html' );

本文标签: Shortcode return function with link href inside PHP