Php 试图在wordpress中将数组附加到用户\元

Php 试图在wordpress中将数组附加到用户\元,php,wordpress,Php,Wordpress,我正在wordpress上为前端做一个“编辑配置文件”。我做到了我想要的一切,除了一件事:我希望用户能够从这个页面添加专业的“体验”(比如linkedin)。 我为用户创建了一个名为“体验”的元,我想在其中创建一个数组,其中包含每个体验的团队、角色和描述。因此,最终的结果将是经验元为每个经验包含一个对象/数组 以下是我尝试过的: <?php function my_show_extra_profile_fields( $user ) { ?> <h3>Pseu

我正在wordpress上为前端做一个“编辑配置文件”。我做到了我想要的一切,除了一件事:我希望用户能够从这个页面添加专业的“体验”(比如linkedin)。 我为用户创建了一个名为“体验”的元,我想在其中创建一个数组,其中包含每个体验的团队、角色和描述。因此,最终的结果将是经验元为每个经验包含一个对象/数组

以下是我尝试过的:

<?php

function my_show_extra_profile_fields( $user ) { ?>

    <h3>Pseudo in-game</h3>

    <div class="form-table">

        <div>
            <div>
                <input type="text" name="ID_inGame" id="ID_inGame" value="<?php echo esc_attr( get_the_author_meta( 'ID_inGame', $user->ID ) ); ?>" class="regular-text" /><br/>
                <input type="text" name="biography_custom" id="biography_custom" value="<?php echo esc_attr( get_the_author_meta( 'biography_custom', $user->ID ) ); ?>" class="regular-text" /><br/>
                <input type="date" name="date_de_naissance" id="date_de_naissance" value="<?php echo esc_attr( get_the_author_meta( 'date_de_naissance', $user->ID ) ); ?>" class="regular-text" /><br/>
                <input type="text" name="experiences" id="experiences" value="<?php echo esc_attr( get_the_author_meta( 'experiences', $user->ID ) ); ?>" class="regular-text" /><br/>
            </div>
        </div>

    </div>
<?php }

function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    if (!empty($_POST['first_name'])) {
        update_usermeta( $user_id, 'first_name', $_POST['first_name'] );
    } else {
        update_usermeta( $user_id, 'first_name', "Prénom non renseigné" );
    };

    if (!empty($_POST['last_name'])) {
        update_usermeta( $user_id, 'last_name', $_POST['last_name'] );
    } else {
        update_usermeta( $user_id, 'last_name', "Nom non renseigné" );
    };

    if (!empty($_POST['ID_inGame'])) {
        update_usermeta( $user_id, 'ID_inGame', $_POST['ID_inGame'] );
    } else {
        update_usermeta( $user_id, 'ID_inGame', "Pseudo non renseigné" );
    };

    if (!empty($_POST['biography_custom'])) {
        update_usermeta( $user_id, 'biography_custom', $_POST['biography_custom'] );
    } else {
        update_usermeta( $user_id, 'biography_custom', "Aucune biographie renseignée" );
    };

    if (!empty($_POST['date_de_naissance'])) {
        update_usermeta( $user_id, 'date_de_naissance', $_POST['date_de_naissance'] );
    } else {
        update_usermeta( $user_id, 'date_de_naissance', "Age non renseigné" );
    };

}

伪游戏内

不确定它是否回答了您的问题,但下面是如何访问
$experience
变量标准方式

//declare this function outside of edit_user_profile_custom() function
function push_experience() {
    $experience = array();

    if(isset($_POST['experience_team'])){
      array_push($experience, $_POST['experience_team']);
    }

    if(isset($_POST['experience_role'])){
      array_push($experience, $_POST['experience_role']);
    }

    if(isset($_POST['experience_description'])){
      array_push($experience, $_POST['experience_description']);
    }
   return $experience;
}
然后在
edit\u user\u profile\u custom()函数中使用此代码

if(isset($_POST['button1'])) {
    $experience = push_experience();
    if(!empty($experience)){
      array_push($data['experiences'], $experience);
    }
    my_save_extra_profile_fields( $current_user_id );
};

为什么要在另一个函数中声明一个函数
push_experience()
&
$experience
push_experience()
功能范围内。。如果没有
global
或reference,您无法访问它…@SajjadHossainSagor我承认我完全是从后端开始的,所以这可能看起来很愚蠢。。。我只需将push_experience()放在edit_user_profile_custom()之外,然后通过使用全局变量调用$experience即可?ThanksOh perfect似乎有效,唯一的问题是它不会从一个保存到另一个保存,知道为什么吗?非常感谢,你帮了初学者一个大忙!不确定你遇到了什么样的问题。。。。是否不保存用户数据?或者别的什么?我想我通过添加一个更新元数据的函数找到了解决方案:)现在我要尝试解决的唯一问题是在数组中的同一级别进行每次推送!谢谢;)
if(isset($_POST['button1'])) {
    $experience = push_experience();
    if(!empty($experience)){
      array_push($data['experiences'], $experience);
    }
    my_save_extra_profile_fields( $current_user_id );
};