如何从非function.php文件重写不可插入的父主题函数


How to override a not plugable parent theme function from a non function.php file?

我想减少父主题产生的图像文件的数量。经过一番研究,我发现为此我需要覆盖父主题的php文件(而不是function.php)中的函数,但这是不可能的,因为这个函数不是可插入的(没有包装在if (!function_exists())条件中)。现在,我只是将父函数包装在if (!function_exists())条件中,并在子主题函数.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]);
    }
}

既然原始函数中有一个过滤器,为什么不利用它呢?在您的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;
}