Snippets: Custom Blog Post Blocks/Sections & Ouput

This is very similar to the snippet example here here but it allows you to have a custom output function rather then creating a template part.

// Add new block for our singular blog posts
// This allows us to manage it via the customizer under Customize > Blog > Single > Single Layout Elements
function my_add_new_block( $blocks ) {
	$blocks['new_block_id'] = __( 'New Block Name', 'total' );
	return $blocks;
}
add_filter( 'wpex_blog_single_blocks', 'my_add_new_block' );

// Define the output of our new block
function my_new_block_output( $blocks ) {

	// Before defining the output we want to make sure the block is enabled in the Customizer
	if ( isset( $blocks[ 'new_block_id' ] ) ) {

		// Example 1: Add new block with an anonymous function for the output
		$blocks['new_block_id'] = function() {

			echo 'test';

		};

		// Example 2: Add new block with a custom function as the output
		$blocks['new_block_id'] = my_function_name();

		// Example 3: Add new block with an anonymous function that returns a template part
		$blocks['new_block_id'] = function() {

			get_template_part( 'my-custom-block-file' );

		};

	}

	// Return blocks
	return $blocks;

}
add_filter( 'wpex_blog_single_layout_blocks', 'my_new_block_output' );

Version Required:4.0++

Snippet Type: PHP

Posted Under: Blog

All PHP snippets should be added via a child theme's functions.php file.