phpwordpresselementorlearndash

How to safely modify an Elementor Widget


I am trying to modify an Elementor Widget which is called "Course Grid". It is to display courses from Learndash LMS. It is different from the native Learndash course grid and is included in Buddyboss's theme. So I believe its author is Buddyboss. The reason I am trying to modify it is I want to sort the courses by a certain order determined by a custom field of the courses. Here is a screenshot of this widget:

enter image description here

So far I have been able to locate the files used to display the widget, it is /wp-content/themes/buddyboss-theme/inc/plugins/elementor/widgets/courses/templates/learndash-courses-template.php. And the query is constructed in /wp-content/themes/buddyboss-theme/inc/plugins/elementor/widgets/courses/bb-lms-courses.php:

....
    $query_args = array(
        'post_type'      => $this->get_active_plugin_post_type(),
        'posts_per_page' => $posts_per_page,
        'paged'          => $current_page,
    );

    /**
     * Below $tax_query will user with `get_my_courses_count` function also.
     */
    $tax_query = array();
    if ( ! empty( $category ) || ! empty( $tags ) ) {
        $tax_query = array(
            'relation' => 'AND',
        );

        if ( ! empty( $category ) ) {
            $tax_query[] = array(
                'taxonomy' => $this->get_active_plugin_category(),
                'field'    => 'id',
                'terms'    => $category,
            );
        }

        if ( ! empty( $tags ) ) {
            $tax_query[] = array(
                'taxonomy' => $this->get_active_plugin_tag(),
                'field'    => 'id',
                'terms'    => $tags,
            );
        }

        $query_args['tax_query'] = $tax_query;
    }
    $query = new \WP_Query( $query_args );

I believe to achieve my goal, I need to "order" the query; and to do that I need to change this $query variable somehow. However, I have checked both files there don't seem to be any hook that I can use. I've also tried copying these two files to the child theme folder but they don't work there. In this situation, do I have to hack the file? Is there any other way to make this modification safely?


Solution

  • Short answer:

    Your assumptions are correct.

    Long answer:

    It's not possible to modify theme plugins without completely copying them and creating your own, so you're right about that. Also, if there are no hooks or actions on which to activate your code, possibilities to modify the output are limited.

    Depending on the structure of the code in the plugin you're trying to edit, you might be able to build a filter on the output of the function. This means you'll have to write a function that performs a callback to the plugin-function and modifies the response.

    However, I think your solution is the most efficient and it's not worth the trouble trying to find a more elegant solution for this problem. Always try to look for the best combination of efficiency / effectivity.