views:

19

answers:

1

Hi

I'm trying to display code snippets entered into my custom field. An example code snippet entered into custom field - snippet-1

<?php if (($wp_query->current_post + 1) < ($wp_query->post_count)) { echo '<div class="post-item-divider">Post Divider</div>'; } ?>

If I try to display this code wrapped in <pre></pre> tags in my page template like

<?php if ( get_post_meta($post->ID, 'snippet-1', true) ) : ?> <pre><?php echo get_post_meta($post->ID, 'snippet-1', true) ?></pre> <?php endif; ?>

but it returns nothing to the the template. I understand WordPress is filtering the snippet out as it sees it as PHP code to execute, is their a way just to print this out on the page as a code snippet?

Many thanks in advance

rob

A: 

Use htmlspecialchars() to escape your code.

Update

echo htmlspecialchars(get_post_meta($post->ID, 'snippet-1', true));
toscho
Thankyou. How would I go about applying htmlspecialchars() to `<?php echo get_post_meta($post->ID, 'snippet-1', true) ?>`. Many thanks rob
Rob Oliver
I’ve added an update to my answer.
toscho