Include attachment URL in another attribute's description

Hi there,

My listing owners have the ability to upload their own booking terms for their listing in pdf format. When clients book their property the client should accept the terms. For that I just added a checkbox that they confirm, but I need an URL to that listing attachment. Not sure what is the best approach. I was thinking to use the %listing.attachment% object in URL format but it does not work. Just prints the text. The URL would be unique for every listing.


Hi,

Unfortunately, there’s no such feature; it would require a custom implementation (this is possible only for Display format).

As a workaround, you can add this information to the general terms (and display a link to the terms on the vendor’s page, in any Display Area).

Thanks for the answer Andrii and the suggested workaround.

I’m familiar with PHP for other projects but still learning on the Wordpress specifics. I like they way everything can be customised as well and the coding practises for Hivepress. Also happy to hire someone at Fiverr at a later stage if required for certain functions if I would not have time.

One question to understand it better and have a lead where to start to customise.

“If I like to refer to an attribute of a listing anywhere from the project/page, how would that look like?”

I assume this would need to have the current “post ID” to “lookup” the attribute.

Please try this PHP snippet

Please change put_your_checkbox_attribute_field_name on your booking checkbox attribute field name and please change get_put_your_url_attribute_field_name on get_ + listing URL attribute field name

add_filter(
	'hivepress/v1/forms/booking_confirm',
	function($form_args, $form){
		$booking = $form->get_model();
		
		if(!$booking){
			return $form_args;
		}
		
		$listing = $booking->get_listing();
		
		if(!$listing || !$listing->get_put_your_url_attribute_field_name()){
			return $form_args;
		}
		
		if(isset($form_args['fields']['put_your_checkbox_attribute_field_name'])){
			$form_args['fields']['put_your_checkbox_attribute_field_name']['description'] = '<a href="'.esc_url($listing->get_put_your_url_attribute_field_name()).'" target="_blank">Custom text</a>';
		}
		
		return $form_args;
	},
	1000,
	2
);

Thanks Yevhen,

That’s something I’m looking for, however does not work yet. I was trying to directly refer to the attachment attribute URL for the custom post type. It looks like HP is saving a field ID for the attachment as listing attachments are saved in the media gallery. I was onto this, which does not return anything until you shared the filter which would be better.

<?php
$listing_id = get_the_ID();
$attachment_ids = get_post_meta($listing_id, '_hivepress_listing_attachments', true);
if ($attachment_ids) {
  foreach ($attachment_ids as $attachment_id) {
    $attachment_url = wp_get_attachment_url($attachment_id);
    echo '<a href="' . $attachment_url . '">' . $attachment_url . '</a><br />';
  }
}
?>


You filter does not generate the correct URL as the attribute is of the Field Type Attachment, not an URL type. Not sure how to fetch the URL.

Here is the listing attribute. To be clear, every listing will have an PDF uploaded which I like to lookup to URL from. Something like $listing->attachment_attribute->URL.

I think I’m close :wink:

Hi again,

I further investigated to pull the data and by combining the wp_get_attachment_url() with the listing attachment attribute field value to lookup the URL. See the solution below in the filter.

Would you agree this is a solid solution?

add_filter(
	'hivepress/v1/forms/booking_confirm',
	function($form_args, $form){
		$booking = $form->get_model();
		
		if(!$booking){
			return $form_args;
		}
		
		$listing = $booking->get_listing();
		
		if(!$listing || !$listing->get_put_your_url_attribute_field_name()){
			return $form_args;
		}
		
		$field_name = 'hp_algemene_voorwaarden'; // replace my_custom_field with the name of your custom field
		$attachment_post_id = $listing->get_algemene_voorwaarden();
		$attachment_url = wp_get_attachment_url( $attachment_post_id );
		echo $attachment_url;
		
		if(isset($form_args['fields']['algemene_voorwaarden'])){
			$form_args['fields']['algemene_voorwaarden']['description'] = '<a href="'.esc_url($attachment_url).'" target="_blank" rel="noopener">Custom text</a>';
		}
		
		return $form_args;
	},
	1000,
	2

Yes, it seems like the possible way to get the attachment URL

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