Issue with Using E2Pdf Merge Tag in Shortcode Attributes

Forminator Forms
6 December, 2024 06:30:58
RussC
Topics: 1
Messages: 13
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."

My Code Running in Snippets, all locations:

add_shortcode('test_song_lyrics', function($atts) {
$atts = shortcode_atts(['title' => ''], $atts);

// 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: 3413
Hello,

Unfortunately, by default, merge tags cannot be used as "shortcode" attributes for security reasons. However, you can modify the code to achieve this:

https://codefile.io/f/CdqL45I2C1 *

* 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!
6 December, 2024 10:18:58
RussC
Topics: 1
Messages: 13
Thank you so much, looking great so far.
20 January, 2025 12:20:27
RussC
Topics: 1
Messages: 13
Due to a design specification change I now have to us a field group to allow addition of sections to the forminaor form.

How would I go about integtaing these as shortcodes into a dynamic length pdf? Thanks so much. I have tried a few ideas but no luck (I have not tried anything worth documenting here).


Thanks
20 January, 2025 12:20:29
RussC
Topics: 1
Messages: 13
Due to a design specification change I now have to us a field group to allow addition of sections to the forminaor form.

How would I go about integtaing these as shortcodes into a dynamic length pdf? Thanks so much. I have tried a few ideas but no luck (I have not tried anything worth documenting here).


Thanks
20 January, 2025 23:57:52
E2Pdf
Support
Topics: 7
Messages: 3413
Hello,

Do you need to achieve the same behavior but modify the group field value during iteration? If so:

1. You need to update the plugin to the latest RC version (1.27.05), which includes a fix for the Forminator Group Fields.. You can follow the steps outlined here to update: https://e2pdf.com/docs/install#rc-version.

2. Use the shortcode in the iteration the same way, but within the "foreach" loop: https://codefile.io/f/DV7O2IbuBT

* where "group-1" is your gorup field and "select-2" is a subfield in the "group-1".

We would really appreciate your feedback at WordPress.org!
3 February, 2025 03:08:27
RussC
Topics: 1
Messages: 13
Thanks this is great. It works mostly well except for a bug where a random pos is showing when I use multiple instances in a loop.

Next step is to look at figuring out 2 per page double sided printing best tostada a new post for that ?
3 February, 2025 03:43:34
E2Pdf
Support
Topics: 7
Messages: 3413
Could you please attach some screenshots showing the bug where a random position is appearing? Also, share the code via codefile.io that is used in the "Value" field.

>>> Next step is to look at figuring out 2 per page double sided printing best tostada a new post for that ?

We’re not entirely sure what you mean. Do you need the post content to appear on the left side, continue on the right side, and then move to the next page in the same pattern—left side first, then right side?
We would really appreciate your feedback at WordPress.org!
3 February, 2025 06:40:34
RussC
Topics: 1
Messages: 13
Thanks for your response!

1. I've a attached screenshot of the formulator form showing the issue where unwanted posts are being added to the output PDF. Primarily the text starting:

"Brothers and Sisters:
Christ has been raised from the dead,
the firstfruits of those who have fallen asleep.
For since death came through a human being,"

Additionally, you can find the code used in the Value field here: https://codefile.io/f/p4nsTM6h1w.

The issue is that the output PDF is including random posts that shouldn’t be there, as shown in the attached screenshots. Do you have any insights into why this might be happening?

2. For the booklet formatting, I need the PDF output to be A5-sized so it can be printed as a booklet. This means having two A5 pages per A4 sheet, with double-sided printing, rather than just alternating page directions. The final layout should allow proper booklet assembly, which depends on the total number of pages.
3 February, 2025 07:46:54
E2Pdf
Support
Topics: 7
Messages: 3413
1. We believe the issue can lie in the [test_song_lyrics] shortcode, which outputs the post content. Could you please debug it and check what values your shortcode receives and what it outputs? It seems like it's not correctly pulling the posts.

2. Do you have a PDF example so we can see the structure you need?


We would really appreciate your feedback at WordPress.org!
3 February, 2025 09:07:55
RussC
Topics: 1
Messages: 13
Thanks, I have debugged the code and it seems that the content that is brought in initially is the desired post, then a second post is added after that..the second post appears to be the 1st post that is found in alpha numeric title order.This happens in the exported e2pdf pdf file....however when the same shortcode is added to a page the searched post is not served up, just the first post from the list of posts. hope that makes sense.

I do not have a pdf example. Because the pages a created with multipage so the pdf can expand to any size. However the image shows what I am talking about.
3 February, 2025 11:56:49
E2Pdf
Support
Topics: 7
Messages: 3413
1. It seems there can be some conflict due to your shortcode using the global post in loop, which doesn't drop the value correctly. You can try rebuilding your shortcode with the following to avoid overwriting the global post: https://codefile.io/f/r5mwdHHL7d.

2. Thank you for the clarification. Unfortunately, E2Pdf does not have the ability to output pages as shown in the described image. You can try building the pages separately; however, if your pages are dynamic, it would not be a trivial task. We have added the point to consider adding this functionality for review, but there is no ETA at the moment, as our team needs to review how complex the implementation would be.
We would really appreciate your feedback at WordPress.org!
4 February, 2025 04:05:02
RussC
Topics: 1
Messages: 13
1.Thanks so much I will try the code modification and get back to you.

2. Thanks yes it is a tricky algorithm to implement and as you say it can't be done with text areas flowing into each other. I think this would definitely be an interesting feature to implement in the future into e2pdf.

4 February, 2025 07:36:25
RussC
Topics: 1
Messages: 13
This looks like it is working well so far. I have this code in the template:

{foreach:group-6}
</br>
{select-13}
</br>
[test_song_lyrics]{select-7}[/test_song_lyrics]
</br>
[test_song_lyrics]{select-8}[/test_song_lyrics]
</br>
[test_song_lyrics]{select-2}[/test_song_lyrics]
</br>
[test_song_lyrics]{select-9}[/test_song_lyrics]
</br>
[test_song_lyrics]{select-10}[/test_song_lyrics]
</br>
[test_song_lyrics]{select-12}[/test_song_lyrics]
</br>
{textarea-8}
</br>
{name-3}
</br>
[test_song_lyrics]{select-11}[/test_song_lyrics]
</br>
{textarea-2}
</br>
{textarea-3}
</br>
{/foreach:group-6}

I had to change the snippet to identify if the returned value was empty and if so point it towards a post named empty. This seems to have solved the issue (though I may be wrong and it may not be the solution).


Here is the snippet code https://codefile.io/f/cdYZFiiDg8
// allow test_song_lyrics shortcode to be processed inside E2Pdf Template
add_filter('e2pdf_extension_render_shortcodes_tags', function ($shortcode_tags) {
$shortcode_tags[] = 'test_song_lyrics';
return $shortcode_tags;
});

add_shortcode('test_song_lyrics', function ($atts, $title) {

if (!function_exists('post_exists')) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}
// Set a default title if $title is empty
if (empty($title)) {
$title = 'Empty'; // Change this to your specific post title
}
// Get Post ID by Title
$post_id = post_exists($title, '', '', 'post');

if ($post_id) {
return apply_filters('the_content', get_post_field('post_content', $post_id));
} else {
return 'Lyrics not found.';
}
});


This seems to allow items in groups to be added and then displayed in order.

The next step I guess is to add a page break. I am not sue if this is something atet can be implemented in e2pdf and my cursory reading of the documentation hasn't revealed it. If you happen o know the answer it would be much appreciated. Again thanks again support here is possibly the best I've experienced.
4 February, 2025 08:19:11
RussC
Topics: 1
Messages: 13
looks like

<span style="page-break-before: always;"></span>

works perfectly in the HTML block
4 February, 2025 08:42:14
RussC
Topics: 1
Messages: 13
actually this span tag has to be in the end of the post, I tried adding it to the template but it repeats even if a shorcode insert is empty, I tried adding it to the snippet and again it seems to add even if the snippet is empty.


putting it at the end of the post seems to work well
4 February, 2025 08:53:57
RussC
Topics: 1
Messages: 13
this snippet will add the page breaks it seems

https://codefile.io/f/WPdZP2Odfd
4 February, 2025 09:02:58
E2Pdf
Support
Topics: 7
Messages: 3413
Glad to hear the solution worked for you! Yes, using page-break-before: always; is the correct approach when you need to create pages dynamically.

>>> 2. Thanks yes it is a tricky algorithm to implement and as you say it can't be done with text areas flowing into each other. I think this would definitely be an interesting feature to implement in the future into e2pdf.

We agree, which is why we've marked the feature for implementation. We will notify you as soon as there are any updates. However, we can't provide an ETA yet, as there are technical challenges in ensuring it works correctly when there are fewer pages than required for printing, along with the complexities of dynamic page creation.
We would really appreciate your feedback at WordPress.org!