Post code snippets in WordPress

Here is the simplest way to easily add source code to WordPress with the built-in TinyMCE editor. There are a lot of posts of people looking for a simple solution like this. And Many people wrote plugins, that don't really work. MoxieCode gets badmouthed for a poorly written editor. Well that's all pretty silly. All we needed to do is read some of TinyMCE's documentation and figure a clever CSS fix.

At the end, you'll be able to visually embed code just like:

 @ require('../../../wp-config.php');          

function wp_translate_tinymce_lang($text) {
if ( ! function_exists('__') ) {
return $text;
} else {
$search1 = "/^tinyMCELang[(['\"])(.*)1]( ?= ?)(['\"])(.*)4/Uem";
$replace1 = "'tinyMCELang[121]3'.stripslashes('4').__('5').stripslashes('4')";

$search2 = "/ : (['\"])(.*)1/Uem";
$replace2 = "' : '.stripslashes('1').__('2').stripslashes('1')";

$search = array($search1, $search2);
$replace = array($replace1, $replace2);

$text = preg_replace($search, $replace, $text);

return $text;
}
}
No plugins are needed. Minimal code changes are neccesary. And it works.

» Continue Reading »

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
CpILL's Gravatar And so the next time you upgrade WP all the changes will be reversed :(
# Posted By CpILL | 6/12/07 4:58 AM
imran's Gravatar Thanks alot this is really a great method to have <code> work for you.
I tried many different methods plugins, also css method.All failed This one seems to be working fine a big Thumbs UP. Thanks
# Posted By imran | 8/29/07 5:54 AM
Jules's Gravatar Yes I've tried a lot as well. None worked. Open source - hooray =)
In the end, as you can tell, I gave up on WordPress entirely.
# Posted By Jules | 9/14/07 8:37 PM
Christoph Voigt's Gravatar One should note that these are not &quot;minimal code changes&quot; and do affect core files of WordPress which is definately not a way to go if you want to regularily update your WordPress installation. That doesn't really qualify as an &quot;easy way to add source code to WordPress&quot; to me ;)
Better use some good plugins like WP-Syntax (http://wordpress.org/extend/plugins/wp-syntax/) that make use of GeSHi (http://qbnz.com/highlighter/). No modifications on the core files needed = happy admins everywhere :)
# Posted By Christoph Voigt | 10/29/07 4:40 PM
fire-man's Gravatar Nice sollution, but after I follow your tutorial my tinyMCE's Fonts view become smaller than usual.

how to make it normal again as usual??

thx before.
# Posted By fire-man | 1/16/08 9:54 PM
Jules's Gravatar In the PRE section of the CSS file, change the font-size from 11px to 12px (or any other size you want)
# Posted By Jules | 1/16/08 10:18 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.6.001.