0
votes

For some reason, Wordpress removes most of the HTML markup from shortcode output while I need to use HTML markup inside those shortcodes.

There are a couple of suggestions on Stackoverflow but none of them worked for me. Let's take an example.

I call my shortcode in WP with a simple [myshortcode id="1"]

In my PHP, I use this:

function myshortcode($atts)
{
   ob_start();
   ?>
   <ul><li>test</li><li>test2</li></ul>
   <?PHP
   return ob_get_clean();
}
add_shortcode( 'myshortcode', 'myshortcode' );

The output I get is: test1test2 without the HTML markup

If I use links (www), they are preserved but div, span, ul, li, ... are systematically removed.

I have tried to add the markup in the end with the return statement but it doesn't work. I have tried what is suggested here https://codex.wordpress.org/Shortcode_API#Output but it doesn't work.

Do you have any idea?

Thanks!

Laurent

1

1 Answers

1
votes

This is working in a fresh install:

function myshortcode_53969195( $atts ) {

    // Attributes
    $atts = shortcode_atts(
      array(
        'id' => '1',
      ),
      $atts,
      'myshortcode'
    );

    $output = '<ul><li>test</li><li>' . $atts['id'] . '</li></ul>';
    return $output;


}
add_shortcode( 'myshortcode', 'myshortcode_53969195' );

Then you can render the shortcode on the content editor as [myshortcode id=XX] or in your template files echoing it:

echo do_shortcode('[myshortcode id=XX]');

If it continues removing the html tags check if you have any plugin or function that could modify the content output. Hope this helps.