3

I am currently modifying a purchased theme for a client that has several required plugins for it to work. One of the required plugins, creates a widget that displays a post type and client would like to remove some of the fields displayed.

Easy to go into the plugin and edit, problem solved. That's, of course, not good practice. I would like to know if an how to create a custom plugin that can over some of the functionality of the required plugin. Seems like it should be reasonable type of action but a little lost on best practice. Few questions.

Here is the start of the class that needs to be overwritten:

add_action('widgets_init', 'gdlr_lms_recent_course_widget');
if( !function_exists('gdlr_lms_recent_course_widget') ){
    function gdlr_lms_recent_course_widget() {
        register_widget( 'Goodlayers_Lms_Recent_Course' );
    }
}

if( !class_exists('Goodlayers_Lms_Recent_Course') ){
    class Goodlayers_Lms_Recent_Course extends WP_Widget{

        // Initialize the widget
        function __construct() {
            parent::WP_Widget(
                'gdlr-lms-recent-course-widget',
                __('Goodlayers Recent Course Widget','gdlr-lms'),
                array('description' => __('A widget that shows latest courses', 'gdlr-lms')));
        }

        // Output of the widget
        function widget( $args, $instance ) {
            global $gdlr_lms_option;

            $title = apply_filters( 'widget_title', $instance['title'] );
            $category = $instance['category'];
            $num_fetch = $instance['num_fetch'];

            // Opening of widget
            echo $args['before_widget'];

            // Open of title tag
            if( !empty($title) ){
                echo $args['before_title'] . $title . $args['after_title'];
            }

            // Widget Content
            $current_post = array(get_the_ID());
            $query_args = array('post_type' => 'course', 'suppress_filters' => false);
            $query_args['posts_per_page'] = $num_fetch;
            $query_args['orderby'] = 'post_date';
            $query_args['order'] = 'desc';
            $query_args['paged'] = 1;
            $query_args['course_category'] = $category;
            $query_args['ignore_sticky_posts'] = 1;
            $query_args['post__not_in'] = array(get_the_ID());
            $query = new WP_Query( $query_args );

            if($query->have_posts()){
                echo '<div class="gdlr-lms-course-widget">';
                while($query->have_posts()){ $query->the_post();
                    echo '<div class="recent-course-widget">';
                    gdlr_lms_print_course_thumbnail('thumbnail');

                    echo '<div class="recent-course-widget-content">';
                    echo '<div class="recent-course-widget-title"><a href="' . get_permalink() . '" >' . get_the_title() . '</a></div>';
                    echo '<div class="recent-course-widget-info blog-info">';
                echo '<span class="gdlr-head">' . __('Created on', 'gdlr-lms') . '</span> ';
                    echo get_the_time($gdlr_lms_option['date-format']);
                    echo '</div>';
                    echo '</div>';
                    echo '<div class="clear"></div>';
                    echo '</div>';
                }
                echo '<div class="clear"></div>';
                echo '</div>';
            }
            wp_reset_postdata();

            // Closing of widget
            echo $args['after_widget'];
        }

Few questions: I believed the purpose of the if(!class_exists(... was to allow the class to be created elsewhere allowing the ability to override, but that does not seem to work. Should I get errors about class having already been defined?

Is the correct method to extend this class with my own and use remove action against the add_action statement, then add my own?

I am a former procedural programer and just getting into my first set of custom classes. If I extend the class, will the constructor still call the parent of the parent or do I need to add parent::__construct into my constructor?

I have found a couple of examples for this but they are rather old and don't explain everything completely. I want to get my head around this as this functionality would be exceptionally helpful when making modifications to client sites when they don't want to go full custom.

1 Answer 1

1

You're actually quite close to answering your own question. Your plugin would have to start with checking whether the plugin is active (which you can also do with function_exists).

The you indeed can use remove_action to make sure functions and classes aren't activated. This doesn't mean these functions become undefined.

So yes, you will need to extend the class if you want to customize it. If you don't define a constructor in that customized class, the parent's constructor will be use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.