Support

Account

Home Forums Front-end Issues Outputting ACF Form from a shortcode in WYSIWYG editor

Solved

Outputting ACF Form from a shortcode in WYSIWYG editor

  • I am trying to output acf_form() from a shortcode.

    function zUpdate( $atts, $content = null ) {
    	
    	$current_id = get_current_user_id();
    	$user_id = 'user_' . $current_id;	
    	
    	$options = array(
    	    'post_id' => $user_id,
    	    /* my field group grabs unit, home phone, and cell phone acf */
    	    'field_groups' => array('3307', ),
    	    'submit_value' => 'Update' 
    	);
    	$form = acf_form( $options );
    	return $form;	
    
    }

    when I do this it adds it above or outside the WYSIWYG editor on front end. Should I not return $form and do this some other way. For example I put the shortcode in the WYSIWYG editor in a section tag. it never showed up inside the section but above it. this might be me fundamentally not understanding how shortcodes work if so I apologize.

  • The problem is that ACF outputs the form when the shortcode is run and what you need it to do is return the value. To do this you use a PHP output buffer

    
    // before you generate form
    ob_start();
    	
    	$current_id = get_current_user_id();
    	$user_id = 'user_' . $current_id;	
    	
    	$options = array(
    	    'post_id' => $user_id,
    	    /* my field group grabs unit, home phone, and cell phone acf */
    	    'field_groups' => array('3307', ),
    	    'submit_value' => 'Update' 
    	);
    	acf_form( $options );
    
    // after your code
    $form = ob_get_clean();
    return $form;
    
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Outputting ACF Form from a shortcode in WYSIWYG editor’ is closed to new replies.