I know there are a lot of hooks in WP for altering a posts content (and other fields) before the post is written to the DB upon saving; but what I am interested in doing is running my custom filter before the post is loaded for editing. So basically when my custom post type is loaded by /wp-admin/post.php?post=##&action=edit, I would like to run my filter on it before the content is displayed in the WYSIWYG editor. Is that possible or no?
2 Answers
You can use the the_editor_content
filter:
function wpa_editor_content( $content ) {
global $post;
return "this post's id is $post->ID " . $content;
}
add_filter( 'the_editor_content', 'wpa_editor_content' );
This is quite an old question but I came across it while searching for some answers of my own and thought I'd add an additional answer.
WordPress has a filter called the_content()
which allows you to filter the content returned to almost any function, including the editor.
Check out the documentation here: https://developer.wordpress.org/reference/hooks/the_content/
Code example from the docs, adapted to determine if we're in the admin:
add_filter( 'the_content', 'filter_the_content_in_the_main_loop', 1 );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're in the wp-admin
if ( is_admin ) {
return $content . esc_html__( 'I’m filtering the content in the admin', 'wporg');
}
return $content;
}