概要
format_to_edit
フィルタは、WordPressにおいて投稿やコメントの内容を、編集用に整形するために使用されるフィルタです。このフィルタは、特にテキストを編集向けに整形する際に利用されます。具体的には、以下のような機能を実装する際に役立ちます。
- テキストエリアに挿入される内容の整形
- 特殊文字のエスケープ処理
- スマイリーやショートコードの処理
- HTMLタグのフォーマット調整
- 不適切な自動整形の防止
- カスタム投稿タイプの整形処理
- エディターのインターフェース用データの整形
- 投稿内容のテーマやプラグインによるカスタマイズ
構文
add_filter('format_to_edit', 'custom_format_to_edit', 10, 1);
パラメータ
$content
(string): 編集対象のコンテンツ。
戻り値
- (string): 編集用に整形されたコンテンツ。
関連する関数
使用可能なバージョン
- WordPress 1.5以降
コアファイルのパス
wp-includes/plugin.php
この関数のアクションでの使用可能性
アクション名 | 使用可否 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプル1: 特殊文字をエスケープする
add_filter('format_to_edit', 'escape_special_characters');
function escape_special_characters($content) {
return esc_html($content);
}
このサンプルは、テキストエリアに表示する際に特殊文字をエスケープ処理します。
サンプル2: スマイリーを無効化する
add_filter('format_to_edit', 'disable_smileys');
function disable_smileys($content) {
return remove_smileys($content);
}
このコードは、すべてのスマイリーを無効化して編集用の内容を整形します。
サンプル3: 特定のショートコードを変換する
add_filter('format_to_edit', 'transform_shortcodes');
function transform_shortcodes($content) {
return do_shortcode($content);
}
このコードは、ショートコードを実行し、その結果を編集内容に含めます。
サンプル4: 自動整形を挿入しない
add_filter('format_to_edit', 'disable_auto_format');
function disable_auto_format($content) {
return $content; // 自動整形を行わない
}
このサンプルは、投稿された内容に対して自動整形を行わないようにします。
サンプル5: カスタムフィルタを追加
add_filter('format_to_edit', 'custom_content_format');
function custom_content_format($content) {
// カスタム処理を行う
$content = str_replace('old_text', 'new_text', $content);
return $content;
}
このサンプルは、特定のテキストを別のテキストに置き換えるカスタム処理を実行します。
参考ページ: https://developer.wordpress.org/reference/hooks/format_to_edit/