php 覆盖 WordPress 中的插件功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33147735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override Plugin Function in WordPress
提问by michaelmcgurk
I have a Plugin installed on my WordPress site.
我的 WordPress 网站上安装了一个插件。
I'd like to override a function within the Plugin. Do I override this in my theme's functions.php
and if so, how do I do this?
我想覆盖插件中的一个功能。我是否在我的主题中覆盖它,functions.php
如果是,我该怎么做?
Here's the original function in my plugin:
这是我的插件中的原始功能:
/**
* sensei_start_course_form function.
*
* @access public
* @param mixed $course_id
* @return void
*/
function sensei_start_course_form( $course_id ) {
$prerequisite_complete = sensei_check_prerequisite_course( $course_id );
if ( $prerequisite_complete ) {
?><form method="POST" action="<?php echo esc_url( get_permalink() ); ?>">
<input type="hidden" name="<?php echo esc_attr( 'woothemes_sensei_start_course_noonce' ); ?>" id="<?php echo esc_attr( 'woothemes_sensei_start_course_noonce' ); ?>" value="<?php echo esc_attr( wp_create_nonce( 'woothemes_sensei_start_course_noonce' ) ); ?>" />
<span><input name="course_start" type="submit" class="course-start" value="<?php echo apply_filters( 'sensei_start_course_text', __( 'Start taking this Course', 'woothemes-sensei' ) ); ?>"/></span>
</form><?php
} // End If Statement
} // End sensei_start_course_form()
采纳答案by Gerald Schneider
You can't really "override" a function. If a function is defined, you can't redefine or change it. Your best option is to create a copy of the plugin and change the function directly. Of course you will have to repeat this everytime the plugin is updated.
你不能真正“覆盖”一个函数。如果定义了函数,则不能重新定义或更改它。您最好的选择是创建插件的副本并直接更改功能。当然,每次更新插件时,您都必须重复此操作。
Give the plugin a different name to distinguish them in the plugin listing. Disable the original, enable your copy.
给插件一个不同的名字以在插件列表中区分它们。禁用原件,启用您的副本。
回答by Braeden Black
I know this is late but in the event that someone else finds this post. A simpler solution is to make a copy of the function if you can to your themes functions file and rename it so that it doesn't conflict with the original function. Then use your new function in place of the original. That way you can update the plugin files without affecting your changes.
我知道这已经晚了,但万一其他人发现了这篇文章。一个更简单的解决方案是,如果可以的话,复制该函数到您的主题函数文件中并重命名,以免它与原始函数发生冲突。然后使用您的新功能代替原来的功能。这样您就可以更新插件文件而不会影响您的更改。
回答by Noman
You can do it by using add_filter()function.
See wordpress stackexchange:Override plugin with functions.php
您可以使用add_filter()函数来完成。
参见wordpress stackexchange:Override plugin with functions.php
Just add the below code in theme's functions.php
file.
只需在主题functions.php
文件中添加以下代码即可。
add_filter('sensei_start_course_form','MyCustomfilter',$priority = 10, $args = 1);
function MyCustomfilter($course_id) {
// Do your logics here
}