如何为wp插件覆盖api中的js函数

问题描述 投票:0回答:1

我必须创建一个 wp 插件,使用英雄物业管理提供的 api 来显示列表。我需要重写 api 中的一个函数,该函数在 iframe 中的 onClick 事件上显示单个列表。如何重写该函数并在新页面/url 中显示单个列表。

api中主要的js函数类似这样: openListingFromSummary('listingId', 'listingDesc');

我尝试使用以下代码覆盖它:

var orig_openListingFromSummary = window.openListingFromSummary('TX055461L', '$1995/month 2 bedroom / 2.5 bathroom Single family');
window.openListingFromSummary = function(){
    orig_openListingFromSummary();
    alert('HI');
}

我希望使用 orig_openListingFromSummary 函数来操作,而不是之前的 openListingFromSummary

javascript wordpress
1个回答
0
投票

您可以通过保留现有函数并根据您的需要添加附加逻辑来覆盖它。

作为参考,我们可以实现类似的东西。

// 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.
};

© www.soinside.com 2019 - 2024. All rights reserved.