簡體   English   中英

覆蓋 WordPress 中的插件功能

[英]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 仍然有能力添加“必須使用”的插件,這些插件在所有其他插件之前運行。 所以這讓我們有機會添加你想要覆蓋的函數,所以它在原始插件運行時已經存在。

在你的情況下

  1. wp-content/mu-plugins文件夾中添加一個 .php 文件讓我們說
wp-content/mu-plugins/custom-override.php
  1. custom-override.php添加您的函數:
if ( ! function_exists( 'sensei_start_course_form' ) ) {
         function sensei_start_course_form( $course_id ) {
             //your magic here
             //...
         }
    }

  1. 確保原始插件也有這個條件“如果函數不存在......”
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM