Skip to content

Filter Code Examples

Mark Root-Wiley edited this page Aug 21, 2023 · 6 revisions

Block Editor Filters

Hide or Unhide a Block

add_filter( 'mrw_hidden_blocks', 'show_verse_hide_comments', 10, 2 );
function show_verse_hide_comments( $blocks, $context ) {

	// Unhide the Verse Block (hidden by default)
	$blocks = array_diff( $blocks, array( 'core/verse' ) );

        // Unhide the details block only in the Site Editor
        if( $context === 'site-editor' ) {
            $blocks = array_diff( $blocks, array( 'core/details' ) );
        }

	// Hide the Cover block (not hidden by default)
	$blocks[] = 'core/cover';

	return $blocks;

}

Unhide all Query Blocks

add_filter( 'mrw_hidden_query_blocks', '__return_empty_array' );

Unhide default Block Variations hidden by the plugin

add_filter( 'mrw_hidden_block_styles', 'show_rounded_img_and_separator_styles' );
function show_rounded_img_and_separator_styles( $styles ) {

	// Unhide (show) one specific variation for a block type
	$styles['core/image'] = array_diff(
		$styles['core/image'],
		array( 'rounded')
	);

	// Unhide (show) *all* variations for a block type
	unset( $styles['core/separator'] );

	return $styles;

}

Show Editor Settings hidden by the plugin

add_filter( 'mrw_hidden_block_editor_settings', 'show_drop_cap_heading_1' );
function show_drop_cap_heading_1( $features ) {
	return array_diff( $features, array( 'drop-cap', 'heading-1' ) );
}

Classic Editor

Unhide the More button (and more block)

/* Add "Insert More Tag" Button and Block */
add_filter( 'mce_buttons', 'mrw_mce_add_more_tag_button' );
function mrw_mce_add_more_tag_button( $buttons ) {
    $buttons[57] = 'wp_more';
    ksort($buttons);
    return $buttons;
}

Add Custom Text Styles to the Editor

Note: When using this, make sure to style the CSS classes with your editor-style.css file.

add_filter( 'mrw_mce_text_style', 'mrw_add_text_styles_example' );
/**
 * Adds a "Text Styles" submenu to the "Formats" dropdown
 * 
 * @param array $styles Contains arrays of style_format arguments to define styles.
 * 						Note: Should be an "array of arrays"
 * 
 * @see tinymce.com/wiki.php/Configuration:style_formats
 * @see tinymce.com/wiki.php/Configuration:formats
 * @see wordpress.stackexchange.com/a/128950/9844
 */
function mrw_add_text_styles_example( $styles ) {
	$new_styles = array(
		/* Inline style that only applies to links */
		array(
			'title' => "Link Button", /* Label in "Formats" menu */
			'selector' => 'a', /* this style can ONLY be applied to existing <a> elements in the selection! */
			'classes' => 'button' /* class to add */
		),
		/* Inline style applied with a <span> */
		array(
			'title' => "Callout Text",
			'inline' => 'span', /* "inline" key for inline phrasing elements */
			'classes' => 'callout'
		),
		/* Block style applied to paragraph. Each paragraph in selection gets the classes. */
		array(
			'title' => "Warning Message",
			'block' => 'p', /* "block" key for block-level elements. these don't always behave */
			'classes' => 'message warning'  /* two classes work (space-separated) but can't be undone easily via editor */
		),
		/* Block style capable of containing other block-level elements */
		array(
			'title' => "Feature Box",
			'block' => 'section', 
			'classes' => 'feature-box',
			'wrapper' => true
		)
	);
	return array_merge( $styles, $new_styles );
}