简体   繁体   English

覆盖WordPress中的函数

[英]Override a function in WordPress

Am having a plugin, In that I need to change some text it in, but this function doesn't have hook to use. 我有一个插件,因为我需要更改一些文本,但是这个函数没有钩子可以使用。

Usually the function will be override by using remove_action() or remove_filter() . 通常使用remove_action()remove_filter()来覆盖该函数。 For both the function we need filter name to override. 对于这两个函数,我们需要覆盖过滤器名称。 But this plugin function doesn't add any filters. 但是这个插件功能不会添加任何过滤器。

Now I need to override this function. 现在我需要覆盖这个功能。

For example 例如

Plugin.php in plugin 插件中的Plugin.php

function plugin(){
    echo 'hello';
    echo 'welcome you';
}

Function.php in theme 主题中的Function.php

I want to the function plugin() in plugin.php to 我想在plugin.php中使用函数plugin()

function theme_plugin(){
    echo 'hello';
    echo 'You are welcome';
}

There is not add_action for plugin() . plugin()没有add_action

How to override the plugin() to theme_plugin() ? 如何覆盖plugin()theme_plugin()

Look here Redefining PHP function? 看看这里重新定义PHP函数? , you can't override php function or overload it. ,你不能覆盖PHP功能或超载它。 You have to find another way for solving your problem. 你必须找到解决问题的另一种方法。

Regards, 问候,

If am right, you can us this link: http://sltaylor.co.uk/blog/customizing-new-user-email-pluggable-function/ 如果我是对的,你可以给我们这个链接: http//sltaylor.co.uk/blog/customizing-new-user-email-pluggable-function/

**** Edit** ****编辑**

I have no much experience with wordpress. 我对wordpress没有多少经验。 But here is someone asking for the same: Overriding a theme function from plugin in WordPress 但是这里有人要求相同: 在WordPress中覆盖插件中的主题功能

And the marked answer is: 明确的答案是:

Unless the function is meant to be overridden, no. 除非该函数被覆盖,否则。 This is basic PHP. 这是基本的PHP。 You can't redefine a function. 您无法重新定义函数。 If you try you will get a fatal error. 如果你尝试,你会得到一个致命的错误。

Parts of WordPress are written to be overwritten. 编写WordPress的部分内容将被覆盖。 Look at /wp-includes/pluggable.php. 看看/wp-includes/pluggable.php。 Every function in there is wrapped in a if( !function_exists(...) ) conditional. 其中的每个函数都包含在if(!function_exists(...))条件中。 Unless your theme did the same, and some do for some functions, you can't overwrite. 除非你的主题做同样的事情,有些功能也是如此,否则你无法覆盖。

Look around for filters that might help you instead. 四处寻找可能对您有所帮助的过滤器。

Looking at your code, you should be able to unhook that. 看看你的代码,你应该能够解开它。 Just make sure to hook the unhook late enough. 只要确保将钩子钩得足够晚。 That is not a good solution, though since you are breaking theme functionality and also must know the know the names of all the hooked functions that themes are using. 这不是一个好的解决方案,但是因为你破坏了主题功能,而且还必须知道主题正在使用的所有钩子函数的名称。

Is there something in $fragments, or in $_POST or $_GET or anything else, that you can use to conditionally run your code, leaving the rest alone. 是否存在$ fragment中的内容,或$ _POST或$ _GET或其他任何东西,您可以使用它来有条件地运行代码,剩下的就是其余部分。

Anyway, Wordpress is opensource, so you can change the code by yourself? 无论如何,Wordpress是开源的,所以你可以自己更改代码吗? Why just change the plugin? 为什么要改变插件?

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

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