0

I've currently been using a plugin called Insert PHP that's just laughably bad and the cause of a lot of headaches. I have a lot of PHP code running in different Pages on my installation, so I was wondering if there's a way to include the .php files and write in a normal text editor?

1
  • 7
    I like to create shortcodes. In my shortcode callbacl function I write all my php, then I insert the shortcode into my post/page Oct 1, 2015 at 19:55

1 Answer 1

2

Writing code in a WP post is not a good idea. As czerspalace mentions the easiest solution is to use a Custom Field to paste the code and use a Shortcode to render it in HTML.

The post would be like:

Any number of shortcodes can be used, just put a unique name for each field name,
in this example cod-1 (an arbitrary name).

The Custom Field:

Plugin code:
See Where do I put the code snippets I found here or somewhere else on the web?

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

add_shortcode( 'codigo', 'shortcode_sopt_83496' );

function shortcode_sopt_83496( $atts )
{
    global $post;

    // field not defined, do nothing
    if ( !isset( $atts['field'] ) )
        return '';

    // get custom field attribute value from Shortcode's "field"
    $code = get_post_meta( $post->ID, $atts['field'], true );

    // envelops code in <pre> tag, stilize at will with CSS
    return sprintf(
        '<pre class="codigo-front">%s</pre>', 
        htmlentities( $code, ENT_QUOTES )
    );
}

Result:

Any Custom Fields plugin can be used to make a nice interface, instead of WP default.

Originally posted at Stack Overflow em Português

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.