简体   繁体   English

覆盖 WordPress 中的插件功能

[英]Override Plugin Function in WordPress

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()

You can do it by using add_filter() function. 您可以使用add_filter()函数来完成。
See wordpress stackexchange: Override plugin with functions.php 请参阅wordpress stackexchange:使用functions.php覆盖插件

Just add the below code in theme's functions.php file. 只需在theme的functions.php文件中添加以下代码即可。

add_filter('sensei_start_course_form','MyCustomfilter',$priority = 10, $args = 1);

function MyCustomfilter($course_id) { 
// Do your logics here
}

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. 禁用原件,启用副本。

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. 这样您就可以更新插件文件而不会影响您的更改。

A bit late on this (november 2021) but I still found this answer today, so I'll add a solution I didn't see around:有点晚了(2021 年 11 月),但我今天仍然找到了这个答案,所以我将添加一个我没有看到的解决方案:

For some historical reasons, WP still has the ability to add "must use" plugins that runs before all other plugins.由于一些历史原因,WP 仍然有能力添加“必须使用”的插件,这些插件在所有其他插件之前运行。 So this give us the opportunity to add the function you want to override, so it already exists when the original plugin run.所以这让我们有机会添加你想要覆盖的函数,所以它在原始插件运行时已经存在。

In your case在你的情况下

  1. add a .php file in the folder wp-content/mu-plugins let saywp-content/mu-plugins文件夹中添加一个 .php 文件让我们说
wp-content/mu-plugins/custom-override.php
  1. add your function in custom-override.php :custom-override.php添加您的函数:
if ( ! function_exists( 'sensei_start_course_form' ) ) {
         function sensei_start_course_form( $course_id ) {
             //your magic here
             //...
         }
    }

  1. be sure that the original plugin also has this conditional "if function doesn't already exists..."确保原始插件也有这个条件“如果函数不存在......”
if ( ! function_exists( 'sensei_start_course_form' ) ) { ... 

This did the trick for me ;-)这对我有用;-)

PD: I'm not an expert, please give me some feedback if this is wrong somehow. PD:我不是专家,如果有什么问题,请给我一些反馈。 Thanks谢谢

REF: https://wordpress.org/support/article/must-use-plugins/参考: https : //wordpress.org/support/article/must-use-plugins/

I also needed to change some code in a WordPress plugin.我还需要更改 WordPress 插件中的一些代码。 So I created a function that can be placed in functions.php in your child-theme.所以我创建了一个函数,可以放在你的子主题的 functions.php 中。 Please test before use.请在使用前进行测试。 It is probably poor written since I'm no PHP expert.它可能写得不好,因为我不是 PHP 专家。 But the concept works for me.但这个概念对我有用。 I tested it first outside WordPress so some variables like $root should/could be modified.我首先在 WordPress 之外对其进行了测试,因此应该/可以修改 $root 等变量。

Situation was that I had to change some values in two different files in the plugin Email posts to subscribers.情况是我不得不在插件电子邮件帖子中更改两个不同文件中的一些值给订阅者。

I needed to change $home_url = home_url('/');我需要更改$home_url = home_url('/'); to $home_url = 'custom-redirect-url';$home_url = 'custom-redirect-url'; and 'content="10; to 'content="1;'content="10;'content="1; in the files optin.php and unsubscribe.php.在文件 optin.php 和 unsubscribe.php 中。

Every time the plugin gets updated it runs a function after the update.每次更新插件时,它都会在更新后运行一个函数。 This is the code I use:这是我使用的代码:

// 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);

I guess this wil only be of use when you have to adjust some minor things.我想这只有在你必须调整一些小东西时才有用。 Rewriting complete code is maybe not working with this code, but I didn't test this.重写完整代码可能不适用于此代码,但我没有对此进行测试。

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

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