Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
namespace JPB\Yassss;
add_filter( 'wp_insert_post_data', __NAMESPACE__ . '\\replace_values' );
function replace_values( $post_data ) {
$content = empty( $post_data['post_content'] ) ? '' : $post_data['post_content'];
$content = str_replace( '{{ my_marker }}', some_really_long_running_function(), $content );
$post_data['post_content'] = $content;
return $post_data;
}
<?php
namespace JPB\WTF;
add_action( 'save_post', __NAMESPACE__ . '\\replace_values' );
function replace_values( $post_id ) {
$content = get_post( $post_id )->post_content;
$content = str_replace( '{{ my_marker }}', some_really_long_running_function(), $content );
remove_action( 'save_post', __FUNCTION__ );
wp_update_post( [ 'ID' => $post_id, 'post_content' => $content ] );
add_action( 'save_post', __FUNCTION__ )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment