最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Override a theme function in a child theme?

programmeradmin1浏览0评论

Most if not all of the answers to this question are pretty old, or are for edge cases.

I have a child theme for a commercial theme ("Total"). The parent theme's functions.php loads functions from several php files in its "framework" folder. The function I am targeting is in fonts.php, called wpex_standard_fonts:

if ( ! function_exists( 'wpex_standard_fonts' ) ) {
function wpex_standard_fonts() {
    return array(
        "Arial, Helvetica, sans-serif",
        "Arial Black, Gadget, sans-serif",
         ...
         );
   }
}

So, from what I have googled, I take this add to my child theme functions.php with my changes to it, right? So I add my custom font to the array.

But when I go to the admin tool, my custom font menu item is not there.

What did I miss here?

Most if not all of the answers to this question are pretty old, or are for edge cases.

I have a child theme for a commercial theme ("Total"). The parent theme's functions.php loads functions from several php files in its "framework" folder. The function I am targeting is in fonts.php, called wpex_standard_fonts:

if ( ! function_exists( 'wpex_standard_fonts' ) ) {
function wpex_standard_fonts() {
    return array(
        "Arial, Helvetica, sans-serif",
        "Arial Black, Gadget, sans-serif",
         ...
         );
   }
}

So, from what I have googled, I take this add to my child theme functions.php with my changes to it, right? So I add my custom font to the array.

But when I go to the admin tool, my custom font menu item is not there.

What did I miss here?

Share Improve this question asked Dec 29, 2014 at 20:34 SteveSteve 3139 silver badges21 bronze badges 6
  • Do you make the check of "if function exists" in your child theme, or this is a check in the parent theme? – vlood Commented Dec 29, 2014 at 20:57
  • it was in parent, but I also did it in the child theme. I heard that's it's good practice. – Steve Commented Dec 29, 2014 at 20:59
  • 3 Override parent theme functions is quite easy and it seems that you are doing right. But we can not know why it is not working in your particular case for the "font menu"; we don't know how that "font menu" works or how it is coded. You should ask to the theme developer. – cybmeta Commented Dec 29, 2014 at 21:31
  • Thanks. As long as I did it right. I will check with the dev to see if that's the right function to target. – Steve Commented Dec 29, 2014 at 21:32
  • 2 I din't try it, but thinking, if ( ! function_exists( 'wpex_standard_fonts' ) ) { will return yes there is a function in the parent theme - isn't that? – Mayeenul Islam Commented Dec 30, 2014 at 4:32
 |  Show 1 more comment

1 Answer 1

Reset to default 1

Since you are using a Child Theme, the functions.php file in there is loaded before the parent theme's functions.

So, if you define some_function() in the Child Theme, any declaration of that function in the parent theme will fail. If the parent theme is using a function_exists() to check if some_function() is defined, it should find the function as already being defined, so the parent function will not be loaded.

So, your code to check if the function_exists() is not really needed - since you are in a Child Theme. Only if the parent theme does not do a function_exists for some_function() will there be issues - possibly fatal errors that will disrupt the site.

发布评论

评论列表(0)

  1. 暂无评论