Add SoftwareSourceCode Structured Data To SyntaxHighlighter Evolved

Add structured data to your SyntaxHighlighter Evolved code snippets. This adds the “SoftwareSourceCode” schema type to your code snippets if you’re using the SyntaxHighlighter Evolved WordPress plugin (by Alex Mills/Viper007Bond and Automattic).

Schema.org has replaced the previous “Code” schema type with the new “SoftwareSourceCode” schema type.

The following code will wrap your SyntaxHighlighter Evolved code snippets with structured data (microdata) for the “SoftwareSourceCode” schema type.

Each of your code snippets will also get the schema properties that belong to “SoftwareSourceCode”:

  • The codeSampleType property will be set to “code snippet.”
  • The text property will be set to the actual code text.
  • The programmingLanguage will be set to the ComputerLanguage property, which will be set to the language of the SyntaxHighlighter brush that the code snippet is using. So, the following function will detect the programming language of your code snippet. It will detect the programming language whether you use the shortcode language tags, or pass the language/lang as a parameter.

This one function will take care of adding all the necessary structured data to each of your code snippets:

/**
 * Wrap Syntaxhighlighter code in microdata structured data for SoftwareSourceCode type
 */
function isa_syntaxhighlighter_htmlresult( $value ) {
	$language_property = '';
	// Try to extract the programming language
	$prefix = 'brush:';
	$pos = strpos( $value, $prefix );
	if ( $pos !== false ) { 
		// get part of string after brush:
		$index = strpos( $value, $prefix ) + strlen( $prefix );
		$result = substr( $value, $index );
		// get part of string before ';' 
		$x = explode(";", $result );
		$lang = empty( $x[0] ) ? '' : trim( $x[0] );
		if ( $lang ) {
			// add programmingLanguage property 
			$language_property = '<span itemprop="programmingLanguage" itemscope itemtype="http://schema.org/ComputerLanguage"><meta itemprop="name" content="' .
				$lang . '" /></span>';
		}
	}

	// replace <pre with <pre schema itemprop text
	$pre = preg_replace('/^<pre /', '<pre itemprop="text" ', $value);

	return '<div itemscope itemtype="http://schema.org/SoftwareSourceCode"><meta itemprop="codeSampleType" content="code snippet" />' . $language_property . $pre . '</div>';
}
add_filter('syntaxhighlighter_htmlresult', 'isa_syntaxhighlighter_htmlresult');

See more: , ,

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]