Add SoftwareSourceCode Schema to Yoast SEO

This function extends Yoast SEO’s structured data by adding a SoftwareSourceCode schema entry to all single Snippet custom post types. It automatically gathers the post’s title, excerpt, tags, and author information, detects the programming language (defaulting to PHP), and extracts the first <pre><code> block from the content to include as the code text. The result is a clean, well-structured schema node that clearly defines the post as SoftwareSourceCode—helping search engines better understand and index your Snippet posts as examples of programming code, improving SEO and visibility in developer-focused search results.

				
					/**
 * Add SoftwareSourceCode schema to Yoast's graph for Snippet posts.
 *
 * Extracts <code> content, tags (or fallbacks), and custom language/runtime/code type fields.
 * Uses the post excerpt for description.
 */
function ta_add_software_sourcecode_schema( $graph ) {
	// Only on individual Snippet posts
	if ( ! is_singular( 'snippets' ) ) {
		return $graph;
	}

	global $post;

	// Programming language (custom field), fallback to PHP
	$language = get_post_meta( $post->ID, 'language', true );
	if ( empty( $language ) ) {
		$language = 'PHP';
	}

	// runtime_platform (custom field), fallback to WordPress
	$runtime_platform = get_post_meta( $post->ID, 'runtime_platform', true );
	if ( empty( $runtime_platform ) ) {
		$runtime_platform = 'WordPress';
	}

	// code_sample_type (custom field), fallback to inline
	$code_sample_type = get_post_meta( $post->ID, 'code_sample_type', true );
	if ( empty( $code_sample_type ) ) {
		$code_sample_type = 'inline';
	}

	/**
	 * Collect keywords (from tags, categories, or fallback)
	 */
	$keywords = array();

	// Try post_tag first (if CPT supports it)
	$tags = get_the_terms( $post, 'post_tag' );
	if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
		foreach ( $tags as $tag ) {
			$keywords[] = $tag->name;
		}
	}

	// Fallback to categories if tags are missing
	if ( empty( $keywords ) ) {
		$categories = get_the_terms( $post, 'category' );
		if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
			foreach ( $categories as $cat ) {
				$keywords[] = $cat->name;
			}
		}
	}

	// Final fallback — ensure keywords aren't empty
	if ( empty( $keywords ) ) {
		$keywords = array( get_the_title(), $language, $runtime_platform );
	}

	/**
	 * Extract the code from the post content
	 */
	$code_text = '';

	// Match <pre><code> or plain <code>
	if ( preg_match( '/<pre[^>]*><code[^>]*>(.*?)<\/code><\/pre>/is', $post->post_content, $matches ) ) {
		$code_text = trim( html_entity_decode( wp_strip_all_tags( $matches[1] ) ) );
	} elseif ( preg_match( '/<code[^>]*>(.*?)<\/code>/is', $post->post_content, $matches ) ) {
		$code_text = trim( html_entity_decode( wp_strip_all_tags( $matches[1] ) ) );
	}

	// Guarantee a non-empty text field
	if ( empty( $code_text ) ) {
		$code_text = 'Code snippet available on this page.';
	}

	/**
	 * Build the SoftwareSourceCode schema
	 */
	$schema = array(
		'@type'               => 'SoftwareSourceCode',
		'@id'                 => get_permalink( $post->ID ) . '#softwareSourceCode',
		'name'                => get_the_title(),
		'description'         => get_the_excerpt(),
		'programmingLanguage' => $language,
		'codeSampleType'      => $code_sample_type,
		'runtimePlatform'     => $runtime_platform,
		'inLanguage'          => 'en',
		'keywords'            => $keywords,
		'url'                 => get_permalink( $post->ID ),
		'author'              => array(
			'@type' => 'Person',
			'name'  => 'Tyler Ager',
			'url'   => home_url( '/' ),
		),
		'text'                => $code_text, // Keep at the end for readability and consistency
	);

	// Append the schema to Yoast’s graph
	$graph[] = $schema;

	return $graph;
}
add_filter( 'wpseo_schema_graph', 'ta_add_software_sourcecode_schema' );
				
			

If this code helped you, or you have any questions on implementation, don’t hesitate to reach out.