要在保存帖子内容之前更改帖子内容,可以使用wp_insert_post_data 滤器
在这个例子中,我只是用一种简单/虚拟的方式来表示加密,我只是用相应的ascii码替换每个字符。应该用您的加密方法替换它。
add_filter( \'wp_insert_post_data\' , \'encrypt_post\' , 99, 1 );
function encrypt_post( $data ) {
// check if it\'s a post
if(\'post\' !== $data[\'post_type\']) {
return $data;
}
// this is just for demonstration purposes (a simple char to ascii code conversion), it should be replaced by your encryption method
$title = str_split( $data[\'post_title\'] );
$title = array_map( function($n) { return ord( $n ); }, $title );
$title = implode( ".", $title );
$data[\'post_title\'] = $title;
return $data;
}
然后,要解密编辑页面上的帖子标题,可以使用title\\u edit\\u pre filter:
function decrypt_post_title( $title, $post_id ) {
if( \'post\' !== get_post_type( $post_id ) ) {
return $title;
}
// same dummy ascii code to char conversion
$title = explode( \'.\', $title );
$title = array_map( function($n) { return chr( $n ); }, $title );
$title = implode( "", $title );
return $title;
}
add_filter( \'title_edit_pre\', \'decrypt_post_title\', 99, 2 );
要解密其他post字段,需要检查其他动态
*_edit_pre 过滤器。