Snippets: Custom Blog Entry 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. It also allows you to override any of the default "blocks" with your custom output!

// Add a new custom block option to the entry blocks so you can enable it via the Customizer
// NOTE: It will be disabled by default!
add_filter( 'wpex_blog_entry_blocks', function( $blocks ) {

    // Add new block "advertisement"
    $blocks['new_block_id'] = __( 'New block', 'wpex' );

    // Return blocks
    return $blocks;

} );

// Custom block front-end output
// Note: This shows 3 different methods for your custom block output as well as an example on how to modify a default block output
add_filter( 'wpex_blog_entry_layout_blocks', function( $blocks ) {

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

		$blocks['new_block_id'] = function() {

			echo 'test';

		};

	}

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

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

		$blocks['new_block_id'] = function() {

			get_template_part( 'your-template-part' );

		};

	}

	// Example 4: Override the default featured_media block
	if ( isset( $blocks['featured_media'] ) ) {

		$blocks['featured_media'] = function() {

			echo 'My custom featured media';

		};

	}

	// Return blocks
	return $blocks;

} );

Version Required:4.0+

Snippet Type: PHP

Posted Under: Blog

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