Display codes on the front end without being executed by Edit Post

Asked

Viewed 200 times

2

I need my front present codes and scripts without them being executed when creating POSTS. Is there a plugin for this? I used <pre></pre>, but still HTML codes are executed.

See in the image below in the front. There is a green board as the HTML has been executed, I don’t want that to happen. And further down the Javascript code, which presents correctly as it should be:

  • Pro HTML, you do not escape from having to escape the characters. You can do this manually (which will be a mess), or use something ready to make the escape for you, and then you glue the result in the post and send bullet. Another solution would be to use a wp filter to do this for you (I will study this option further). A third way is to use something like Prism.js http://prismjs.com/

1 answer

2


The simplest is to use one Custom Field to paste the code and use a Shortcode to make the display in pure HTML.

The post would look like this:

You can use as many shortcodes as you want, just put a unique name to the field name, in my case cod-1 (an arbitrary name).

The Custom Field:

Plugin code:
You’ll always see who says "paste this into the functions.php file of Theme";
it’s wrong, make your own plugin, no problem that has 20 lines.

<?php
/**
 * Plugin Name: (SOpt) Shortcode para código HTML
 * Plugin URI: /a/86941/201
 * Version: 1.0
 * Author: brasofilo 
 */

add_shortcode( 'codigo', 'shortcode_sopt_83496' );

function shortcode_sopt_83496( $atts )
{
    global $post;

    // field não definido, não faz nada
    if ( !isset( $atts['field'] ) )
        return '';

    // pega valor do custom field no atributo "field" do shortcode
    $code = get_post_meta( $post->ID, $atts['field'], true );

    // envelopa o código na tag <pre>, estilizar a classe com CSS
    return sprintf(
        '<pre class="codigo-front">%s</pre>', 
        htmlentities( $code, ENT_QUOTES )
    );
}

Upshot:


You can use a plugin like Advanced Custom Fields to interface to Cfs instead of the default WP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.