Home › Forums › Backend Issues (wp-admin) › Shortcode attributes to set options
Is it possible to pass shortcode attributes to acf_form($options)
? It seems like I’m running into a race condition. For example using the shortcode [form_generator submit="Make a Request"]
, I get different results depending on which function I var_dump from. Ideally the submit
attribute would get passed to the $options
array and then to acf_form()
.
What I’m trying is something like this…
Using v4
// request-form.php - my plugin
function output_acf_form($submit) {
// find the location of the plugin.
$acf_base = plugin_basename('/advanced-custom-fields/acf.php');
var_dump($submit);
// check to see if acf is active.
if (is_plugin_active($acf_base)) {
// make sure that acf has access to the site before doing anything else.
acf_form_head();
// start organizing the form.
$fields = get_field_objects();
$fieldGroup;
// make sure that the $key has a value and save it.
foreach ($fields as $key => $field) {
if ($key) {
$fieldGroup = $field['field_group'];
}
}
// store the options for the form.
$options = array(
'post_id' => 'new_post',
'field_groups' => array($fieldGroup),
'submit_value' => $submit,
'return' => home_url('thank-you'),
// 'updated_message' => 'I tried to redirect'
);
// prepare to return the content of the form.
$content = acf_form($options);
return $content;
} else {
echo "Something broke";
}
}
function output_acf_form_shortcode($atts, $content = null) {
$opts = shortcode_atts(array(
// other options may get passed in here...
'submit' => ''
), $atts);
$submit = $opts['submit'];
$content = output_acf_form($submit);
return $content;
}
add_shortcode('form_generator', 'output_acf_form_shortcode');
The only problem I can see with what you’re trying to do is that acf_form_head() must be called before any output is generated for the site. This function must be called before get_header() is called. When you call this function from a shortcode then this function is being called too late. Other than that, I don’t see anything in the code that you provided that might cause a problem.
Thanks for the help. I was able to get around having to hardcode acf_form_head()
into my theme by doing this (more or less) in the plugin.
function output_acf_form($args) {
global $post;
if (has_shortcode($post->post_content, 'my_shortcode')) {
acf_form_head();
}
// everything else...
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.