In der Blacklist werden Blöcke angezeigt, die nirgends im Editor verfügbar sind. Sonst kannst du unter 5) selbst definieren, welche Blöcke pro Post Type möglich sind.
PHP
<?php
/**
* Erlaubte Blöcke pro Post Type + globale Blacklist (robust)
*/
add_filter( 'allowed_block_types_all', function( $allowed, $context ) {
// 1) Globale Blacklist: diese Blöcke NIE erlauben
$global_blacklist = [
'core/button',
'core/buttons',
'core/column',
'core/columns',
'core/gallery',
'core/video',
'noptin/email-optin',
'noptin/form',
'happyfiles/gallery',
'fluentfom/guten-block',
'core/embed',
];
// 2) Alle registrierten Blocknamen holen
$all_registered = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() );
// 3) $allowed auf ein Array normalisieren
if ( $allowed === true || $allowed === null ) {
// "alles erlaubt" -> nimm alle registrierten
$allowed = $all_registered;
} elseif ( $allowed === false ) {
// "nichts erlaubt"
$allowed = [];
} elseif ( ! is_array( $allowed ) ) {
// Unerwarteter Typ -> fallback auf alle
$allowed = $all_registered;
}
// 4) Ohne Post-Kontext (z. B. Site-Editor) nur Blacklist anwenden
if ( empty( $context->post ) ) {
return array_values( array_diff( $allowed, $global_blacklist ) );
}
$post_type = $context->post->post_type;
// 5) Pro Post Type Whitelists (optional)
switch ( $post_type ) {
case 'post':
$allowed = [
'core/heading',
'core/paragraph',
];
break;
case 'portfolio':
$allowed = [
'core/heading',
'core/paragraph',
'core/image',
'core/block', // Reusable Blocks
'kadence/image', // Kadence Advanced Image
];
break;
case 'page':
// Seiten: alles (aus $allowed) – aber gleich kommt die Blacklist drüber
// -> keine Änderung nötig
break;
default:
// Andere Post Types: $allowed so lassen
break;
}
// 6) Blacklist IMMER zuletzt anwenden
$allowed = array_values( array_diff( $allowed, $global_blacklist ) );
return $allowed;
}, PHP_INT_MAX, 2 ); // sehr spät hooken, um andere Filter zu überstimmen
Nur Whitelists für Post Types
PHP
<?php
/**
* Nur für den Post Type "insight" eine Whitelist.
* Für alle anderen Post Types: alle Blöcke erlaubt.
*/
add_filter('allowed_block_types_all', function ($allowed, $context) {
// Im Site-Editor oder ohne Post-Kontext -> alles erlauben
if (empty($context->post)) {
return true; // true = alle registrierten Blöcke
}
// Nur für "insight" einschränken
if ($context->post->post_type === 'insight') {
return [
'core/heading',
'core/paragraph',
'core/list',
'core/list-item', // Hinweis: ist ein Inner-Block von core/list, schadet aber nicht
'core/table',
'kadence/image',
];
}
// Für alle anderen Post Types: alles erlauben
return true;
}, PHP_INT_MAX, 2);
Nur Blacklist – alles andere bleibt
PHP
<?php
/**
* Globale Block-Blacklist: diese Blocktypen NIE anzeigen/erlauben.
* Rest bleibt überall erlaubt (alle Post Types, inkl. Site-Editor).
*/
add_filter('allowed_block_types_all', function ($allowed, $context) {
// 1) Liste der unerwünschten Blöcke
$blacklist = [
'core/button',
'core/buttons',
'core/embed',
];
// 2) Alle registrierten Blöcke holen
$all = array_keys(WP_Block_Type_Registry::get_instance()->get_all_registered());
// 3) Erlaubte = Alle minus Blacklist
$allowed_blocks = array_values(array_diff($all, $blacklist));
return $allowed_blocks; // gilt global für alle Kontexte/Post Types
}, PHP_INT_MAX, 2);