Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Plugin Function in WordPress

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()
like image 975
michaelmcgurk Avatar asked Oct 15 '15 11:10

michaelmcgurk


People also ask

How do I override a WordPress theme?

you need to create a folder '/woocommerce/' inside your plugin directory, inside woocommerce you need to add a folder say, for email 'emails' and put the required template inside '/emails/' to override. just copy paste this code in the main.

How do I use constants in WordPress?

To define a constant in PHP, you have to use the define() function. In the first parameter, give the name of the constant and, in the second one, its value, which can be a string, a number, or whatever you want. As you may realise, we will need to indicate the database credentials in order to let WordPress use it.


2 Answers

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.

like image 99
Gerald Schneider Avatar answered Sep 17 '22 15:09

Gerald Schneider


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
}
like image 36
Noman Avatar answered Sep 20 '22 15:09

Noman