如何重新安排这些上钩的内容?

时间:2018-09-24 作者:Adarsh Khatri

我正在尝试重新安排Woocommerce唤醒者的钩住内容。基本上,我想course_image 处于最高(高优先级)。

// 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() );
        ?>
我补充道:remove_action( \'sensei_course_content_inside_before\', \'course_image\', 10); 在我的主题中function.php 但它不起作用。

如何重新安排上述内容title, image and meta?

请提出建议。

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
可以找到代码here

UPDATE 2

我得到以下信息:

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] => 
)

1 个回复
最合适的回答,由SO网友:Tung Du 整理而成

要覆盖插件中使用的操作,请使用可以包装remove_action 代码输入template_redirect 确保您的操作稍后执行。

例如

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 );
如果它不起作用,您可以尝试增加优先级形式11 更伟大的东西。

请注意,如果您的PHP版本不支持匿名函数,则需要将wrap函数更改为命名函数。

UPDATE

为什么?template_redirect?

因为很难知道(IMO)插件执行的操作顺序,所以我们可以在所有操作都被添加/删除之后,尝试稍后跟踪所有操作的顺序。template_redirect 是可以帮助使用重新排序操作的“稍后”操作之一,因为此操作挂钩在WordPress确定加载哪个模板页面之前执行。

UPDATE 2

您可以尝试此代码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 );

结束

相关推荐

Wordpress Admin Tooltip hooks

我想知道是否有一种方法可以使用Wordpress管理工具提示(灰色和蓝色),当你更新你的Wordpress(3.x)时会显示这些提示。显示新功能。谢谢