Dynamic text inside the form inside a shortcode with id parameter
-
On a WordPress powered site, I have a custom post type called “Free Stuff”. This CPT is used to register ebooks and other materials that can be downloaded by the user. It has a custom field created with ACF called “download_link”, where the administrator registers a URL to download the ebook.
On the post page, to download, the user fills in a CF7 form that has a hidden Dynamic Text Extension field that is filled with the “download_link”. In the email sent to the user by CF7, the link is available for download. I use the field like this: [dynamichidden download_link “acf field=’download_link'”]
This is working perfectly.
Now I have the following challenge. I want to generate a shortcode that allows inserting a kind of widget referring to this post inside other regular posts.
Using a custom function I created a new shortcode called [free_stuff] with a parameter called “id”, eg [free_stuff id=”1234″]. This shortcode generates an HTML with a title, a description and also the download form. In the id parameter I put the post id of “Free Stuff” and paste the shortcode in the post to be displayed. Below is the code that generates the shortcode:
function material_gratuito_shortcode($atts) { $attributes = shortcode_atts(array( 'id' => '', ), $atts); $id = $attributes['id']; if (empty($id)) { return 'Por favor, forne?a um ID válido.'; } $html = '<div style="margin-bottom: 1rem!important; padding: 1rem!important; background: var(--color-content-bg-alt)!important;">'; $html .= '<h3 style="text-align:center!important;font-weight:700;">'; $html .= do_shortcode('[acf field="form_titulo" post_id="' . $id . '"]'); $html .= '</h3>'; $html .= '<p style="text-align:center;margin-bottom:1rem;">'; $html .= do_shortcode('[acf field="form_descricao" post_id="' . $id . '"]'); $html .= '</p>'; $html .= do_shortcode('[contact-form-7 id="75206" title="Formulário para o blog"]'); $html .= '</div>'; return $html; } add_shortcode('free_stuff', 'material_gratuito_shortcode');
As you can see, it will generate an HTML filled with some custom fields from the ACF of the selected post by ID. Furthermore it generates a CF7 form.
The only problem is that inside this generated form, I need that instead of being
[dynamichidden download_link "acf field='download_link'"]
, it should be[dynamichidden download_link "acf field='download_link' post_id='1234'"]
, being the ID number replaced by the top shortcode id.I’ve tried several things, but I can’t get it to work. Can you help me with this?
- The topic ‘Dynamic text inside the form inside a shortcode with id parameter’ is closed to new replies.