how to override a js function in api for wp plugin

I have to create a wp plugin that uses api provided by hero property management to show listings. I need to override a function that is in the api that shows single listing on onClick event in iframe. How to override that function a show the single listing in a new page/url. The main js function in api is similar to this: openListingFromSummary('listingId', 'listingDesc'); I tried to override it using the following code: var orig_openListingFromSummary = window.openListingFromSummary('TX055461L', '$1995/month 2 bedroom / 2.5 bathroom Single family'); window.openListingFromSummary = function(){ orig_openListingFromSummary(); alert('HI'); } I want the orig_openListingFromSummary function to operate instead of previous openListingFromSummary But I always get the previous function getting operated. Most likely the reason is api js function is getting called after the plugin js file. how do I ensure the custom wordpress plugin js file is getting called after the api js?

Comment (1)

Jese Leos

September 25, 2024

Verified user

You can override this by preserving the existing function and adding the additional logic as per your need. For reference we can implement something like this. // Here we are preserving the original function. var orig_openListingFromSummary = window.openListingFromSummary; // This is to override the openListingFromSummary function. window.openListingFromSummary = function(listingId, listingDesc) { // This is to redirect to the new URL, using the listingId. var newUrl = '/path-to-your-listing-page?listingId=' + encodeURIComponent(listingId); window.location.href = newUrl; // Redirect to the new URL // This is to log the listing description. console.log(listingDesc); // This is just for debugging/confirmation purpose. }; Run code snippetExpand snippet

You’ll be in good company