How to override a not plugable parent theme functi

2019-05-26 09:12发布

I want to reduce the number of image files that my parent theme produces. After a little research I found that for this I need to override a function from a php file (not function.php) of the parent theme, but this is impossible because this function is not pluggable (not wrapped in a if (!function_exists()) condition). For now, I just wrapped the parent function in a if (!function_exists()) condition and overridden it in my child theme function.php file.

In the described situation, is possible to override the parent function not changing the parent theme? I ask here because I do not have any reaction from the developer.

I tried to remove the parent theme function with the next code in my child theme and in a plugin, but this didn't helped:

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');

This is the function I need to override (it is bigger, so I keeped here only what I really need to change):

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]);
    }
}

The same parent function made pluggable:

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]);
    }
}

}

And the child theme function that override the parent function:

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条回答
Deceive 欺骗
2楼-- · 2019-05-26 09:33

Since the original function has a filter in the function, why not take advantage of that? In your functions.php, something like (untested code):

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;
}

Alternatively, you could use the input variable $sizes to change individual entries, eg

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;
}
查看更多
登录 后发表回答