首页 > 解决方案 > 向 Wordpress 插件的类函数添加过滤器

问题描述

我试图在不编辑实际插件的情况下在一些插件 HTML 之前添加一些 HTML。

该函数出现在他们的类 NavigationBar 中。

class NavigationBar {
    public static function get_navigation_bar( $options ) {
        // THE CODE
        return $html;
    }
}

我想在我的functions.php中添加一个过滤器,有点像这样:

function get_navigation_bar_with_ad( $html ) {
    $html = '<div>This appears before the nav bar.</div>' . $html;
    return $html;
}
add_filter( 'get_navigation_bar', 'get_navigation_bar_with_ad' );

除了将 HTLM 添加到插件的代码中之外,还有其他简单的方法吗?我宁愿避免这种情况,以便将来的更新不会破坏代码。谢谢!


在使用该NavigationBar::get_navigation_bar( $options )函数的构造函数中,他们将操作和过滤器添加到 $html 对象。

add_filter( 'tps_the_content_after_current_slide', __NAMESPACE__ . '\\Content::filter_content_after_current_slide', 10, 2 );

如果我可以添加到这个过滤器,我想我会得到我想要的结果。

标签: wordpress

解决方案


如果插件本身提供了过滤器/操作,则可能是的,但它看起来不像。您可以在“apply_filter()”或“do_action”函数中重新识别它,例如

$html = apply_filter(...);

我建议您使用 jQuery 并按如下方式操作 html:

jQuery('#idOfPluginHtml').prepend("<div>Your HTML</div>");

感谢 DubVader:前置是正确的

很好的@LucyTurtle,我可以帮助你。如果有人需要这个,我会将您的最终解决方案添加到我的评论中

function get_navigation_bar_with_ad( $html ) {
    return $html . '<div>This will appear above the nav bar.</div>';
}
add_filter( 'tps_the_content_after_current_slide', 'get_navigation_bar_with_ad' );

推荐阅读