简体   繁体   中英

Wordpress page goes blank after form submit

I'm following a tutorial that creates form with Advanced Custom Fields that updates the user profile. When I press the submit button the page turns blank (white). Maybe you folks can see a line of code below that is causing the trouble. The tutorial says it tells Wordpress that it's updating a user not the page.

I'd like the page to reload, or maybe redirect to another page after it submits.

Tutorial Ref: https://usersinsights.com/acf-user-profile/

This code is in the functions.php. It converts the shortcode and submits the data, which does update, but after it submits the page has nothing on it, it's just blank.


    function my_acf_user_form_func( $atts ) {
     
    
      $a = shortcode_atts( array(
        'field_group' => ''
      ), $atts );
     
      $uid = get_current_user_id();
      
      if ( ! empty ( $a['field_group'] ) && ! empty ( $uid ) ) {
        $options = array(
          'post_id' => 'user_'.$uid,
          'field_groups' => array( intval( $a['field_group'] ) ),
          'return' => add_query_arg( 'updated', 'true', get_permalink())
        );
        
        ob_start();
        
        acf_form( $options );
        $form = ob_get_contents();
        
        ob_end_clean();
    
      }
      
        return $form;
        
    
    }
     
    add_shortcode( 'my_acf_user_form', 'my_acf_user_form_func' );
    
    //adding AFC form head
    function add_acf_form_head(){
        global $post;
        
      if ( !empty($post) && has_shortcode( $post->post_content, 'my_acf_user_form' ) ) {
            acf_form_head();
        }
    }
    add_action( 'wp_head', 'add_acf_form_head', 7 );

Thanks to John Tyner's tip to turn on wp_debug_display I found the problem had something to do with the code being added to the wp_head. I searched online and found out from an ACF article that acf_form_head() needs to go before the header.

Change:

add_action( 'wp_head', 'add_acf_form_head', 7 );

to

add_action( 'get_header', 'add_acf_form_head', 0 );

It works: :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM