Skip to content

Instantly share code, notes, and snippets.

@DahmaniAdame
Forked from jmccall75/functions.php
Created May 4, 2022 21:58
Show Gist options
  • Save DahmaniAdame/a51b32c8f3599faa134049dcb9ad69e1 to your computer and use it in GitHub Desktop.
Save DahmaniAdame/a51b32c8f3599faa134049dcb9ad69e1 to your computer and use it in GitHub Desktop.

Revisions

  1. @jmccall75 jmccall75 revised this gist May 4, 2022. 1 changed file with 19 additions and 7 deletions.
    26 changes: 19 additions & 7 deletions theme.json
    Original file line number Diff line number Diff line change
    @@ -2,27 +2,39 @@
    "$schema": "http://schemas.wp.org/trunk/theme.json",
    "version": 2,
    "settings": {
    "layout": {
    "contentSize": "930px"
    },
    "appearanceTools": false,
    "color": {
    "text": false,
    "background": false,
    "link": false,
    "custom": false,
    "customDuotone": false,
    "customGradient": false,
    "defaultDuotone": false,
    "defaultGradients": false,
    "defaultPalette": false,
    "text": false
    "defaultPalette": false
    },
    "layout": {
    "contentSize": null,
    "wideSize": null
    },
    "spacing": {
    "blockGap": null,
    "margin": false,
    "padding": false,
    "units": []
    },
    "typography": {
    "customFontSize": false,
    "dropCap": false,
    "fontStyle": false,
    "fontWeight": false,
    "letterSpacing": false,
    "lineHeight": false,
    "textDecoration": false,
    "textTransform": false
    "textTransform": false,
    "dropCap": false,
    "fontSizes": [],
    "fontFamilies": []
    }
    }
    }
  2. @jmccall75 jmccall75 revised this gist May 4, 2022. 2 changed files with 62 additions and 26 deletions.
    71 changes: 46 additions & 25 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,60 +1,81 @@
    <?php
    //Disable gutenberg style in Front
    /**
    * Gutenberg Block customization for this theme.
    */
    // gutenberg disable for posts
    //add_filter('use_block_editor_for_post', '__return_false', 10);

    // gutenberg disable for post types
    //add_filter('use_block_editor_for_post_type', '__return_false', 10);

    /**
    *Disable gutenberg stylesheets in frontend; requires creating custom styles for allowed core blocks
    **/
    function jjm_deregister_styles() {
    wp_dequeue_style( 'wp-block-library' );
    }
    add_action( 'wp_print_styles', 'jjm_deregister_styles', 100 );

    /**
    * Theme Block defaults
    * Customize core block functionality
    */
    function jjm_2022_block_setup() {
    add_theme_support( 'disable-custom-colors' );
    add_theme_support( 'disable-custom-gradients' ); // Sometimes you add theme support to disable functionality
    remove_theme_support( 'block-templates' ); // This one was not easy to Google
    remove_theme_support( 'core-block-patterns' ); // Sometimes you remove theme support to disable functionality
    add_theme_support( 'disable-custom-font-sizes' );
    add_theme_support('editor-font-sizes', array( // Can't seem to remove entirely, so limit to our "normal" font size
    array(
    'name' => 'Normal',
    'size' => 20,
    'slug' => 'normal'
    )
    ) );
    remove_theme_support( 'widgets-block-editor' );
    // Sometimes you disable block functionality by adding theme support...
    add_theme_support( 'disable-custom-colors' ); // User-defined colors
    add_theme_support( 'disable-custom-gradients' ); // User-defined gradient
    add_theme_support( 'disable-custom-font-sizes' ); // User-defined font sizes
    add_theme_support('editor-font-sizes', array( // Define our own allowed font sizes here
    array(
    'name' => 'Normal',
    'size' => 20,
    'slug' => 'normal'
    )
    ) );

    // And sometimes by removing theme support...
    remove_theme_support( 'block-templates' ); // Allows user to create their own templates from within the editor
    remove_theme_support( 'core-block-patterns' ); // Allows user to create their own block patterns from within the editor
    remove_theme_support( 'widgets-block-editor' ); // Revert to the classic widget editing experience
    }

    add_action( 'after_setup_theme', 'jjm_2022_block_setup' );

    // Remove some block style options via JS (See below JS file)
    /**
    * Some functionality is easier to remove with JS. Or only possible to remove with JS.
    **/
    add_action( 'init', 'jjm_remove_block_styles' );
    function jjm_remove_block_styles() {
    // Register the block editor script.
    wp_register_script( 'remove-block-style', get_stylesheet_directory_uri() . '/assets/js/remove-block-styles.js', [ 'wp-blocks', 'wp-edit-post' ] );
    // register block editor script.
    // I'm actually not at all sure what this does.
    register_block_type( 'remove/block-style', [
    'editor_script' => 'remove-block-style',
    ] );
    }
    // See you later dropcaps

    /**
    * Back to removing functionality via PHP
    **/
    function jjm_docs_block_editor_settings( $editor_settings, $editor_context ) {
    $editor_settings['__experimentalFeatures']['typography']['dropCap'] = false;

    return $editor_settings;
    }

    add_filter( 'block_editor_settings_all', 'jjm_docs_block_editor_settings', 10, 2 );


    /************************************************
    * Default Block Whitelist
    * Default Block Whitelist: only blocks in the whitelist are allowed
    ************************************************/

    add_filter( 'allowed_block_types_all', 'jjm_allowed_block_types', 10, 2 );

    function jjm_allowed_block_types( $allowed_block_types, $editor_context ) {

    $home_id = get_option('page_on_front');
    $current_post_id = $editor_context->post->ID;

    if ( $current_post_id == $home_id ) { // Restrict block(s) allowed on homepage
    if ( $current_post_id == $home_id ) { // Restrict block(s) allowed on homepage.

    return array(
    'acf/home-feature',
    @@ -91,10 +112,10 @@ function jjm_allowed_block_types( $allowed_block_types, $editor_context ) {
    }

    /**
    * Disable Gutenberg on certain templates. Thanks Bill Erickson https://www.billerickson.net/disabling-gutenberg-certain-templates/
    * Templates and Page IDs without editor
    *
    */
    * Disable the block editor by template and/or page_id. Reverts to classic editor.
    * Courtesty of Bill Erickson:
    * https://www.billerickson.net/disabling-gutenberg-certain-templates/
    **/
    function jjm_disable_editor( $id = false ) {

    $excluded_templates = array(
    17 changes: 16 additions & 1 deletion remove-block-styles.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,19 @@
    // Remove formatting options for image block
    wp.domReady(() => {
    wp.blocks.unregisterBlockStyle('core/image', 'rounded');
    wp.blocks.unregisterBlockStyle('core/image', 'default');
    } );
    } );

    // Whitelist on specific embed block variations
    // Thanks Ty Baily (https://www.linkedin.com/in/tylerb24890)
    const { getBlockVariations, unregisterBlockVariation } = wp.blocks;

    wp.domReady(() => {
    const allowedEmbedBlocks = ['vimeo', 'youtube', 'twitter', 'soundcloud']; // Only these are allowed

    getBlockVariations('core/embed').forEach(function (blockVariation) {
    if (allowedEmbedBlocks.indexOf(blockVariation.name) === -1) {
    unregisterBlockVariation('core/embed', blockVariation.name);
    }
    });
    });
  3. @jmccall75 jmccall75 revised this gist May 3, 2022. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@ function jjm_deregister_styles() {
    }
    add_action( 'wp_print_styles', 'jjm_deregister_styles', 100 );

    if ( ! function_exists( 'jjm_2022_block_setup' ) ) :
    /**
    * Theme Block defaults
    */
    @@ -24,7 +23,6 @@ function jjm_2022_block_setup() {
    ) );
    remove_theme_support( 'widgets-block-editor' );
    }
    endif;
    add_action( 'after_setup_theme', 'jjm_2022_block_setup' );

    // Remove some block style options via JS (See below JS file)
  4. @jmccall75 jmccall75 revised this gist May 3, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,9 @@ function jjm_deregister_styles() {
    */
    function jjm_2022_block_setup() {
    add_theme_support( 'disable-custom-colors' );
    add_theme_support( 'disable-custom-gradients' );
    add_theme_support( 'disable-custom-gradients' ); // Sometimes you add theme support to disable functionality
    remove_theme_support( 'block-templates' ); // This one was not easy to Google
    remove_theme_support( 'core-block-patterns' );
    remove_theme_support( 'core-block-patterns' ); // Sometimes you remove theme support to disable functionality
    add_theme_support( 'disable-custom-font-sizes' );
    add_theme_support('editor-font-sizes', array( // Can't seem to remove entirely, so limit to our "normal" font size
    array(
  5. @jmccall75 jmccall75 revised this gist May 3, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@ function jjm_deregister_styles() {
    function jjm_2022_block_setup() {
    add_theme_support( 'disable-custom-colors' );
    add_theme_support( 'disable-custom-gradients' );
    remove_theme_support( 'block-templates' );
    remove_theme_support( 'core-block-patterns' ); // This one was not easy to Google
    remove_theme_support( 'block-templates' ); // This one was not easy to Google
    remove_theme_support( 'core-block-patterns' );
    add_theme_support( 'disable-custom-font-sizes' );
    add_theme_support('editor-font-sizes', array( // Can't seem to remove entirely, so limit to our "normal" font size
    array(
  6. @jmccall75 jmccall75 revised this gist May 3, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ function jjm_2022_block_setup() {
    add_theme_support( 'disable-custom-colors' );
    add_theme_support( 'disable-custom-gradients' );
    remove_theme_support( 'block-templates' );
    remove_theme_support( 'core-block-patterns' );
    remove_theme_support( 'core-block-patterns' ); // This one was not easy to Google
    add_theme_support( 'disable-custom-font-sizes' );
    add_theme_support('editor-font-sizes', array( // Can't seem to remove entirely, so limit to our "normal" font size
    array(
  7. @jmccall75 jmccall75 created this gist May 3, 2022.
    136 changes: 136 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    <?php
    //Disable gutenberg style in Front
    function jjm_deregister_styles() {
    wp_dequeue_style( 'wp-block-library' );
    }
    add_action( 'wp_print_styles', 'jjm_deregister_styles', 100 );

    if ( ! function_exists( 'jjm_2022_block_setup' ) ) :
    /**
    * Theme Block defaults
    */
    function jjm_2022_block_setup() {
    add_theme_support( 'disable-custom-colors' );
    add_theme_support( 'disable-custom-gradients' );
    remove_theme_support( 'block-templates' );
    remove_theme_support( 'core-block-patterns' );
    add_theme_support( 'disable-custom-font-sizes' );
    add_theme_support('editor-font-sizes', array( // Can't seem to remove entirely, so limit to our "normal" font size
    array(
    'name' => 'Normal',
    'size' => 20,
    'slug' => 'normal'
    )
    ) );
    remove_theme_support( 'widgets-block-editor' );
    }
    endif;
    add_action( 'after_setup_theme', 'jjm_2022_block_setup' );

    // Remove some block style options via JS (See below JS file)
    add_action( 'init', 'jjm_remove_block_styles' );
    function jjm_remove_block_styles() {
    // Register the block editor script.
    wp_register_script( 'remove-block-style', get_stylesheet_directory_uri() . '/assets/js/remove-block-styles.js', [ 'wp-blocks', 'wp-edit-post' ] );
    // register block editor script.
    register_block_type( 'remove/block-style', [
    'editor_script' => 'remove-block-style',
    ] );
    }
    // See you later dropcaps
    function jjm_docs_block_editor_settings( $editor_settings, $editor_context ) {
    $editor_settings['__experimentalFeatures']['typography']['dropCap'] = false;

    return $editor_settings;
    }

    add_filter( 'block_editor_settings_all', 'jjm_docs_block_editor_settings', 10, 2 );
    /************************************************
    * Default Block Whitelist
    ************************************************/

    add_filter( 'allowed_block_types_all', 'jjm_allowed_block_types', 10, 2 );

    function jjm_allowed_block_types( $allowed_block_types, $editor_context ) {

    $home_id = get_option('page_on_front');
    $current_post_id = $editor_context->post->ID;

    if ( $current_post_id == $home_id ) { // Restrict block(s) allowed on homepage

    return array(
    'acf/home-feature',
    'acf/home-post',
    'acf/products',
    'acf/magazine-highlights',
    );

    } else { // Restrict blocks in all other contexts
    return array(
    'core/image',
    'core/paragraph',
    'core/heading',
    'core/list',
    'core/gallery',
    'core/shortcode',
    'core/audio',
    'core/file',
    'core/video',
    'core/verse',
    'core/html',
    //'core/pullquote',
    'core/preformatted',
    //'core/search',
    'core/separator',
    'core/embed',
    'acf/largepullquote',
    'acf/blockquote',
    'acf/search-field',
    //'yoast/faq-block',
    );
    }

    }

    /**
    * Disable Gutenberg on certain templates. Thanks Bill Erickson https://www.billerickson.net/disabling-gutenberg-certain-templates/
    * Templates and Page IDs without editor
    *
    */
    function jjm_disable_editor( $id = false ) {

    $excluded_templates = array(
    //'projects.php',
    );

    $excluded_ids = array(
    //get_option( 'page_on_front' ),
    get_option( 'page_for_posts' )
    );

    if( empty( $id ) )
    return false;

    $id = intval( $id );
    $template = get_page_template_slug( $id );

    return in_array( $id, $excluded_ids ) || in_array( $template, $excluded_templates );
    }

    /**
    * Disable Gutenberg by template
    *
    */
    function jjm_disable_gutenberg( $can_edit, $post_type ) {

    if( ! ( is_admin() && !empty( $_GET['post'] ) ) )
    return $can_edit;

    if( jjm_disable_editor( $_GET['post'] ) )
    $can_edit = false;

    return $can_edit;

    }
    add_filter( 'gutenberg_can_edit_post_type', 'jjm_disable_gutenberg', 10, 2 );
    add_filter( 'use_block_editor_for_post_type', 'jjm_disable_gutenberg', 10, 2 );
    4 changes: 4 additions & 0 deletions remove-block-styles.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    wp.domReady(() => {
    wp.blocks.unregisterBlockStyle('core/image', 'rounded');
    wp.blocks.unregisterBlockStyle('core/image', 'default');
    } );
    28 changes: 28 additions & 0 deletions theme.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    {
    "$schema": "http://schemas.wp.org/trunk/theme.json",
    "version": 2,
    "settings": {
    "layout": {
    "contentSize": "930px"
    },
    "color": {
    "background": false,
    "custom": false,
    "customDuotone": false,
    "customGradient": false,
    "defaultGradients": false,
    "defaultPalette": false,
    "text": false
    },
    "typography": {
    "customFontSize": false,
    "dropCap": false,
    "fontStyle": false,
    "fontWeight": false,
    "letterSpacing": false,
    "lineHeight": false,
    "textDecoration": false,
    "textTransform": false
    }
    }
    }