Reputation: 6509
I have a Plugin installed on my WordPress site.
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?
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()
Upvotes: 32
Views: 69701
Reputation: 1
I also needed to change some code in a WordPress plugin. So I created a function that can be placed in functions.php in your child-theme. Please test before use! It is probably poor written since I'm no PHP expert. But the concept works for me. I tested it first outside WordPress so some variables like $root should/could be modified.
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('/'); to $home_url = 'custom-redirect-url'; and 'content="10; to 'content="1; in the files optin.php and 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.
Upvotes: 0
Reputation: 155
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:
For some historical reasons, WP still has the ability to add "must use" plugins that runs before all other plugins. 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
wp-content/mu-plugins
let saywp-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' ) ) { ...
This did the trick for me ;-)
PD: I'm not an expert, please give me some feedback if this is wrong somehow. Thanks
REF: https://wordpress.org/support/article/must-use-plugins/
Upvotes: 9
Reputation: 441
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.
Upvotes: 13
Reputation: 17797
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.
Upvotes: 17
Reputation: 4116
You can do it by using add_filter() function.
See wordpress stackexchange: Override plugin with functions.php
Just add the below code in theme's functions.php
file.
add_filter('sensei_start_course_form','MyCustomfilter',$priority = 10, $args = 1);
function MyCustomfilter($course_id) {
// Do your logics here
}
Upvotes: 12