Using WordPress ‘format_to_edit’ PHP filter

The format_to_edit WordPress PHP filter allows you to modify the text before it is formatted for editing.

Usage

add_filter('format_to_edit', 'your_custom_function', 10, 2);
function your_custom_function($content) {
  // your custom code here
  return $content;
}

Parameters

  • $content (string) – The text, prior to formatting for editing.

More information

See WordPress Developer Resources: format_to_edit

Examples

Replace all occurrences of a word

Replace all occurrences of the word “example” with “demo” in the text to be edited.

add_filter('format_to_edit', 'replace_example_with_demo', 10, 1);
function replace_example_with_demo($content) {
  $content = str_replace('example', 'demo', $content);
  return $content;
}

Remove HTML tags

Strip all HTML tags from the text before editing.

add_filter('format_to_edit', 'strip_html_tags', 10, 1);
function strip_html_tags($content) {
  $content = strip_tags($content);
  return $content;
}

Convert all URLs in the text to clickable links before editing.

add_filter('format_to_edit', 'convert_urls_to_links', 10, 1);
function convert_urls_to_links($content) {
  $content = make_clickable($content);
  return $content;
}

Add a prefix to all headings

Add a prefix “My Site – ” to all headings in the text before editing.

add_filter('format_to_edit', 'add_heading_prefix', 10, 1);
function add_heading_prefix($content) {
  $content = preg_replace('/(<h[1-6]>)(.*)(<\/h[1-6]>)/i', '$1My Site - $2$3', $content);
  return $content;
}

Capitalize all words

Capitalize the first letter of every word in the text before editing.

add_filter('format_to_edit', 'capitalize_all_words', 10, 1);
function capitalize_all_words($content) {
  $content = ucwords($content);
  return $content;
}