Summary:
I’m trying to use the {select-2} merge tag as a value for a shortcode attribute in my E2Pdf template. The merge tag works correctly when placed directly in the template, but it doesn’t resolve when passed as an attribute in a shortcode. Here’s the detailed explanation of the issue:
What I’m Trying to Achieve:
I have a custom shortcode [test_song_lyrics title="{select-2}"] in my E2Pdf template. The shortcode is supposed to query a WordPress post by its title, where the title is dynamically provided by the {select-2} merge tag.
For example:
{select-2} resolves to The Lord's Prayer.
The shortcode [test_song_lyrics title="The Lord's Prayer"] works perfectly and retrieves the correct post content.
However, when I use [test_song_lyrics title="{select-2}"], the content is not retrieved, and my shortcode returns "Lyrics not found."
// Process the title: Replace {select-2} with its value if present
$title = sanitize_text_field($atts['title']);
if (strpos($title, '{') !== false) {
// Use E2Pdf merge tag processing
$title = apply_filters('e2pdf_extension_render_shortcodes', $title);
}
error_log("Shortcode received title: " . $title);
// Query the post
$post = new WP_Query([
'post_type' => 'post',
'title' => $title,
'posts_per_page' => 1,
]);
if ($post->have_posts()) {
$post->the_post();
$lyrics = apply_filters('the_content', get_the_content());
wp_reset_postdata();
return $lyrics;
} else {
return 'Lyrics not found.';
}
});
What I’ve Tried So Far:
Direct Use of Merge Tag in the Template:
When I manually replace {select-2} with the exact title (e.g., [test_song_lyrics title="The Lord's Prayer"]), the post content is retrieved successfully.
When I use [test_song_lyrics title="{select-2}"], the debug log shows that the {select-2} value is not resolved and is passed as-is to the shortcode function.
Merge Tag Parsing in Shortcode:
I’ve modified my shortcode to process merge tags using apply_filters('e2pdf_extension_render_shortcodes', $title). This ensures merge tags like {select-2} are processed before querying for the post. Despite this, {select-2} remains unresolved when passed in the shortcode attribute.
Testing Merge Tag Outside the Shortcode:
I added {select-2} directly into the template without the shortcode. In this case, it resolves correctly to The Lord's Prayer.
Default Shortcode Functionality:
A simpler version of the shortcode without the {select-2} attribute (e.g., [test_song_lyrics]) works fine and returns the hardcoded post content.
Questions for Support:
Does E2Pdf support using merge tags like {select-2} in shortcode attributes?
Is there a specific way to configure E2Pdf to resolve merge tags before passing them as shortcode attributes?
Are there any known limitations or additional steps required to enable this functionality?
Any insights or guidance would be greatly appreciated. Thank you!
Thanks so much.
6 December, 2024 07:28:43
E2Pdf
Support
Topics: 7
Messages: 3345
Hello,
Unfortunately, by default, merge tags cannot be used as "shortcode" attributes for security reasons. However, you can modify the code to achieve this:
* a e2pdf_extension_render_shortcodes_tags filter is required to allow the shortcode to execute.
** the shortcode has been modified to get the value from "nested" content rather than as an attribute.
The usage of the shortcode should be modified to:
[test_song_lyrics]{select-2}[/test_song_lyrics] We would really appreciate your feedback at WordPress.org!
We use tools, such as cookies, to enable essential services and functionality on our site and to collect data on how visitors interact with our site, products and services. By accepting cookies you are agreeing to our Cookie Policy.