OK, this is possible, but it will be a little bit tricky and I would recommend a lot of testing after deploying this solution.
Disclaimer: I use base64_encode
and base64_decode
as cipher/decipher functions, but of course you can change them to whatever you like.
First part (encoding before post is stored in DB) is pretty easy:
function encrypt_post_content( $data, $postarr ) {
$data['post_content'] = base64_encode( $data['post_content'] );
return $data;
}
add_filter( 'wp_insert_post_data', 'encrypt_post_content', 10, 2 );
Second part is a little bit trickier, because WordPress uses raw
context in many places and if this context is used, then no filters are applied to post fields…
function decrypt_post_content( $content, $default_editor =false ) {
return base64_decode( $content );
}
add_filter( 'the_editor_content', 'decrypt_post_content', 10, 2 );
add_filter( 'the_content', 'decrypt_post_content', 1, 1 );
PS. It’s a little bit sad that there is no easy possibility to modify post_content just after getting it from DB… 🙁