How to override a non-plugin function of a parent theme from a non function.php file?

I want to reduce the number of image files created by the parent theme. After a little research, I found that for this I need to redefine the function from the php file ( not function.php ) of the parent theme, but this is impossible because this function does not connect (not wrapped in the condition if (! Function_exists ())). For now, I just wrapped the parent function in the if (! Function_exists ()) condition and redefined it in the child.php file of the child theme.

In the described situation, can I override the parent function that does not change the parent theme? I ask here because I have no reaction from the developer.

I tried to remove the parent function of the theme with the following code in my child theme and in the plugin, but this did not help:

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 larger, so I only saved 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 is 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 function of the child theme, which overrides 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]); } } 
+2
source share
1 answer

Since the original function has a filter in the function, why not take advantage of this? In your functions.php , something like (unverified 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 can use the $sizes input variable to change individual records, for example

 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; } 
+1
source

All Articles