Support

Account

Home Forums General Issues Change WYSIWYG toolbars for specific fields only

Solved

Change WYSIWYG toolbars for specific fields only

  • Hello, Hope you all doing good. I am working on a Gutenberg block and getting stuck to the following. I found out that by using a bit of code I can change the fields added to the WYSIWYG bars.

    What I would like to do, is change this, but only for a couple of type of fields. Basicly I want to able to have the normal WYSIWYG options + a more basic one.

    I now have the following code, but not working due to remove_filter always overrules my earlier add_filter.

    
    add_filter('acf/load_field/type=wysiwyg', 'normal_wysiwyg_field', 9, 1);
    
    // Change the basic wysiwyg interface for basic blocks
    function basic_wysiwyg( $toolbars ) {
    
      $toolbars['Basic'][1] = array(
        'bold',
        'italic',
        'underline',
        'strikethrough',
        'link'
      );
    
    	unset( $toolbars['Full'] );
    
    	return $toolbars;
    }
    
    // Add the earlier function as a filter to the targeted field
    function normal_wysiwyg_field($field) {
      $basic_wysiwyg_fields = array(
        'field_5bcc4ed44d858'
      );
    
      if(in_array($field['key'], $basic_wysiwyg_fields)) {
        add_filter('acf/fields/wysiwyg/toolbars', 'basic_wysiwyg');
      } else {
        remove_filter('acf/fields/wysiwyg/toolbars', 'basic_wysiwyg');
      }
      return $field;
    }
    

    Hope you can help me figure this out.

  • I haven’t tested if this works in Gutenberg yet, but works great in the classic editor. https://support.advancedcustomfields.com/forums/topic/resrict-valid-formats-in-wysiwyg-editor/

  • You should be using the field key specific version of the filter rather than the generic field type version acf/load_field/type=wysiwyg

    If this is not possible then you should use the acf/prepare_field filter for specific fields.

  • Since this topic still has some attention I came back to share my solution. I misunderstood the way of making a more simple toolbar. The current solution suites my needs:

    
    if (class_exists('acf')) {
      add_filter('acf/fields/wysiwyg/toolbars', 'wysiwyg_toolbars');
    }
    
    function wysiwyg_toolbars($toolbars) {
      $toolbars['Very Very Simple'] = array();
      $toolbars['Very Very Simple'][1] = array('bold', 'italic', 'underline', 'strikethrough');
    
      $toolbars['Very Simple'] = array();
      $toolbars['Very Simple'][1] = array('bold', 'italic', 'underline', 'strikethrough', 'link');
    
      return $toolbars;
    }
    

    After that, you can assign the field type to your designated field, just like you normally would do.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.