I have a Main Theme and I am writing a Child Theme for it.
In the functions.php file of the original theme, I added add_custom_background(); to create the ability to customize the theme background and I've added Custom Headers like in the Twenty Ten theme.
Now, I want to disable these functions when the Child Theme is activated.
The question is, how do I do this WITHOUT editing the functions.php file of the Parent Theme?
Michael Fields answers:
I would do this in the parent theme:
if ( ! function_exists( 'add_custom_background' ) ) {
function add_custom_background() {
// Custom Background code goes here...
}
}
I would do this in the child theme:
function add_custom_background() {
return false;
}
if, however, you do not have control over the parent at all - ie, someone else wrote it, another idea is to use the [[LINK href="http://codex.wordpress.org/Function_Reference/remove_action"]]remove_action()[[/LINK]] function.
Armand Morin comments:
I modified the code to remove one and it worked.
The problem lies with the add_custom_background(); since it is not called like...
function add_custom_background();
It is added by just placing... add_custom_background(); in the functions file.
Any idea of how to remove this one? I tried remove_action and that didn't work.
I also tried this, which worked for the previous function.
if ( ! function_exists( 'add_custom_background' ) ) {
function add_custom_background() {
return false;
}
}
Michael Fields comments:
My bad, I thought that add_custom_background() was a theme specific function while it is really a core function. Are you creating the "parent" theme yourself or are you working with one that you do not wish to edit?
If you are making it yourself, then you should take a tip from TwentyTen and use the following structure in the parent:
add_action( 'after_setup_theme', 'twentyten_setup' );
if ( ! function_exists( 'twentyten_setup' ) ):
function twentyten_setup() {
// Lot's of stuff
add_custom_background();
// Lot's of other stuff
}
}
If you are using a parent theme coded by someone else that you do not wish to overwrite, the only thing that I can really suggest is to contact the author and request that they release a new version of the theme which will allow you to properly create a child theme.
Unfortunately, WordPress will not allow you to use the following, I can't figure out why:
remove_theme_support( 'custom-background' );
Armand Morin comments:
Got it, worked perfectly. thanks.
Peter Michael answers:
If you have twentyten as parent, you need to remove the action 'after_setup_theme':
remove_action( 'after_setup_theme', 'twentyten_setup' );
and implement the action & function in your own functions.php
Michael Fields link would be the correct answer.
Gabriel Reguly answers:
Hi Armand,
There is a rather easy solution.
Just set $GLOBALS['custom_background'].
As in $GLOBALS['custom_background'] = '';
That will do the trick for you.
Kind Regards,
Gabriel Reguly