如何从非 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()) 条件中,并在我的子主题 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]);
}
}
既然原函数在函数中有一个过滤器,为什么不利用它呢?在您的 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;
}
我想减少父主题生成的图像文件的数量。经过一些研究,我发现为此我需要覆盖父主题的 php 文件( 而不是 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]);
}
}
既然原函数在函数中有一个过滤器,为什么不利用它呢?在您的 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;
}