Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save generatepress/a5a842677e75ef968aa52ee4d9e805a3 to your computer and use it in GitHub Desktop.
Save generatepress/a5a842677e75ef968aa52ee4d9e805a3 to your computer and use it in GitHub Desktop.
Search string for {{custom_field}} and return the post meta
if ( strpos( $content, '{{custom_field' ) !== false ) {
$data = preg_match_all('/{{custom_field="([^"]*)"}}/', $content, $matches);
foreach ( $matches[1] as $match ) {
if ( null !== get_post_meta( get_the_ID(), $match, true ) && '' !== get_post_meta( get_the_ID(), $match, true ) ) {
$search[] = '{{custom_field="' . $match . '"}}';
$replace[] = get_post_meta( get_the_ID(), $match, true );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment