Display an advertiser attribute on an ad

Hello, I am in the process of redoing the ad model to add some modifications and among these I have been trying for some time to recover an attribute from the advertiser profile to make a button but to no avail.

Let me explain: each user has the possibility of giving their WhatsApp or messenger number retrieved under “hp_whatsapp” and “hp_messenger” in meta_key.
As soon as I pass it into shortcode I either crash Wordpress or the data is not recovered even though the ID is recovered.

image

Salut !

First things first, did you try to display your attribute “the normal way”, by specifying an area ?

Cheers !

yes this is what I currently have.

but I notice that the rate of contact between users is dropping due to the outdated messaging service, so I d’ like to give more importance to external contacts by putting 2 other buttons at the same level, such as the simple “reply”.

Hi,

Please specify which shortcode you mean. You can add a WhatsApp button, for example, using this documentation: How to add a WhatsApp button - HivePress Help Center. Also, you can simply add a Reply button by installing the Messages extension.

Hi I actually already have a whatsapp button, I am looking to modify the template in this one the seller profile will no longer be visible so inserting a whatsapp button in this one will be of no use.
I already have the message plugins I am looking to make 2 other identical buttons below this one for more visibility.
My goal is simply to retrieve 2 data from the vendor profile (whatsapp and messenger) already stored in the database and thus create 2 shortcodes example [whatsapp] [messenger] to be able to make buttons on the new template

By using codes found I tested this which returns an empty list

add_shortcode(
	'hivepress_listing_attribute',
	function( $atts ) {
		if ( ! is_singular( 'hp_listing' ) ) {
			return 'Pas une page d\'annonce'; 
		}

		$listing = \HivePress\Models\Listing::query()->get_by_id( get_the_ID() );

		if ( ! $listing ) {
			return 'Pas d\'objet trouvé'; 
		}
		if ( ! isset( $atts['whatsapp'] ) ) {
			$attributes = $listing->get_attributes();

			if ( empty( $attributes ) ) {
				return 'Aucun attribut disponible.';
			}

			$attribute_names = array_keys( $attributes );
			return 'Attribut non défini. Voici les attributs disponibles : ' . implode( ', ', $attribute_names );
		}

		return call_user_func( [ $listing, 'display_' . $atts['whatsapp'] ] );
	}
);

Sorry, I can’t quite understand the desired result, but the shortcode itself seems to be ok - it should display the value of the listing attribute passed to the “whatsapp” shortcode parameter. I recommend using this code snippet, though Attribute as shortcode - #13 by ihor Then you can use this shortcode within the listing page template, e.g. if you have a Messenger attribute (with the field name “messenger”), you can do this:

[hivepress_listing_attribute name="messenger"]

Hope this helps

I’ll explain this with photos, here is what I currently have:


I specify that currently everything works but for the sake of visibility I would like to move on to this:

The problem is that the whatsapp or messenger attribute must be retrieved from the seller profile and not via the ad.

Please try this code snippet instead:

add_shortcode(
	'hivepress_vendor_attribute',
	function( $atts ) {
		if ( ! isset( $atts['name'] ) || ! is_singular( 'hp_listing' ) ) {
			return;
		}

		$listing = \HivePress\Models\Listing::query()->get_by_id( get_the_ID() );

		if ( ! $listing ) {
			return;
		}

		$vendor = $listing->get_vendor();

		if ( ! $vendor ) {
			return;
		}

		return call_user_func( [ $vendor, 'display_' . $atts['name'] ] );
	}
);

When used within the listing template, it retrieves any vendor attribute by name.

1 Like

If anyone is interested in having the differentiated whatsapp/messenger code and explained in the form of a button, here it is thanks to Ihor

add_shortcode(
	'hivepress_vendor_attribute',
	function( $atts ) {
		// Vérifie si le nom de l'attribut est défini et si on est sur une page de type "hp_listing"
		if ( ! isset( $atts['name'] ) || ! is_singular( 'hp_listing' ) ) {
			return;
		}

		// Récupère l'objet de la fiche (listing)
		$listing = \HivePress\Models\Listing::query()->get_by_id( get_the_ID() );

		if ( ! $listing ) {
			return;
		}

		// Récupère le profil du vendeur associé à la fiche
		$vendor = $listing->get_vendor();

		if ( ! $vendor ) {
			return;
		}

		// Récupère la valeur de l'attribut du vendeur
		$attribute_value = call_user_func( [ $vendor, 'display_' . $atts['name'] ] );

		// Si l'attribut existe et n'est pas vide
		if ( $attribute_value ) {
			// Vérifie le nom de l'attribut pour déterminer le type de lien
			if ( strtolower( $atts['name'] ) === 'whatsapp' ) {
				// Génère le lien WhatsApp
				$link = 'https://wa.me/' . esc_attr( $attribute_value );
				$button_text = 'Contacter sur WhatsApp';
			} elseif ( strtolower( $atts['name'] ) === 'messenger' ) {
				// Génère le lien Messenger
				$link = 'https://m.me/' . esc_attr( $attribute_value );
				$button_text = 'Contacter sur Messenger';
			} else {
				// Si l'attribut n'est pas reconnu, retourne un message par défaut
				return '<p>Type de contact non pris en charge.</p>';
			}

			// Retourne le bouton HTML
			return '<a href="' . esc_url( $link ) . '" target="_blank" class="button button--large button--primary hp-button--wide">' . esc_html( $button_text ) . '</a>';
		}

		// Si l'attribut est vide, retourne un bouton spécifique
		if ( strtolower( $atts['name'] ) === 'whatsapp' ) {
			$settings_link = site_url( '/' . $vendor->get_username() . '/account/settings/' );
			return '<a href="' . esc_url( $settings_link ) . '" target="_blank" class="button button--large hp-button--wide">WhatsApp non défini</a>';
		} elseif ( strtolower( $atts['name'] ) === 'messenger' ) {
			$settings_link = site_url( '/' . $vendor->get_username() . '/account/settings/' );
			return '<a href="' . esc_url( $settings_link ) . '" target="_blank" class="button button--large hp-button--wide">Messenger non défini</a>';
		}

		// Message par défaut pour les autres attributs vides
		$settings_link = site_url( '/' . $vendor->get_username() . '/account/settings/' );
		return '<a href="' . esc_url( $settings_link ) . '" target="_blank" class="button button--large hp-button--wide">Informations non renseignées</a>';
	}
);
[hivepress_vendor_attribute name="whatsapp"]
1 Like

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