Enabling syntax highlighting

0 comments

Posted on 15th juni 2011 by Bart McLeod in Web development |WordPress

, , , ,

I would like to know what happens if I enter some php code. Does the syntax get highlighted by default in WordPress? Is it going to be stripped out? Will it be forbidden? Let me just type “echo ‘Hello World!’” and see what happens:

<?php echo ‘Hello World’; ?>

I expect this to show up just as I typed it, nothing special.

Now let me type it into the html editor, and see what I get then:

(You see nothing here, but on the backend, in the editor there is a hidden html comment that contains the php code. On the front-end, without the editor, the comment gets stripped out completely)

 

It gets commented out like this:

<!–?php echo ‘Hello World in the html editor’; ?–>

I remember there was a setting to allow php, or was that Drupal only? It must have been Drupal, because I can’t find it in WordPress.

In fact, it doesn’t matter. What I am after at the moment is not to have posts executed, I only want syntax highlighting.

I search for syntax highlighting in the plugins repository and I find six pages of plugins for it. One that looks very promising from its’ statistics is ‘Syntax Highlighter for WordPress’. And this is what it does:


function admin_menu() {
    $this->addOptionPage(__('Syntax Highlighter', $this->textdomain_name), array($this, 'option_page'));
}

It just requires pseudotags around the code snippet.  To preserve indentation, the pseudotags themselves should be wrapped in <pre></pre> tags, or you could turn off the visual editor for yourself (this is an option in WordPress). If you use the visual editor without <pre> tags all indentation will simply be lost. Now I notice that there is a neat dropdown box in the left corner of the editor toolbar, where you can select preformatted as a style for a paragraph! Let’s see if this works for a large block of code. No, it doesn’t work, you can set preformatted on the pseudotags using the visual editor, but you have to switch to html view to paste the code with the indention in.

	function __construct() {
		$this->init(__FILE__);

		$this->options = (array)$this->getOptions();
		$this->version = ( isset($this->options["version"]) ? $this->options["version"] : $this->sh_versions[0] );
		$this->theme = ( isset($this->options["theme"]) ? $this->options["theme"] : $this->sh_themes[0] );
		if (version_compare($this->version, "3.0", "theme === 'MDUltra')
			$this->theme = $this->sh_themes[0];

		$this->languages = array(
			"as3"  => array(false, 'AS3', 'js/shBrushAS3.js', 'shBrushAS3') ,
			"bash" => array(false, 'Bash', 'js/shBrushBash.js', 'shBrushBash') ,
			"c" => array(false, 'C', 'js/shBrushCpp.js', 'shBrushCpp') ,
			"cpp" => array(false, 'C++',  'js/shBrushCpp.js', 'shBrushCpp') ,
			"c-sharp" => array(false, 'C#', 'js/shBrushCSharp.js', 'shBrushCSharp') ,
			"coldfusion" => array(false, 'ColdFusion', 'js/shBrushColdFusion.js', 'shBrushColdFusion') ,
			"jscript" => array(false, 'Java Script', 'js/shBrushJScript.js', 'shBrushJScript') ,
			"java" => array(false, 'JAVA', 'js/shBrushJava.js', 'shBrushJava') ,
			"javafx" => array(false, 'JavaFX', 'js/shBrushJavaFX.js', 'shBrushJavaFX') ,
			"delphi" => array(false, 'Delphi', 'js/shBrushDelphi.js', 'shBrushDelphi') ,
			"diff" => array(false, 'Diff', 'js/shBrushDiff.js', 'shBrushDiff') ,
			"erlang" => array(false, 'Erlang', 'js/shBrushErlang.js', 'shBrushErlang') ,
			"groovy" => array(false, 'Groovy', 'js/shBrushGroovy.js', 'shBrushGroovy') ,
			"patch" => array(false, 'Patch', 'js/shBrushDiff.js', 'shBrushDiff') ,
			"pascal" => array(false, 'Pascal', 'js/shBrushDelphi.js', 'shBrushDelphi') ,
			"perl" => array(false, 'Perl', 'js/shBrushPerl.js', 'shBrushPerl') ,
			"php" => array(false, 'PHP', 'js/shBrushPhp.js', 'shBrushPhp') ,
			"plain" => array(false, 'Plain Text', 'js/shBrushPlain.js', 'shBrushPlain') ,
			"powershell" => array(false, 'PowerShell', 'js/shBrushPowerShell.js', 'shBrushPowerShell') ,
			"python" => array(false, 'Python', 'js/shBrushPython.js', 'shBrushPython') ,
			"ruby" => array(false, 'Ruby', 'js/shBrushRuby.js', 'shBrushRuby') ,
			"scala" => array(false, 'Scala', 'js/shBrushScala.js', 'shBrushScala') ,
			"shell" => array(false, 'Shell', 'js/shBrushBash.js', 'shBrushBash') ,
			"text" => array(false, 'Plain Text', 'js/shBrushPlain.js', 'shBrushPlain') ,
			"vb" => array(false, 'VB', 'js/shBrushVb.js', 'shBrushVb') ,
			"vb.net" => array(false, 'VB.Net', 'js/shBrushVb.js', 'shBrushVb') ,
			"sql" => array(false, 'SQL', 'js/shBrushSql.js', 'shBrushSql') ,
			"css" => array(false, 'CSS', 'js/shBrushCss.js', 'shBrushCss') ,
			"xml" => array(false, 'XML', 'js/shBrushXml.js', 'shBrushXml') ,
			"html" => array(false, 'HTML', 'js/shBrushXml.js', 'shBrushXml') ,
			"xhtml" => array(false, 'XHTML', 'js/shBrushXml.js', 'shBrushXml') ,
			"xslt" => array(false, 'XSLT', 'js/shBrushXml.js', 'shBrushXml') ,
			);

		if ( is_admin() ) {
			add_action('admin_menu', array(&$this, 'admin_menu'));
			add_filter('plugin_action_links', array(&$this, 'plugin_setting_links'), 10, 2 );

		} else {
			if ( $this->isKtai() ) {
				add_filter('the_content', array(&$this, 'parse_shortcodes'), 7);
			} else {
				add_action('wp_print_styles', array(&$this, 'add_stylesheet'));
				add_action('wp_head', array(&$this, 'add_head'));
				add_action('atom_head', array(&$this, 'add_feed_head'));
				add_action('rdf_header', array(&$this, 'add_feed_head'));
				add_action('rss2_head', array(&$this, 'add_feed_head'));
				add_action('rss_head', array(&$this, 'add_feed_head'));
			}
		}
	}

Nog geen reacties

Een reactie plaatsen

*
Easy AdSense by Unreal