Add text above account sidebar menu

Hi there!

I would love to add a little text above the sidebar menu on the account page.

Could you please provide a snippet to add a tiny paragraph section there?

Please see the screenshot to understand exactly where I mean.

Thank you!

Hi,

Please try this PHP snippet:

add_filter(
 'hivepress/v1/templates/user_account_page',
 function($template){
  $menu = hivepress()->template->fetch_block($template, 'user_account_menu');
  return hivepress()->template->merge_blocks(
   $template,
   [
    'page_sidebar' => [
     'blocks' => [
      'custom_menu_block' => [
       'type' => 'container',
       '_order' => 10,
       
       'attributes' => [
        'class' => [ 'hp-widget', 'widget', 'widget--sidebar', 'widget_nav_menu' ],
       ],
       
       'blocks' => [
        'custom_text' => [
         'type' => 'content',
         'content' => '<p>Custom text</p>',
         '_order' => 5,
        ],
        
        'user_account_menu' => [
         'type'       => 'menu',
         'menu'       => 'user_account',
         '_order'     => 10,
        ],
       ],
      ],
     ],
    ],
   ]
  );
 },
 1000
);

Thank you!

For some reason it changes the layout of some elements on the page after adding this snippet. Is there a way to add content block without influencing those other elements?

Unfortunately there’s no easy way to insert custom text inside the menu container, but it’s possible to add it above the menu (outside of the menu box with shadow), please let me know if it’s ok and I’ll share the snippet.

You can also try using the account sidebar for this purpose, then code changes are not required (you can add a Text widget in Appearance/Widgets), but widgets are displayed under the menu.

Hi ihor, above the menu would be even better actually. A snippet for it would be great.

Thank you

Hi,

Please try this PHP snippet:

add_filter(
 'hivepress/v1/templates/user_account_page',
 function($template){
  return hivepress()->template->merge_blocks(
   $template,
   [
    'page_sidebar' => [
     'blocks' => [
      'custom_text' => [
       'type' => 'content',
       'content' => '<p>Custom text</p>',
       '_order' => 5,
      ],
     ],
    ],
   ]
  );
 },
 1000
);

That’s super helpful. Thank you all a lot!

Is there a way to combine this with the snippet you provided to show a profile link:

add_filter(
	'hivepress/v1/menus/user_account',
	function ( $menu ) {
		if ( is_user_logged_in() ) {
			$vendor_id = HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();

			if ( $vendor_id ) {
				$menu['items']['vendor_view'] = [
					'label'  => 'Profil',
					'url'    => hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor_id ] ),
					'_order' => 21,
				];
			}
		}

		return $menu;
	},
	1000
);

Thank you a lot!

You can use both snippets at the same time, but if you mean adding the profile link above the menu (not as the menu item), it’s possible but requires further customizations.

Hi again,

ok, so in the end I would like to add an image and the name of the vendor with a link to the vendor’s profile to that area above the menu.

So you already provided a snippet to add plain text above there:

add_filter(
 'hivepress/v1/templates/user_account_page',
 function($template){
  return hivepress()->template->merge_blocks(
   $template,
   [
    'page_sidebar' => [
     'blocks' => [
      'custom_text' => [
       'type' => 'content',
       'content' => '<p>Custom text</p>',
       '_order' => 5,
      ],
     ],
    ],
   ]
  );
 },
 1000
);

This snippet should display the vendor name:

<h4 class="hp-vendor__name"><?php echo esc_html( $user->get_display_name() ); ?></h4>

And this snippet displays the vendor’s image (or if he doesn’t have any image a placeholder instead:

<div class="hp-vendor__image">
	<?php if ( $vendor->get_image__url( 'hp_square_small' ) ) : ?>
		<img src="<?php echo esc_url( $vendor->get_image__url( 'hp_square_small' ) ); ?>" alt="<?php echo esc_attr( $vendor->get_name() ); ?>" loading="lazy">
	<?php else : ?>
		<img src="<?php echo esc_url( hivepress()->get_url() . '/assets/images/placeholders/user-square.svg' ); ?>" alt="<?php echo esc_attr( $vendor->get_name() ); ?>" loading="lazy">
	<?php endif; ?>
</div>

And this snippet adds a link to the vendor’s profile:

<a href="<?php echo esc_url( hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor->get_id() ] ) ); ?>" class="hp-form__action hp-form__action--vendor-view hp-link"><i class="hp-icon fas fa-eye"></i><span><?php esc_html_e( 'View Profile', 'hivepress' ); ?></span></a>

However, when I combine them, my whole site crashes. I guess because it’s not allowed to just put php into html. This is the final snippet I tried out but my site crashed:

add_filter(
 'hivepress/v1/templates/user_account_page',
 function($template){
  return hivepress()->template->merge_blocks(
   $template,
   [
    'page_sidebar' => [
     'blocks' => [
      'custom_text' => [
       'type' => 'content',
       'content' => '<a href="<?php echo esc_url( hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor->get_id() ] ) ); ?>" class="hp-form__action hp-form__action--vendor-view hp-link"><i class="hp-icon fas fa-eye"></i><span><?php esc_html_e( 'View Profile', 'hivepress' ); ?><h4 class="hp-vendor__name"><?php echo esc_html( $user->get_display_name() ); ?></h4><div class="hp-vendor__image">
	<?php if ( $vendor->get_image__url( 'hp_square_small' ) ) : ?>
		<img src="<?php echo esc_url( $vendor->get_image__url( 'hp_square_small' ) ); ?>" alt="<?php echo esc_attr( $vendor->get_name() ); ?>" loading="lazy">
	<?php else : ?>
		<img src="<?php echo esc_url( hivepress()->get_url() . '/assets/images/placeholders/user-square.svg' ); ?>" alt="<?php echo esc_attr( $vendor->get_name() ); ?>" loading="lazy">
	<?php endif; ?>
</div></span></a>',
       '_order' => 5,
      ],
     ],
    ],
   ]
  );
 },
 1000
);

I wrapped the vendor’s image and the vendor’s name inside the vendor’s profile link. As I mentioned before, obviously it’s not as simple as I thought. My whole site crashed after inserting this snippet into the functions.php.

Could you help me adjust this snippet to finally make it work?

I really appreciate your efforts, thank you a lot!

Hi,

Please try this PHP snippet:

add_filter(
 'hivepress/v1/templates/user_account_page/blocks',
  function($blocks, $template){
  $user_id = get_current_user_id();
  
  if(!$user_id){
   return $blocks;
  }
  
  $user = \HivePress\Models\USer::query()->get_by_id($user_id);
  
  if(!$user){
   return $blocks;
  }
  
  $vendor = \HivePress\Models\Vendor::query()->filter(['user' => $user_id])->get_first();
  
  if(!$vendor){
   return $blocks;
  }
  
  $content = '<a href="'.esc_url( hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor->get_id() ] ) ).'" class="hp-form__action hp-form__action--vendor-view hp-link"><i class="hp-icon fas fa-eye"></i><span>View Profile<h4 class="hp-vendor__name">'.esc_html($user->get_display_name()).'</h4><div class="hp-vendor__image"><img src="'.esc_url( $vendor->get_image__url( 'hp_square_small' ) ).'" alt="'.esc_attr( $vendor->get_name() ).'" loading="lazy"></div></span></a>';
  
  if ( !$vendor->get_image__url( 'hp_square_small' ) ) {
   $content = '<a href="'.esc_url( hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor->get_id() ] ) ).'" class="hp-form__action hp-form__action--vendor-view hp-link"><i class="hp-icon fas fa-eye"></i><span>View Profile<h4 class="hp-vendor__name">'.esc_html($user->get_display_name()).'</h4><div class="hp-vendor__image"><img src="'.esc_url( hivepress()->get_url() . '/assets/images/placeholders/user-square.svg' ).'" alt="'.esc_attr( $vendor->get_name() ).'" loading="lazy"></div></span></a>';
  }
  
  return hivepress()->template->merge_blocks(
      $blocks,
      [
       'page_sidebar' => [
         'blocks' => [
           'custom_text' => [
             'type' => 'content',
             'content' => $content,
             '_order' => 5,
           ],
         ],
       ],
      ]
    );
 },
  1000,
 2
);

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

Seems to be really good, thank you!

Hi again. I used your code which you provided above and it’s pretty near what I want to have.

The only thing that is missing: When a user is only a user and not a vendor then there is no image visible at all.

Is it possible to add at least the user’s profile image or a placeholder if the user has no user profile image uploaded?

Thank you a lot

Hi,

Sorry, there’s no simple code snippet for this, it would require further customizations.
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

1 Like