0

I know the title is a bit odd (I couldn't think of a better one, so if a moderator would be kind enough to change it if needed), but I ran into a little problem while making a Wordpress theme.
I'm trying to make a short tag to post code snippets.
However, I ran into issues when using HTML code in the snippet itself.

This is the text I want to display (in this exact way):

HUEHUEHUE
<br />
<span>HELLEU</span>
<script>
console.log("hii");
console.log("test");
</script>

As you can see, I use <br /> and <script></script>.
Normally, these would be executed as HTML, so I used htmlentities() to prevent this However, it's currently being displayed like this in the code snippet:

<br />
HUEHUEHUE<br />
<br />
<span>HELLEU</span><br />
<script>
console.log("hii");
console.log("test");
</script><br />

I don't really know how to fix this issue by myself (I've searched around all night, and asked some friends, but they didn't know either).

here is the code I use in the functions.php:

function code_func($atts, $content = null){
    $a = shortcode_atts( array(
      'lang' => '',
      'code' => $content,
    ), $atts );
    $codeblock = "<pre class=\"line-numbers\"><code class=\"language-".$a['lang']."\">".htmlentities($a['code'])."</code></pre>";
    return $codeblock;
}
add_shortcode( 'code', 'code_func' );

And here is the code of the shorttag in the post:

[code lang="html"]
HUEHUEHUE
<br />
<span>HELLEU</span>
<script>
console.log("hii");
console.log("test");
</script>
[/code]

For those wondering: I'm using PrismJS (which is the the <pre> and <code> tags have classes)

I hope I've provided enough information to get some help with it :) I'd prefer to see the full working code, with explanation (as telling me "you should do this" can be confusing for me, and makes it diffucult to learn what to actually do)

Finlay Roelofs
  • 533
  • 6
  • 21

1 Answers1

0

After a long long search, I managed to find this answer, which made me able to fix the issue!

Community
  • 1
  • 1
Finlay Roelofs
  • 533
  • 6
  • 21