Hi,
I’m trying to add a custom “Share” button with a popup modal next to the “Add to Favorites” button on the single listing page.
I’ve tried several approaches:
Using the hivepress/v1/templates/listing_view_page/actions hook → nothing appeared
Using topbar and blocks hooks → either didn’t run or broke the layout
Created an override file: /wp-content/themes/listinghive-child/hivepress/listing/view/page/listing-actions.php
→ but it doesn’t seem to be loaded at all in my current HivePress version
So my question is:
Which is the correct hook or template file to use if I want to inject a custom button into the listing actions section, specifically where the “Add to Favorites” button is shown?
I would like to make this fully compatible and avoid injecting it via JavaScript if possible.
I’ve added a custom share button to the listing page using the hivepress/v1/templates/listing_view_page/blocks filter and merge_trees() – everything works perfectly. On click, a modal with social sharing links (Facebook, WhatsApp, etc.) appears.
Now I want to improve the behavior on mobile: I would like the modal to appear only on desktop, and on mobile (≤ 480px), I want the native sharing (navigator.share) to be triggered instead, without showing the modal at all.
Here’s the code I’m using (which works fine on both desktop and mobile with the modal):
To conditionally trigger the modal only on desktop and use navigator.share on mobile without showing the modal in your provided PHP code, you need to modify the share button’s behavior based on the device type. This can be achieved by adding JavaScript to detect whether the user is on a mobile or desktop device and then either displaying the modal (for desktop) or invoking the native share functionality (for mobile). Below is a complete solution with explanations.
Solution
You’ll need to:
Detect whether the device is mobile or desktop using JavaScript.
Update the share button’s onclick event to conditionally either open the modal or use navigator.share.
Integrate this logic into your existing add_filter function.
The isMobile function uses a regular expression to check the navigator.userAgent string for common mobile device identifiers (e.g., Android, iPhone, iPad). If a match is found, it returns true, indicating the user is on a mobile device; otherwise, it returns false for desktop.
function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
2. Share Button Behavior
The share button’s onclick event now calls the handleShare function instead of directly opening the modal. The handleShare function:
Prevents the default link behavior with event.preventDefault().
Checks if the device is mobile (isMobile()) and if the navigator.share API is supported (navigator.share).
On mobile devices with navigator.share support, it triggers the native share dialog with the page title and URL.
On desktop (or mobile devices without navigator.share support), it displays the modal.
The JavaScript is embedded within the 'content' string of the 'custom_share_button' block, ensuring it loads alongside the HTML for the share button and modal. The existing modal HTML remains unchanged and is still used for desktop users.
How It Works
Desktop: Clicking the share button opens the modal with options for Facebook, Messenger, WhatsApp, and copying the link.
Mobile: Clicking the share button triggers the native share dialog (e.g., on iOS or Android), bypassing the modal entirely. If navigator.share isn’t supported (e.g., on older mobile browsers), the modal will still appear as a fallback.
Notes
Browser Compatibility: The navigator.share API is supported in modern mobile browsers (e.g., Chrome, Safari on iOS 12.2+, Android browsers), but not on desktop browsers. The fallback ensures functionality across all devices.
User Experience: This approach enhances the mobile experience by leveraging native sharing, while retaining a custom modal for desktop users who lack navigator.share.
This solution fully addresses your request to conditionally show the modal on desktop and use navigator.share on mobile without displaying the modal.
thanks @ChrisB
did you try this code ?
at my end the copy link works well, but everything else does not share the link to the url.
not sure if there is some small ’ or . missing etc
Just to clarify — the code I shared works both on mobile and desktop, and that’s not in question. But my original post wasn’t about “making the links work” everywhere — it was about getting different behavior on mobile vs desktop: modal on desktop, native sharing on mobile.
That was the point of my question.
If the links work partially or you expect deep-linking behavior, that’s fine — but that’s a separate topic and not what I asked for or intended to solve here.
I really appreciate constructive feedback (like the first reply I received), but please let’s stay focused on what’s actually being discussed — otherwise it just derails the conversation.
This is perfect, Chris — exactly what I was trying to achieve. Clear explanation, working code, fallback support… really appreciated. I’ll test it out and confirm, but it looks spot on. Thanks a lot again!
This should work with the base HivePress plugin. If you use the ‘Favourites’ extension, this custom code will add a share option next to where it’s located on the listing details page.
It seems you didn’t really understand what I wrote earlier.
I noticed you keep directing your request for a full implementation to someone else, even though the original post was mine — and the intention behind it was not to build an all-in-one social sharing toolkit.
The fact that you want working links for Facebook, WhatsApp, and Messenger is fair — but it’s not what this thread is about. I do, in fact, have a working solution for those platforms, but that’s beside the point here. Forums work best when we respect the topic and contributors’ intentions, rather than treating threads like code vending machines.
Let’s keep the conversation focused and respectful — we all benefit that way.