Code snippet for adding a contact form to the listings sidebar

I’m using the HivePress plugin with a third party theme so don’t have access to Appearance > Widgets for the listings sidebar. Can I be provided with a code snippet to add the below shortcode?

Shortcode: [wpforms id=“298”]

Also, how do I remove the existing maps widget from the listings sidebar? I’d like it to be replaced with the above shortcode and for the shortcode to be stickied.

  1. If you mean a single listing page then please try this PHP snippet to add the shortcode to the listing sidebar. But please note that it can require further customization
add_filter(
	'hivepress/v1/templates/listing_view_page',
	function( $template ) {
		return hivepress()->helper->merge_trees(
			$template,
			[
				'blocks' => [
					'page_sidebar' => [
						'blocks' => [
							'custom_listing_side_shortcode' => [
								'type' => 'content',
								'content' => do_shortcode('[wpforms id=“298”]'),
								'_order' => 1000,
							]
						],
					],
				],
			]
		);
	},
	1000
);
  1. If you mean a single listing page then please try this CSS snippet to hide the map
.hp-template--listing-view-page .hp-map {
  display: none;
}

Which PHP file should the snippet be added to?

Please use the Code Snippets plugin to add and manage custom PHP code snippets Code Snippets – WordPress plugin | WordPress.org

The snippet has been added but it doesn’t seem to be working. Here’s an example listing where the shortcode should be appearing: Crestavilla | Retirement Being

Also, what steps are necessary to remove the map from the sidebar altogether, rather than just hide it? It’s negatively affecting SEO performance.

Please try to put the shortcode on a test page just to make sure that the shortcode is correct. As you can see on this screenshot code snippet works as it adds shortcode as text to the listing sidebar. But in your case, the shortcode should work as expected if the shortcode is correct

The shortcode works on other pages. Nothing is appearing on the listing pages though, not even the text of the shortcode. When I view source I see that it says, “WPForms: no fields, form hidden”. Is the CSS for removing the map interfering with the shortcode?

  1. Please try to add Text widget with content [wpforms id=“298”] in Listing (sidebar) section in Appearance/Widgets to add this shortcode to listing sidebar without PHP code snippet

  2. If you mean a single listing map then please try this CSS snippet

.hp-template--listing-view-page .hp-map {
  display: none;
}

As I mentioned in the first post, I’m using the HivePress plugin with a third party theme so don’t have access to Appearance > Widgets for the listing sidebar.

Please try another approach, if you use this code snippet then the sidebar will be registered for the Listing page:

add_action(
	'widgets_init',
	function() {
		register_sidebar(
			[
				'name'          => 'Listing Sidebar',
				'id'            => 'hp_listing_view_sidebar',
				'before_widget' => '<div id="%1$s" class="widget widget--sidebar %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<h3 class="widget__title">',
				'after_title'   => '</h3>',
			]
		);
	}
);

This way you can add the contact form or any other widgets in Appearance/Widgets section.

That works. Thanks.

As for the maps widget, how can I remove it entirely, rather than just hide it with CSS?

Please try this PHP snippet

add_filter(
	'hivepress/v1/templates/listing_view_page',
	function ($template){
		return hivepress()->helper->merge_trees(
			$template,
			[
				'blocks' => [
					'listing_map' => [
						'type' => 'content',
					],
				],
			]
		);
	},
	1000
);

This works. Thank you both.