我有一个插件安装在我的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: Override plugin with functions.php
只需在主题的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函数不存在…"
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;
每次插件更新时,它都会在更新后运行一个函数。下面是我使用的代码:
// 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);
我想这只会在你需要调整一些小东西的时候有用。重写完整的代码可能无法使用此代码,但我没有测试此