How to Sync Vendor Social Links on Listing Page

Description:
I searched through the HivePress community forum, but couldn’t find a perfect answer to my problem. I tried several code snippets and methods, but only after some trial and error did I finally come up with a perfect solution for syncing vendor social links on the listing page. Syncing basic attributes is easy, but syncing social link details (such as those from the plugin) isn’t achievable with the default settings.

Here’s how I did it.

Solution:

  1. Install the Social Links Plugin
    First, ensure that the Social Links plugin is installed and activated.

  2. Configure Social Links in Vendor Settings
    Go to HivePress > Settings > Vendors and configure the social links you want for the vendor profile in the Social Links section. This will allow the vendor to input their social media links. (See the screenshot for reference.)

Vendor Profile Setting

  1. Choose Social Links for Display on Listing Page
    Next, go to HivePress > Settings > Listings and select the social links you want to display on the listing page in the Social Links section. (Refer to the screenshot here.)

Once the plugin and settings are configured, you’ll need to add a simple PHP code snippet in your functions.php to fetch and display the vendor’s social media links on the listing page.

add_filter( 
    'hivepress/v1/models/listing/attributes', 
    function( $attributes ) { 
        $fields = ['whatsapp', 'facebook']; // List of attributes to modify

        foreach ( $fields as $field ) { 
            if ( isset( $attributes[ $field ] ) ) { 
                $attributes[ $field ]['synced'] = true; 
            } 
        } 

        return $attributes; 
    }, 
    1000 
);

What Happens After Adding the Code:

Once you’ve added the code, you will now see the synced data in the Listing Submit Form (check the screenshot).

Boom! :rocket:
Now, when a vendor submits the form, their social links will be displayed like this on the listing page (see the screenshot).

Bonus Trick 1: Show Only WhatsApp Button on Listing Page

Want to display only the WhatsApp button on the listing page? Simple. Just use this code snippet in functions.php:

add_filter( 
    'hivepress/v1/models/listing/attributes', 
    function( $attributes ) { 
        $fields = ['whatsapp']; // List of attributes to modify

        foreach ( $fields as $field ) { 
            if ( isset( $attributes[ $field ] ) ) { 
                $attributes[ $field ]['synced'] = true; 
            } 
        } 

        return $attributes; 
    }, 
    1000 
);

After that, go to HivePress > Settings > Listings > Social Links and change the icon to Button (refer to the screenshot).

Bonus Trick 2: Hide Social Links in the Listing Form, but Sync Them for Display

If you want to hide the social links in the listing form and prevent vendors from editing them, while still syncing and displaying the links on the listing page, simply add this code:

$attributes[ $field ]['editable'] = false; // Prevent editing 

Full Code

add_filter( 
    'hivepress/v1/models/listing/attributes', 
    function( $attributes ) { 
        $fields = ['whatsapp', 'facebook']; // List of attributes to modify

        foreach ( $fields as $field ) { 
            if ( isset( $attributes[ $field ] ) ) { 
                $attributes[ $field ]['synced'] = true; 
                $attributes[ $field ]['editable'] = false; // Prevent editing 
            } 
        } 

        return $attributes; 
    }, 
    1000 
);

This will hide the social link fields in the Listing Submit Form (check the screenshot) but will sync the data and display it on the listing page correctly.

Conclusion:

There you have it! You can now sync vendor social links to their listings and customize the display however you want using these simple PHP code snippets.

If you found this helpful, show some :heart: and share your thoughts in the comments! :blush:

2 Likes

Thanks for sharing!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.