表单提交后Wordpress页面变为空白

时间:2021-04-26 16:49:06

标签: php wordpress forms

我正在学习使用高级自定义字段创建表单以更新用户个人资料的教程。当我按下提交按钮时,页面变为空白(白色)。也许你们可以看到下面的一行代码导致了问题。该教程说它告诉 Wordpress 它正在更新用户而不是页面。

我希望页面重新加载,或者在提交后重定向到另一个页面。

教程参考:https://usersinsights.com/acf-user-profile/

这段代码在functions.php中。它转换短代码并提交数据,它会更新,但提交后页面上没有任何内容,它只是空白。


    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 );

1 个答案:

答案 0 :(得分:0)

感谢 John Tyner 打开 wp_debug_display 的提示,我发现问题与添加到 wp_head 的代码有关。网上查了一下,从一篇ACF文章中发现acf_form_head()需要放在header之前。

变化:

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

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

它有效! :)