[英]Override Plugin Function in WordPress
我在我的 WordPress 网站上安装了一个插件。
我想覆盖插件中的一个函数。 我是否要在主题的functions.php
中覆盖它?如果是,我该怎么做?
这是我插件中的原始功能:
/**
* 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()
您可以使用add_filter()函数来完成。
请参阅wordpress stackexchange:使用functions.php覆盖插件
只需在theme的functions.php
文件中添加以下代码即可。
add_filter('sensei_start_course_form','MyCustomfilter',$priority = 10, $args = 1);
function MyCustomfilter($course_id) {
// Do your logics here
}
你不能真正“覆盖”一个功能。 如果定义了函数,则无法重新定义或更改它。 您最好的选择是创建插件的副本并直接更改功能。 当然,每次更新插件时都必须重复此操作。
为插件添加一个不同的名称,以便在插件列表中区分它们。 禁用原件,启用副本。
我知道这已经很晚了,但是如果其他人发现了这个帖子。 一个更简单的解决方案是,如果可以对主题函数文件进行复制,并将其重命名,以使其不与原始函数冲突。 然后使用新功能代替原件。 这样您就可以更新插件文件而不会影响您的更改。
有点晚了(2021 年 11 月),但我今天仍然找到了这个答案,所以我将添加一个我没有看到的解决方案:
由于一些历史原因,WP 仍然有能力添加“必须使用”的插件,这些插件在所有其他插件之前运行。 所以这让我们有机会添加你想要覆盖的函数,所以它在原始插件运行时已经存在。
在你的情况下
wp-content/mu-plugins
文件夹中添加一个 .php 文件让我们说wp-content/mu-plugins/custom-override.php
custom-override.php
添加您的函数:if ( ! function_exists( 'sensei_start_course_form' ) ) {
function sensei_start_course_form( $course_id ) {
//your magic here
//...
}
}
if ( ! function_exists( 'sensei_start_course_form' ) ) { ...
这对我有用;-)
PD:我不是专家,如果有什么问题,请给我一些反馈。 谢谢
参考: https : //wordpress.org/support/article/must-use-plugins/
我还需要更改 WordPress 插件中的一些代码。 所以我创建了一个函数,可以放在你的子主题的 functions.php 中。 请在使用前进行测试。 它可能写得不好,因为我不是 PHP 专家。 但这个概念对我有用。 我首先在 WordPress 之外对其进行了测试,因此应该/可以修改 $root 等变量。
情况是我不得不在插件电子邮件帖子中更改两个不同文件中的一些值给订阅者。
我需要更改$home_url = home_url('/'); 到$home_url = 'custom-redirect-url'; 和'content="10;到'content="1; 在文件 optin.php 和 unsubscribe.php 中。
每次更新插件时,它都会在更新后运行一个函数。 这是我使用的代码:
// Function that replaces the code
function replace_text_plugin_email_posts_to_subscribers($pluginTargetFile, $replaceURL) {
$root = $_SERVER['DOCUMENT_ROOT'];
$replaceThis = array("\$home_url = home_url('/');", "content=\"10;");
$withThis = array($replaceURL, "content=\"1;");
$fname = $root . $pluginTargetFile;
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = str_replace($replaceThis, $withThis, $content);
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
}
//Function that runs every time that email-posts-to-subscribers is updated
function my_upgrade_function( $upgrader_object, $options ) {
$current_plugin_path_name = 'email-posts-to-subscribers/email-posts-to-subscribers.php';
if ($options['action'] == 'update' && $options['type'] == 'plugin' ) {
foreach($options['plugins'] as $each_plugin) {
if ($each_plugin==$current_plugin_path_name) {
replace_text_plugin_email_posts_to_subscribers("/wp-content/plugins/email-posts-to-subscribers/job/optin.php","\$home_url = 'https://example.com/redirect-optin';");
replace_text_plugin_email_posts_to_subscribers("/wp-content/plugins/email-posts-to-subscribers/job/unsubscribe.php","\$home_url = 'https://example.com/redirect-unsubscribe';");
}
}
}
}
add_action( 'upgrader_process_complete', 'my_upgrade_function',10, 2);
我想这只有在你必须调整一些小东西时才有用。 重写完整代码可能不适用于此代码,但我没有对此进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.