Skip to content

Instantly share code, notes, and snippets.

@SchneiderSam
Created September 19, 2023 07:15
Show Gist options
  • Save SchneiderSam/1d432f34e13a4934a1482e01a732b5af to your computer and use it in GitHub Desktop.
Save SchneiderSam/1d432f34e13a4934a1482e01a732b5af to your computer and use it in GitHub Desktop.
Dynamic Content Replacement in WordPress with Jet Engine and GenerateBlocks
function replace_jet_engine_option_in_content($content) {
// Regular expression to detect all occurrences of {{any-slug::any-name}}
preg_match_all('/{{(.*?)::(.*?)}}/', $content, $matches);
if (!empty($matches) && isset($matches[0])) {
foreach ($matches[0] as $index => $match) {
// Get the slug and the option name
$page_slug = $matches[1][$index];
$option_name = $matches[2][$index];
// Fetch the value from Jet Engine
$value = jet_engine()->listings->data->get_option($page_slug . '::' . $option_name);
// Replace the detected placeholder with its actual value
$content = str_replace($match, $value, $content);
}
}
return $content;
}
add_filter('the_content', 'replace_jet_engine_option_in_content');
@SchneiderSam
Copy link
Author

SchneiderSam commented Sep 19, 2023

Inspired by this video which showcases how to dynamically replace content in WordPress using ACF, I wanted to implement something similar but for those of us using Jet Engine.

This simple code snippet makes it possible. Just drop it into your functions.php, and you'll be able to insert dynamic placeholders directly into your content, just like in the video. For instance, creating dynamic mailto links becomes as easy as mailto:{{page-slug::mail}}.

Usage:

To display any Jet Engine Option Page value in your content:

  1. Use the format {{page-slug::value}}.
  2. Replace page-slug with the slug of your Jet Engine Option Page.
  3. Replace value with the name/ID of the desired option.

Note: Please ensure your page slugs and option names avoid using :: as it's designated as the delimiter. Currently, this code functions within content only. Any solutions for the Footer and Header are appreciated! :-)

Docs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment