Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to display XML in Wordpress post

If you want to display an XML fragment in a WordPress post - how do you do that?

Suppose you want to display this:

<family>
    <dad>whatever</dad>
    <mom>whatever</mom>
</family>

Using the [sourcecode language="xml"] does not help, as it mangles up the XML. Also, I believe, plugins cannot be used with free version of WordPress ( i.e wordpress.com ) - so, probably that won't be an option.

Using HTML <pre> tag works, but it does not give correct look and feel. Can any one show me how they have done it?

like image 770
Jasper Avatar asked Feb 15 '13 14:02

Jasper


1 Answers

The wordpress codex has a whole page about writing code in your posts.

http://codex.wordpress.org/Writing_Code_in_Your_Posts

Their suggestion is to use html entity codes within <pre> or <code> tags

&lt;family&gt;
    &lt;dad&gt;whatever&lt;/dad&gt;
    &lt;mom&gt;whatever&lt;/mom&gt;
&lt;/family&gt;

This could be tedious if you have a lot of code to show in your post. I suggest writing a small shortcode that would do this for you.

<?php
function xml_shortcode( $atts, $content ) {
    return '<pre>' . htmlentities( $content ) . '</pre>';
}

add_shortcode( 'xml', `xml_shortcode` );
?>

How to use your shortcode in a post

[xml]your code here[/xml]
like image 166
Jrod Avatar answered Sep 28 '22 07:09

Jrod