Skip to:
Content
Pages
Categories
Search
Top
Bottom

xprofile_get_field_data() throwing a Fatal error


  • juliendumont
    Participant

    @juliendumont

    Hello everyone,

    I am sure the problem is quite basic, but I can’t fix it. On my site, some users don’t have their email in extended profile, while there is in their wordpress profile. I want to take it from their wordpress profile and put it in their extended one.
    I wrote this little piece of code to do so, that I run in the Code Snippet plugin of WordPress :

    function mettre_a_jour_tous_les_profils() {
        $users = get_users();
    
        foreach ($users as $user) {
            $user_id = $user->ID;
            $user_data = get_userdata($user_id);
            $user_email = $user_data->user_email;
    
            $current_email = xprofile_get_field_data(204, $user_id);
    
            // Vérifier si l'email du profil étendu est vide
            if (empty($current_email)) {
                // Mettre à jour l'email du profil étendu avec l'email du profil WordPress
                xprofile_set_field_data(204, $user_id, $user_email);
            }
        }
    }
    mettre_a_jour_tous_les_profils();

    Here is the erro i get :

    PHP Fatal error:  Uncaught Error: Call to undefined function xprofile_get_field_data() in /homepages/14/d736706778/htdocs/IMABIO/wp-content/plugins/code-snippets/php/snippet-ops.php(575) : eval()'d code:9
    Stack trace:
    #0 /homepages/14/d736706778/htdocs/IMABIO/wp-content/plugins/code-snippets/php/snippet-ops.php(575) : eval()'d code(32): mettre_a_jour_tous_les_profils()
    #1 /homepages/14/d736706778/htdocs/IMABIO/wp-content/plugins/code-snippets/php/snippet-ops.php(575): eval()
    #2 /homepages/14/d736706778/htdocs/IMABIO/wp-content/plugins/code-snippets/php/snippet-ops.php(656): Code_Snippets\execute_snippet()
    #3 /homepages/14/d736706778/htdocs/IMABIO/wp-includes/class-wp-hook.php(308): Code_Snippets\execute_active_snippets()
    #4 /homepages/14/d736706778/htdocs/IMABIO/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters()
    #5 /homepages/14/d736706778/htdocs/IMABIO/wp-includes/plugin.php(517): WP_Hook->do_action()
    #6 /homepages/14/d736706778/htdocs/IMABIO/wp-settings.php(486): do_action()
    #7 /homepages/14/d736706778/htdocs/IMABIO/wp-config.php(99): require_once('/homepages/14/d...')
    #8 /homepages/14/d736706778/htdocs/IMABIO/wp-load.php(50): require_once('/homepages/14/d...')
    #9 /homepages/14/d736706778/htdocs/IMABIO/wp-admin/admin-ajax.php(22): require_once('/homepages/14/d...')
    #10 {main}
      thrown in /homepages/14/d736706778/htdocs/IMABIO/wp-content/plugins/code-snippets/php/snippet-ops.php(575) : eval()'d code on line 9

    If some clever minds can tell me where am i wrong, it would definitely help me, thank you !

Viewing 1 replies (of 1 total)

  • VibeThemes
    Participant

    @vibethemes

    Your function mettre_a_jour_tous_les_profils(); needs to be called at some action. Currently your code executes at the time when the files are being loaded so the functions are not defined.
    Further more your code gets called for all the users, so I assume you just want to run it once and then remove it.

    replace your direct function call : mettre_a_jour_tous_les_profils(); with
    add_action(‘xprofile_field_after_save’,’mettre_a_jour_tous_les_profils’);

    If you want to run for all users just once : replace xprofile_field_after_save wtih “init”

    So now your code runs when a user saves the profile field but it is still not optimised at all.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar