Hi, All in Hivepress.
I hope this message finds you well.
I have successfully implemented the following code to hide the “categories” field in the listing submission form:
add_filter(
'hivepress/v1/forms/listing_submit',
function( $form ) {
unset($form['fields']['categories']);
return $form;
},
1000
);
add_filter(
'hivepress/v1/models/listing',
function( $args ) {
$args['fields']['categories']['required'] = false;
return $args;
},
1000
);
However, my goal is different. I would like to restrict the listing categories based on user roles .
Specifically, I want to achieve the following:
• For users with the artist_user role, they should only be able to select the sounds category (tag_ID = 121) when submitting a listing.
Could you please advise on the best way to implement this functionality?
Thank you in advance for your guidance and support.
andrii
December 5, 2024, 3:55pm
5
Hi,
Please use this PHP snippet.
add_filter(
'hivepress/v1/forms/listing_submit',
function( $form ) {
if(current_user_can('role_name_here')){
$form['fields']['categories']['option_args']['exclude']=[1,2,3];
}
},
1000
);
You need to replace role_name_here
with the user role and 1,2,3
with the IDs of your categories.
Thank you for your response!
I tried using the provided snippet, but encountered the following error:
Warning: foreach() argument must be of type array|object, null given in /home/tenpo10/www/tenpo10.com/wp-content/plugins/hivepress/includes/forms/class-form.php on line 173
If you happen to know the cause and a simple way to address this issue, I would greatly appreciate your guidance.
Thank you in advance for your support.
Best regards,
andrii
December 9, 2024, 3:48pm
9
Hi,
Sorry for the confusion, please use this PHP snippet:
add_filter(
'hivepress/v1/forms/listing_submit',
function( $form ) {
if(current_user_can('role_name_here')){
$form['fields']['categories']['option_args']['exclude']=[1,2,3];
return $form;
}
},
1000
);
Thank you for your previous guidance. Unfortunately, the provided code did not seem to have the desired effect, as there were no visible changes in the behavior.
To investigate further, I attempted to output the $form array using the following snippet:
add_filter(
'hivepress/v1/forms/listing_submit',
function( $form ) {
echo '<pre>';
print_r( $form['fields'] );
echo '</pre>';
return $form;
},
1000
);
The output is as follows:
Array
(
[images] => Array
(
[_order] => 10
)
[title] => Array
(
[_order] => 20
)
[description] => Array
(
[html] =>
[_order] => 200
)
[price] => Array
(
[label] => 価格
[type] => number
[min_value] => 0
[required] => 1
[_order] => 30
[decimals] => 0
)
[price_extras] => Array
(
[label] => オプション
[type] => repeater
[_order] => 197
[fields] => Array
(
[name] => Array
(
[placeholder] => タイトル
[type] => text
[max_length] => 256
[required] => 1
[_order] => 10
)
[price] => Array
(
[placeholder] => 価格
[type] => currency
[min_value] => 0
[required] => 1
[_order] => 20
)
)
)
[tags] => Array
(
[label] => タグ
[type] => tags
[options] => terms
[max_values] => 10
[_order] => 190
[option_args] => Array
(
[taxonomy] => hp_listing_tags
[number] => 100
)
)
[_terms] => Array
(
[caption] => 利用規約に同意します
[type] => checkbox
[required] => 1
[_separate] => 1
[_order] => 1000
)
[categories] => Array
(
[multiple] =>
[required] => 1
[_order] => 5
[attributes] => Array
(
[data-multistep] => true
[data-render] => https://tenpo10.com/wp-json/hivepress/v1/forms/listing_submit/
)
)
)
It seems that the categories field’s array does not contain the expected option_args. Could this be the issue?
I would greatly appreciate any advice or recommendations on how to narrow down the output or investigate the root cause more efficiently.
Thank you for your time and support.
ihor
December 17, 2024, 12:30am
14
Please try this code snippet instead:
add_filter(
'hivepress/v1/models/listing',
function( $form ) {
if ( ! hivepress()->helper->is_rest() && current_user_can( 'artist_user' ) ) {
$form['fields']['categories']['option_args']['include'] = [ 121 ];
}
return $form;
},
1000
);
I tested it locally and it seems to be ok.
It’s resolved! Thank you!