Created
September 19, 2023 07:15
-
-
Save SchneiderSam/1d432f34e13a4934a1482e01a732b5af to your computer and use it in GitHub Desktop.
Dynamic Content Replacement in WordPress with Jet Engine and GenerateBlocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 asmailto:{{page-slug::mail}}
.Usage:
To display any Jet Engine Option Page value in your content:
{{page-slug::value}}
.page-slug
with the slug of your Jet Engine Option Page.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: