I want to change the "list a service" label based on condition

Hi there,

I hired a developer to help me out with this one, but we’re facing an issue / it doesn’t work. What I want to have:

We simply want to change the slug “Creator werden” (which was “list a service” before) into “Gig erstellen” when the user is logged in.

This is the snippet I’ve tried:

add_filter( 'hivepress/v1/templates/header/menu_items', 'custom_menu_items' );
function custom_menu_items( $items ) {
	if ( is_user_logged_in() ) {
		foreach ( $items as &$item ) {
			if ( 'hivepress/v1/components/listing/list_button' === $item['template'] ) {
				$item['label'] = __( 'Gig erstellen', 'ccfyde' );
			}
		}
	}
	return $items;
}

You guys may help us with the right hook or info about why that’s not working.

Hi,

Please try this PHP snippet:

add_filter(
 'gettext',
 function($translated_text, $untranslated_text, $domain){
  if('List a Service' !== $untranslated_text || 'taskhive' !== $domain){
   return $translated_text;
  }
  
  if(!hivepress()->request->get_context('listing_count')){
   return $translated_text;
  }
  
  $translated_text = 'Custom text';
  
  return $translated_text; 
 },
 1000,
 3
);

Please note that it can require further customization.

2 Likes

How can I make this same code snippet work for listinghive instead?

In this case it’s easier to use the hivepress/v1/strings filter, it accepts an array of strings - you can check some condition and change the $array['add_listing']=... string.

1 Like

Hi buddy, usually when the code snippet plugin faces an error you can simply go back to the code and it will show you the revision before the crash (or nothing if you added a new code). So after a few seconds your site should be fine.

Hey thanks for writing. Normally I can just click back but this time is first time it wont let me. Worse is that the code snippet plugin in the dashboard completely disappeared. The only way my site actually can be viewed was by deactivate it.

But I found the solution. I actually used my jetpack dashboard recovery and was able to go back to my yesterdays saved version. so all is good now! :grinning:

1 Like

ihor, I tried this and I don’t see any changes. What am I missing to make it work? Thanks

add_filter(
    'hivepress/v1/strings',
    function ( $strings ) {
        if ( ! hivepress()->request->get_context( 'listing_count' ) ) {
            return $strings;
        }
 
        $strings['add_listing'] = 'Custom text';
 
        return $strings;
    }
);

If you want to change string text depending on is user a vendor or not then please try this PHP snippet to start. But please note that it can require further customization. If you are not familiar with code customization then please consider hiring someone for custom work https://fvrr.co/32e7LvY

add_filter(
    'hivepress/v1/strings',
    function ( $strings ) {
		
		if(is_user_logged_in()){
			// User logged in and user can be vendor.

			// Get vendor.
			$vendor = \HivePress\Models\Vendor::query()->filter(
			[
				'status' => [ 'auto-draft', 'draft', 'publish' ],
				'user'   => get_current_user_id(),
			]
			)->get_first();
			
			if(!$vendor){
				//User is not vendor.
			}else{
				//User is vendor.
			}
 
		}else{
			//User is not vendor as user is not logged in. 
		}
 
        return $strings;
    }
);

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