如何从非function.php文件覆盖不可插入的父主题函数?

时间:2015-07-08 05:49:49

标签: php wordpress override

我想减少父主题产生的图像文件数量。经过一番研究后,我发现为此我需要覆盖父主题的php文件( not function.php )中的函数,但这是不可能的,因为此函数不可插入(未包含在 if(!function_exists())条件中)。现在,我只是将父函数包装在 if(!function_exists())条件中,并在我的子主题function.php文件中重写它。

在描述的情况下,是否可以覆盖父函数而不更改父主题?我在这里问,因为我没有得到开发者的任何反应。

我尝试使用我的子主题和插件中的下一个代码删除父主题函数,但这没有帮助:

function remove_fastnews_actions() {
    remove_action('after_setup_theme','kopa_front_after_setup_theme');
}
add_action('after_setup_theme','remove_fastnews_actions');
//add_action('wp_loaded','remove_fastnews_actions');

这是我需要覆盖的功能(它更大,所以我只注意到我真正需要改变的地方):

add_action('after_setup_theme', 'kopa_front_after_setup_theme');

function kopa_front_after_setup_theme() {
    $sizes = array(
        'flexslider-image-size'   => array(500, 500, TRUE, __('Flexslider Post Image (Kopatheme)', kopa_get_domain())),
        'article-list-image-size' => array(300, 277, TRUE, __('Article List Post Image (Kopatheme)', kopa_get_domain())),
        'article-list-sm-image-size' => array(120, 120, TRUE, __('Article List Small Post Image (Kopatheme)', kopa_get_domain())),
        'article-carousel-image-size' => array(354, 354, TRUE, __('Article Carousel Post Image (Kopatheme)', kopa_get_domain())),
        'entry-list-image-size' => array(227, 182, TRUE, __('Entry List Thumbnail Image (Kopatheme)', kopa_get_domain())),
        'blog-image-size' => array(680, 419, TRUE, __('Blog Image Size (Kopatheme)', kopa_get_domain())),
    );

    apply_filters('kopa_get_image_sizes', $sizes);

    foreach ($sizes as $slug => $details) {
        add_image_size($slug, $details[0], $details[1], $details[2]);
    }
}

相同的父函数可插入:

if( !function_exists('kopa_front_after_setup_theme') )
{

add_action('after_setup_theme', 'kopa_front_after_setup_theme');

function kopa_front_after_setup_theme() {
    $sizes = array(
        'flexslider-image-size'   => array(500, 500, TRUE, __('Flexslider Post Image (Kopatheme)', kopa_get_domain())),
        'article-list-image-size' => array(300, 277, TRUE, __('Article List Post Image (Kopatheme)', kopa_get_domain())),
        'article-list-sm-image-size' => array(120, 120, TRUE, __('Article List Small Post Image (Kopatheme)', kopa_get_domain())),
        'article-carousel-image-size' => array(354, 354, TRUE, __('Article Carousel Post Image (Kopatheme)', kopa_get_domain())),
        'entry-list-image-size' => array(227, 182, TRUE, __('Entry List Thumbnail Image (Kopatheme)', kopa_get_domain())),
        'blog-image-size' => array(680, 419, TRUE, __('Blog Image Size (Kopatheme)', kopa_get_domain())),
    );

    apply_filters('kopa_get_image_sizes', $sizes);

    foreach ($sizes as $slug => $details) {
        add_image_size($slug, $details[0], $details[1], $details[2]);
    }
}

}

覆盖父函数的子主题函数:

add_action('after_setup_theme', 'kopa_front_after_setup_theme');

function kopa_front_after_setup_theme() {
    $sizes = array(
        'flexslider-image-size'   => array(0, 0, TRUE, __('Flexslider Post Image (Kopatheme)', kopa_get_domain())),
        'article-list-image-size' => array(0, 0, TRUE, __('Article List Post Image (Kopatheme)', kopa_get_domain())),
        'article-list-sm-image-size' => array(120, 120, TRUE, __('Article List Small Post Image (Kopatheme)', kopa_get_domain())),
        'article-carousel-image-size' => array(0, 0, TRUE, __('Article Carousel Post Image (Kopatheme)', kopa_get_domain())),
        'entry-list-image-size' => array(0, 0, TRUE, __('Entry List Thumbnail Image (Kopatheme)', kopa_get_domain())),
        'blog-image-size' => array(0, 0, TRUE, __('Blog Image Size (Kopatheme)', kopa_get_domain())),
    );

    apply_filters('kopa_get_image_sizes', $sizes);

    foreach ($sizes as $slug => $details) {
        add_image_size($slug, $details[0], $details[1], $details[2]);
    }
}

1 个答案:

答案 0 :(得分:1)

由于原始函数在函数中有一个过滤器,为什么不利用它呢?在functions.php中,类似于(未经测试的代码):

add_filter('kopa_get_image_sizes', 'override_kopa_images');

function override_kopa_images($sizes) {
    $new_sizes = array(
        'flexslider-image-size'   => array(0, 0, TRUE, __('Flexslider Post Image (Kopatheme)', kopa_get_domain())),
        'article-list-image-size' => array(0, 0, TRUE, __('Article List Post Image (Kopatheme)', kopa_get_domain())),
        'article-list-sm-image-size' => array(120, 120, TRUE, __('Article List Small Post Image (Kopatheme)', kopa_get_domain())),
        'article-carousel-image-size' => array(0, 0, TRUE, __('Article Carousel Post Image (Kopatheme)', kopa_get_domain())),
        'entry-list-image-size' => array(0, 0, TRUE, __('Entry List Thumbnail Image (Kopatheme)', kopa_get_domain())),
        'blog-image-size' => array(0, 0, TRUE, __('Blog Image Size (Kopatheme)', kopa_get_domain())),
    );
    return $new_sizes;
}

或者,您可以使用输入变量$sizes来更改单个条目,例如

add_filter('kopa_get_image_sizes', 'override_kopa_images');

function override_kopa_images($sizes) {
    $sizes['flexslider-image-size'] = array(0, 0, TRUE, __('Flexslider Post Image (Kopatheme)', kopa_get_domain()));
    return $sizes;
}