wp_get_code_editor_settings()


WordPressのwp_get_code_editor_settings()関数は、コードエディターの設定を取得するために使用されます。主にJavaScriptのCodeMirrorライブラリと連携して、シンタックスハイライトやインデントなどの設定を提供します。

構文

wp_get_code_editor_settings( array $args );

引数の説明:

  • $args(array)—コードエディターの設定をカスタマイズするための引数。
  • type(string)—編集するコードのタイプ(php、css、jsなど)。
  • codemirror(array)—CodeMirrorの特定の設定を上書きします。
  • indentUnit(int)—インデントのスペース数。
  • tabSize(int)—タブのサイズ。
  • lineNumbers(bool)—行番号を表示するかどうか。
  • lineWrapping(bool)—行の折り返しを有効にするかどうか。

例1:PHPコード用のエディター設定を取得

PHPファイル編集用の基本設定を取得します。

$settings = wp_get_code_editor_settings(array('type'=>'php'));

例2:JavaScript用に行番号を無効化

JS編集時に行番号を非表示にします。

$settings = wp_get_code_editor_settings(array('type'=>'javascript','lineNumbers'=>false));

例3:CSS編集用にタブサイズを変更

CSS編集時のタブサイズを2スペースに設定。

$settings = wp_get_code_editor_settings(array('type'=>'css','tabSize'=>2));

例4:HTML編集で行折り返しを有効化

HTMLコードの行折り返しを有効にします。

$settings = wp_get_code_editor_settings(array('type'=>'html','lineWrapping'=>true));

例5:カスタムCodeMirror設定を追加

CodeMirrorのテーマを変更します。

$settings = wp_get_code_editor_settings(array('type'=>'js','codemirror'=>array('theme'=>'monokai')));

例6:インデント単位を4スペースに設定

インデントを4スペースに変更。

$settings = wp_get_code_editor_settings(array('indentUnit'=>4));

例7:複合的な設定の適用

複数の設定を同時に適用します。

$settings = wp_get_code_editor_settings(array('type'=>'php','lineNumbers'=>true,'tabSize'=>4,'lineWrapping'=>false));

注意点

  • この関数は管理画面での使用を想定しています。
  • CodeMirrorライブラリが読み込まれている必要があります。
  • 設定はフィルターフックで変更可能です。

関連機能: