WpCode.net

Daily Wordpress Code Snippet

Tools to display code in your wordpress posts

| 0 comments

Pin It

By default you can’t write php or any kind of code directly in your post because wordpress will replace it with the relative ASCII code, so if someone try to copy and paste it, will do not works.

There are some solutions to display code in your posts. In this article i try to show you some of the best ways to display code in your posts without problems. Your users can copy and paste the code, and wordpress don’t execute the code.

WordPress:

First solution is delivered by wordpress core, to display code in your posts you can use the <code></code> tags. 

Example:

<Code>
function wpc_test() {
some function ecc ecc
}
</code>

But Using the <code></code> tag wordpress will replace < in “&lt;” so when you will paste the code in your php file you will copy &lt;?php ecc ecc.

To solve this trouble you may use also Wp_codeshield, is a plugin that disallow the overwriting of <?php in &lt;?php

Via Shortcode:

Adding this code to your function.php you can create a shortcode where you can paste the code.

function code_shortcode( $attr, $content = null ) {
        $content = clean_pre($content); // Clean pre-tags
        return '<pre"><code>' .
               str_replace('<', '<', $content) . // Escape < chars
               '</code></pre>';
}
add_shortcode('code', 'code_shortcode');

To use it in your articles you just need to put in your HTML editor:


function somethings() {

}


Via Plugins:

SyntaxHighlighter Evolved:

SyntaxHighligher is the plugin i’m using on my wpcode.net, it allow you to post code from different languages in the articles, you can use it switching the editor in the html mode and write a shortcode [languagename] where to write the code [/languagename].

In this way i can write wp php function, and you can copy it without errors.  You can use differents layout to publish code, customizations are available from the plugin’s option page.

Download from Wp Repo http://wordpress.org/extend/plugins/syntaxhighlighter

CodeColorer:

It’s another code publisher, i never used, but watching his popularity i think it’s valid such as SyntaxHighlighter.

It allows you to publish code function in your posts.

Wp Repo http://wordpress.org/extend/plugins/codecolorer/

 

Leave a Reply

Required fields are marked *.

*