• Welcome to the all-new Kolakube support, the official place to get help for Marketers Delight, XFtoWP, CryptoWP, and other WordPress products! Please login with your account details to access members-only support forums.

Call sync function

joska1993

Active member
XFtoWP
Messages
32
Reaction score
1
I think my question is basic, but I would like to develop based on this question.

Is there an example of calling sync with a hook?

I need to sync a xenforo profile when the hook: "xprofile_avatar_uploaded" is executed

thanks
 
Last edited:

Alex

MD developer
Staff member
Md
XFtoWP
Messages
6,762
Reaction score
1,917
Hi @joski apologize for the late response! I will post some more information when I get to the computer but I recommend you look through the XFWP/integrations/ folder for direct code examples on adding sync functions to WP hooks.
 
Comment

joska1993

Active member
XFtoWP
Messages
32
Reaction score
1
Hi @joski apologize for the late response! I will post some more information when I get to the computer but I recommend you look through the XFWP/integrations/ folder for direct code examples on adding sync functions to WP hooks.

I've been researching and I managed to create this...

But I need to sync only the user's "url_avatar" custom field. I don't need to update the email. How can I do it?

I haven't seen an example on how to sync a custom field.

thanks @Alex ;)

PHP:
<?php


class XFWP_avatar_Integration extends XFWP_Integration {

    /**
     * Fire actions to plugin hooks and filters.
     *
     * @since 1.4.1
     */

    public function init() {
        add_action( 'xprofile_avatar_uploaded', 'avatar_upload_dir');
    }

    public function avatar_upload_dir( $user_id = 0 ) {
        $wp_user = new WP_User( $user_id );
        $this->users->update_user( $user_id, array(
            'email' => sanitize_email( $wp_user->user_email ),
            'action' => 'edit_email'
        ) );
    }

}

new XFWP_avatar_Integration;
 
Comment

Alex

MD developer
Staff member
Md
XFtoWP
Messages
6,762
Reaction score
1,917
Hi @joska1993, sorry for the late reply but I am back and happy to help you learn how to use the developer tools.

Regarding custom user fields, you can extend the XFtoWP interface to add your own custom user fields into the sync process. See this tutorial:

 
Comment

joska1993

Active member
XFtoWP
Messages
32
Reaction score
1
Hi @joska1993, sorry for the late reply but I am back and happy to help you learn how to use the developer tools.

Regarding custom user fields, you can extend the XFtoWP interface to add your own custom user fields into the sync process. See this tutorial:


Yes, I already have that created. I need to sync a user's profile when an event happens.

In my case the event is: "xprofile_avatar_uploaded"

What would be the function to synchronize the entire profile of that specific user?

So far I have this:

PHP:
<?php


class XFWP_avatar_Integration extends XFWP_Integration {

    /**
     * Fire actions to plugin hooks and filters.
     *
     * @since 1.4.1
     */

    public function init() {
        add_action( 'xprofile_avatar_uploaded', 'avatar_upload_dir');
    }

    public function avatar_upload_dir( $user_id = 0 ) {
        $wp_user = new WP_User( $user_id );
        $this->users->update_user( $user_id, array(
            'email' => sanitize_email( $wp_user->user_email ),
            'action' => 'edit_email'
        ) );
    }

}

new XFWP_avatar_Integration;

But this updates the email. I need to refresh the complete profile.
 
Comment

Alex

MD developer
Staff member
Md
XFtoWP
Messages
6,762
Reaction score
1,917
Hi @joska1993, I looked at this last night and am going to be making a change which I want to release for you that will make the update_user method safe for you to set your custom action to. I'm close and ready to help you get past this finally.

Currently, this method only takes actions defined by the core plugin, as you found by the 'action' => 'edit_email' parameter. I will have an update here shortly.
 
Comment

joska1993

Active member
XFtoWP
Messages
32
Reaction score
1
OK thank you! I look forward to those changes.

Also, would there be an option to sync all users 1 time per day via CRON?
 
Comment

Alex

MD developer
Staff member
Md
XFtoWP
Messages
6,762
Reaction score
1,917
Hi @joska1993, I have been working on this and made some cool developer's tweaks for the next RC version. In the meantime, you be able to do what you are asking without the update.

The class below is derived from your example in this thread, with the difference being the filters to add the avatar custom user field have been grouped into the Class as well for better organization. So you can remove the functions you are already using and just use this Class instead.

Finally, I changed the update_user action to use the sync_user action, which will run a full profile sync and hooked it into the xprofile_avatar_uploaded hook you want to use.

The last thing you will probably need to do is set the dynamic avatar URL in the set_custom_user_fields method.

Thank you for your patience and let me know if this helps you. For the longer than usual support time I have also credited your license +1 month for renewals.

PHP:
/**
 * Add Avatar URL to custom user fields and update user
 * on specific action.
 *
 * @since 1.0
 */

class XFWP_Avatar_Integration extends XFWP_Integration {

    /**
     * Fire actions to plugin hooks and filters.
     *
     * @since 1.0
     */

    public function init() {
        add_action( 'xprofile_avatar_uploaded', array( $this, 'update_user_avatar' ) );
        add_filter( 'xfwp_custom_fields', array( $this, 'add_custom_user_fields' ) );
        add_filter( 'xfwp_custom_user_fields_data', array( $this, 'set_custom_user_fields' ), 10, 4 );
    }

    /**
     * Register field to XFtoWP + adds to Custom fields select box
     *
     * @since 1.0
     */
   
    public function add_custom_user_fields( $custom_fields ) {
        $custom_fields['user']['url_avatar'] = array(
            'label' => 'Custom avatar URL'
        );
        return $custom_fields;
    }
   
    /**
     * Set dynamic value to custom user field as set in add_custom_user_fields() method.
     *
     * @since 1.0
     */
   
    public function set_custom_user_fields( $custom_fields, $wp_user, $wp_usermeta, $post_id ) {
        $custom_fields['url_avatar'] = 'https://domain.com/avatar5.png';
        return $custom_fields;
    }

    /**
     * Update XF user when action is run.
     *
     * @since 1.0
     */

    public function update_user_avatar( $wp_user_id ) {
        $wp_user = new WP_User( $wp_user_id );
        $this->users->update_user( $wp_user_id, array(
            'email' => sanitize_email( $wp_user->user_email ),
            'action' => 'sync_user'
        ) );
    }

}

new XFWP_Avatar_Integration;
 
Comment
Top