How to set up or disable lint in the built-in code editor

    Under the cut there is a small note on how you can set up rules for linting in the built-in WordPress code editor.

    Starting from version 4.9 , CodeMirror is built into WordPress . It supports syntax highlighting for more than 100 languages, and also has a built-in code analyzer.

    So, to change the parameters, the wp_code_editor_settings filter will help us .

    The first parameter is the array of options for the code editor. In it we are interested in only a few properties. See the documentation for details .

    add_filter( 'wp_code_editor_settings', 'change_code_editor_settings');
    functionchange_code_editor_settings( $settings ){
      /**
       * Массив параметров передаваемых в codemirror
       * @see https://codemirror.net/doc/manual.html#config
       */
      $settings['codemirror']
      /**
       * Массив параметров для CSSLint
       * @see https://github.com/CSSLint/csslint/wiki
       */
      $settings['csslint']
      /**
       * Массив параметров для JSHint
       * @see https://jshint.com/docs/options
       */
      $settings['jshint']
      /**
       * Массив параметров для HTMLHint
       * @see https://github.com/htmlhint/HTMLHint/wiki/Rules
       */
      $settings['htmlhint']
      return $settings;
    }
    

    Examples


    Turn off CSSLint checking while leaving syntax highlighting . (It can be useful if you use css variables in the theme. # 720 )

    add_filter( 'wp_code_editor_settings', 'disable_csslint' );
    functiondisable_csslint( $settings ){
      if ($settings['codemirror']['mode'] === 'css') {
        $settings['codemirror']['lint'] = false;
      }
      return $settings;
    }
    

    Register a global variable.

    add_filter( 'wp_code_editor_settings', 'change_code_editor_settings');
    functionchange_code_editor_settings( $settings ){
      $settings['jshint']['globals']['axios'] = false// Глобальная переменная существует
      $settings['jshint']['globals']['user_rates'] = true// Глобальная переменная существует и доступна для записиreturn $settings;
    }
    

    Prohibit the use of anti-boots without values

    add_filter( 'wp_code_editor_settings', 'change_code_editor_settings');
    functionchange_code_editor_settings( $settings ){
      $settings['htmlhint']['attr-value-not-empty'] = truereturn $settings;
    }
    

    Only registered users can participate in the survey. Sign in , please.

    Using the built-in code editor?


    Also popular now: