Home Forums Backend Issues (wp-admin) Shortcode attributes to set options


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');
    	// 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.
    		// start organizing the form.
    		$fields = get_field_objects();
    		// 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.

  • @john

    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')) {
    // everything else...
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Shortcode attributes to set options’ is closed to new replies.