0

I am trying to re-arrange Woocommerce Sensei hooked content. Basically, I would like to have course_image at the top (high priority).

// code in sensei /includes/hooks/template.php

<?php
        /**
         * Fires just before the course content in the content-course.php file.
         *
         * @since 1.9
         *
         * @param integer $course_id
         *
         * @hooked Sensei_Templates::the_title          - 5
         * @hooked Sensei()->course->course_image       - 10
         * @hooked  Sensei()->course->the_course_meta   - 20
         */
        do_action('sensei_course_content_inside_before', get_the_ID() );
        ?>

I added: remove_action( 'sensei_course_content_inside_before', 'course_image', 10); in my Theme's function.php but it doesn't work.

How do I re-arrange above title, image and meta?

Please suggest.

Update

//code called add_action
// add course content title to the courses on the archive page
add_action('sensei_course_content_inside_before', array( 'Sensei_Templates', 'the_title' ) ,5, 1 );

// add the course image above the content
add_action('sensei_course_content_inside_before', array( $this->course, 'course_image' ) ,10, 1 ); //if I change 10 to 4 in their code directly, it works

Code can be found here

UPDATE 2

I get following on:

global $wp_filter;
echo '<pre>';
print_r( $wp_filter['sensei_course_content_inside_before'] );
echo '</pre>';

//output
WP_Hook Object
(
    [callbacks] => Array
        (

            [5] => Array
                (
                    [Sensei_Templates::the_title] => Array
                        (
                            [function] => Array
                                (
                                    [0] => Sensei_Templates
                                    [1] => the_title
                                )

                            [accepted_args] => 1
                        )

                )

            [10] => Array
                (
                    [0000000032a2e99b00007fe8c14d85d0the_course_meta] => Array
                        (
                            [function] => Array
                                (
                                    [0] => WooThemes_Sensei_Course Object
                                        (
                                            [token] => course
                                            [meta_fields] => Array
                                                (
                                                    [0] => course_prerequisite
                                                    [1] => course_featured
                                                    [2] => course_video_embed
                                                    [3] => course_woocommerce_product
                                                )

                                            [my_courses_page] => 
                                        )

                                    [1] => the_course_meta
                                )

                            [accepted_args] => 1
                        )
            [10] => Array
                (
                    [0000000032a2e99b00007fe8c14d85d0course_image] => Array
                        (
                            [function] => Array
                                (
                                    [0] => WooThemes_Sensei_Course Object
                                        (
                                            [token] => course
                                            [meta_fields] => Array
                                                (
                                                    [0] => course_prerequisite
                                                    [1] => course_featured
                                                    [2] => course_video_embed
                                                    [3] => course_woocommerce_product
                                                )

                                            [my_courses_page] => 
                                        )

                                    [1] => course_image
                                )

                            [accepted_args] => 1
                        )

                )

                    [0000000032a2e99b00007fe8c14d85d0content_before_backwards_compatibility_hooks] => Array
                        (
                            [function] => Array
                                (
                                    [0] => WooThemes_Sensei_Course Object
                                        (
                                            [token] => course
                                            [meta_fields] => Array
                                                (
                                                    [0] => course_prerequisite
                                                    [1] => course_featured
                                                    [2] => course_video_embed
                                                    [3] => course_woocommerce_product
                                                )

                                            [my_courses_page] => 
                                        )

                                    [1] => content_before_backwards_compatibility_hooks
                                )

                            [accepted_args] => 1
                        )

                )

        )

    [iterations:WP_Hook:private] => Array
        (
        )

    [current_priority:WP_Hook:private] => Array
        (
        )

    [nesting_level:WP_Hook:private] => 0
    [doing_action:WP_Hook:private] => 
)
4
  • Where did you add the remove_action() code? Maybe the action wasn't added yet (in terms of code execution order)
    – kero
    Commented Sep 24, 2018 at 1:04
  • I have added in theme/my-theme/functions.php. Commented Sep 24, 2018 at 1:11
  • Can you add the code corresponding to this to the question? You'll probaby have something like add_action('init', 'my_func'); with my_func having the remove_action(), right? Try add_action('init', 'my_function', 99); instead - but it depends on how/when Sensei adds the action, I don't know that
    – kero
    Commented Sep 24, 2018 at 1:15
  • Updated my question. Commented Sep 24, 2018 at 1:18

1 Answer 1

2

To override action that use in plugin, use can wrap the remove_action code in template_redirect action to make sure your action will be excute later.

For example

add_action('template_redirect', function() {

    // Remove the title.
    remove_action('sensei_course_content_inside_before', array( 'Sensei_Templates', 'the_title' ) ,5, 1 );

    // Add it again
    add_action('sensei_course_content_inside_before', array( 'Sensei_Templates', 'the_title' ) ,15, 1 );
}, 11 );

If it doesn't work, you can try to increase priority form 11 to something greater.

Note that if your PHP version doesn't support anonymous function, you need to change the wrap function to a named function.

UPDATE

Why template_redirect?

Because it's hard to know (IMO) the order of action that plugins excute, so we can try to catch up the order of all actions later, after all of them has been add/remove. template_redirect is one of "later" actions that can help use re-order the actions because this action hook executes just before WordPress determines which template page to load.

UPDATE 2

You can try this code for course_image:

add_action('template_redirect', function() {

    // Remove the image.
    remove_action('sensei_course_content_inside_before', array( Sensei()->course, 'course_image' ), 10, 1);

    // Add it again
    add_action('sensei_course_content_inside_before', array( Sensei()->course, 'course_image' ), 1, 1 );
}, 11 );
15
  • Can you elaborate (by editing the answer), why you advise using the template_redirect action?
    – kero
    Commented Sep 24, 2018 at 7:46
  • @kero I did that, please check my update ;)
    – Tung Du
    Commented Sep 24, 2018 at 7:54
  • Makes sense. I try to be precise (if I have the time): If we search through the repository, we'll see where exactly the file is loaded: Sensei_Main->load_hooks(), which in turn is called from Sensei_Main->__construct(), which is then called from the root file (via Sensei_Main::instance()). Which, in turn, isn't called via an action at all :(
    – kero
    Commented Sep 24, 2018 at 8:42
  • Not a best practice (in my opinion). If it were called from another action, we could've used that exact same hook with higher priority
    – kero
    Commented Sep 24, 2018 at 8:43
  • 1
    Small trick for loading the action after all other plugins in case you need to do this - wordpress.stackexchange.com/questions/63141/… (see the last comment in this post).
    – Clinton
    Commented Sep 25, 2018 at 9:59

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.