简体   繁体   English

如何在子主题中使用钩子来覆盖插件?

[英]How to use hooks in child theme to override a plugin?

I want to add some HTML code to a plugin but I want to do that in a child theme , the function that I wanna override it is in a file named as admin-functions.php 我想一些HTML代码添加到一个插件 ,但我想这样做,在一个孩子的主题 ,那我想重写它是在一个名为管理-functions.php文件功能

I want to add extra buttons to booked_render_custom_fields function 我想在booking_render_custom_fields函数中添加额外的按钮

admin-functions.php: 管理员-的functions.php:

 function booked_render_custom_fields($calendar = false) { ?>
    <button class="button">Text</button>;
<?php }

any suggestion? 有什么建议吗? thanks 谢谢

you can't really override a function like that. 你无法真正覆盖这样的功能。 you can only use the filters and hooks that the plugin implements. 你只能使用插件实现的过滤器和钩子。 but you got some options: 但你有一些选择:

a) make a copy of the plugin, change it's name and make the changes you need. a)制作插件的副本,更改其名称并进行所需的更改。 after that, it wont get updates anymore. 之后,它不再获得更新。 you'll have to implement them manually. 你必须手动实现它们。

b) you could add just one line to the original plugin and define a filter or hook to which you subscribe in your functions.php . b)您只能在原始插件中添加一行,并定义您在functions.php中订阅的过滤器或挂钩。 so you only have to fix this one line after updating the plugin. 所以你只需要在更新插件后修复这一行。

c) since the newer versions of wordpress, you have the possibilites to run your own code after any shortcode. c)自wordpress的新版本以来,您有可能在任何短代码后运行自己的代码。 so you could add or inject your own html into the output of the shortcode. 所以你可以在短代码的输出中添加或注入你自己的html。 https://developer.wordpress.org/reference/hooks/do_shortcode_tag/ https://developer.wordpress.org/reference/hooks/do_shortcode_tag/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM