wordpress - 向 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 );
如果我可以添加到这个过滤器,我想我会得到我想要的结果。
解决方案
如果插件本身提供了过滤器/操作,则可能是的,但它看起来不像。您可以在“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' );
推荐阅读
- r - 图表没有显示在闪亮的应用程序中,但它们在控制台中运行良好
- javascript - Fancytree - 组合扩展 dnd5 多编辑
- php - 如何使用在php中多次收到的单个输入
- python - 根据列条件过滤几乎重复的数据
- javascript - 如何使用 typescript / mysql 包将 blob 插入 mysql
- php - PHP 修剪非字母 Unicode
- ios - 为什么 swipeActions 修饰符不能直接与 List 容器一起使用?#SwiftUI
- html - How do I resize my image when zooming in or out with the browser zoom tool?
- reactjs - 从 React 中的状态获取单个数据
- javascript - 无法解析 expo 应用程序中的模块“child_process”