Add a Label to a section of listing update page

Hi there, I would like to add a descriptive label to certain part of my listing update page. This label is not supposed to describe a specific attribute but a set of attributes so I can’t add it to just one of the attribute. I could not find a label type for custom attributes, is there any other way to do this? The way to think about this is a label to describe a section of a page that consists of many attributes.

Hi,

Unfortunately, there’s no such feature, it would require a custom implementation. If customizations are required for your site, please try customizing it using the collection of code snippets Search · user:hivepress · GitHub and other developer resources, or consider hiring someone for custom work https://fvrr.co/32e7LvY

Hi Andril, thanks for your response. Is having a label as an attribute field type something you are planning to support soon? It seems like something that should be supported by default since it is one of the mot basic (and used) html component. Also can you think of a work around? maybe there is another component I could use and style is as label?

What I have done is use an optional Radio button field with no values and use css to hide the optional label. It seems to work but with some limitations. Ideally I would like to add a value to the Radio button and select it by default so I can also display this label in the view listing section.
Is there anyway to automatically select an attribute value?
So I have a required radio button with just one value and I would like to select that value by default.

Btw thanks for your continuing help :slight_smile:

Hi,

Yes, we are planning to improve the display of attributes in updates, but there is no simple solution at the moment, it will require a custom implementation. Regarding the default attributes, please check this samples in this topic Vendor Attribute With Default Option - #14

Selecting my attribute (a checkbox) by default didn’t work out. I tried the code snippet you referred to

add_action('hivepress/v1/models/listing/create',
	function ($listing_id, $listing){
		$listing->set_activities_label(297)->save_activities_label();
	},
	1000,
	2
);

Then I tried this code snippet from a different post. What I am trying do to is select a checkbox by default and it is not working

add_action('hivepress/v1/models/listing/create', function($listing_id) {
	update_post_meta($listing_id, 'activities_label', true);
});

See my attribute set up below (please disregard the label in the name)

Thanks in advance!

Update: I did some debugging and if I use the update hook it works, but the create hook doesn’t work.
This code works:

add_action('hivepress/v1/models/listing/update', function($listing_id) {
	update_post_meta($listing_id, 'hp_activities_label', true);
});

However this same code with create doesn’t work

add_action('hivepress/v1/models/listing/create', function($listing_id) {
	update_post_meta($listing_id, 'hp_activities_label', true);
});

I need to use this when listings are created. Any ideas what the problem is?

I’m sorry for the delay. Please try using “1” string instead of “true”. Also, you can use the update hook anyway, just check if the value is already set, or if the listing title is empty (to prevent setting the value on every update).

Thank you for your response and no worry, I am sure you have your hands full :slight_smile: I will report back.

Hi Ihor,
unfortunately this didn’t work. I don’t believe that the issue is with how the value is being set. I believe that the issue it with the create hook. It is never being called. I added a debug statement inside the method and it was never called. As you can see using “true” worked for the update hook. I think the question is what is the correct create listing hook? Thanks!

Yes, I realized it might not be called since there’s a draft listing being created when the Add Listing page is visited, so this hook is not guaranteed to fire every time. Also, a draft may be created if you visited WordPress/Listings and clicked Add New without completing the form.

I recommend using the update hook instead, with a condition to check if the listing title is empty, if so, then set the checkbox value, this way, it will be set only once since when the title is set, this means that the Add Details form is completed.

Thanks. That makes sense! My apologies but I still need your help figuring this out. I had to change set_activities_label to radio button because it was easier to find and style. The button only has one option called ‘Yes’ with an id of 297. I am trying to select that option by default using the update hook code as you suggested, see below. What I am getting is an infinite loop because every time I make the update the hook is called again and again and again lol. Please help!

add_action('hivepress/v1/models/listing/update', function($listing_id) {
	$listing = HivePress\Models\Listing::query()->get_by_id($listing_id);
	error_log('Listing  found');
	
	if(!$listing ){
		error_log('Listing not found');
		return;
	}
	if($listing->get_title()) {
		$listing->set_activities_label(297)->save_activities_label();
	}
    
});

I fixed this by using wp_set_object_terms to update the database directly.

1 Like

I’m sorry for the late reply. I recommend using our API where possible, the snippet you posted may work if you provide an array of IDs instead of a number, even if it’s a single ID:

$listing->set_activities_label([297])->save_activities_label();

Also, please try using some condition to check if the actions are already set to prevent setting the value over and over on every update (this causes an infinite loop). Another solution is using remove_action inside the callback to make sure that it’s called once remove_action() – Function | Developer.WordPress.org

Hope this helps.

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