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

customization - Word Press Theme Customiser and Child theme Function.php

programmeradmin0浏览0评论

I've cast a pretty wide net to find an answer but nothing seems to quite hit the mark.

Process:: I Login to my site/dashboard and I decide I need to edit a portion of my theme through the customiser, so I do, pretty straight forward...

In the mean time, I've built up my child theme and it seems to work pretty well for a novice and have generally figured out how to duplicate and edit particular .php files such as the footer, header etc... and, change (template to stylesheet) to load child theme images; however!

I wanted to change the social media links in my footer and had to scratch my head for a fews before figuring out how the customiser works and where the .php files are stored so I could change and add new fab fa.icons icon.

Problem:: my child theme function.php is doing what it needs to be doing, likewise with my pages, and is safeguarded form theme updates, however! not my new "customeriser" changes, so I have to go back in and change them time over...

I have found the location of the directory for the customiser, hence the ability to edit it, but to safeguard it from being changed is another thing.

The customiser files are duplicated to the child directory, then I load (along with tried variations of include, stylesheet, url)

require get_template_directory() . '/inc/business-prime-customizer.php'; 

it crashes my site...

So how do I point my child function.php at that new customiser address/files?

Many thanks and greatly appreciated

John

I've cast a pretty wide net to find an answer but nothing seems to quite hit the mark.

Process:: I Login to my site/dashboard and I decide I need to edit a portion of my theme through the customiser, so I do, pretty straight forward...

In the mean time, I've built up my child theme and it seems to work pretty well for a novice and have generally figured out how to duplicate and edit particular .php files such as the footer, header etc... and, change (template to stylesheet) to load child theme images; however!

I wanted to change the social media links in my footer and had to scratch my head for a fews before figuring out how the customiser works and where the .php files are stored so I could change and add new fab fa.icons icon.

Problem:: my child theme function.php is doing what it needs to be doing, likewise with my pages, and is safeguarded form theme updates, however! not my new "customeriser" changes, so I have to go back in and change them time over...

I have found the location of the directory for the customiser, hence the ability to edit it, but to safeguard it from being changed is another thing.

The customiser files are duplicated to the child directory, then I load (along with tried variations of include, stylesheet, url)

require get_template_directory() . '/inc/business-prime-customizer.php'; 

it crashes my site...

So how do I point my child function.php at that new customiser address/files?

Many thanks and greatly appreciated

John

Share Improve this question edited Jun 22, 2019 at 13:05 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Jun 22, 2019 at 12:30 Shadow CrownShadow Crown 31 bronze badge 6
  • See my answer to a similar question here: wordpress.stackexchange/a/309845/39152 The short version is that you can't just copy any old file from a theme into a child theme and replace it. If there's other PHP files that you want to replace, you'll need to use remove_filter() and remove_action() to un-hook any functionality that they add. – Jacob Peattie Commented Jun 22, 2019 at 12:37
  • Hi Jacob. Is this similar or the same as Chetans answer below? More so as his suggestion gets me half towards and independent (Child) customizer, but I have to manually block out #require get_template_directory() . '/inc/business-prime-customizer.php'; within the parent customizer for it to work? – Shadow Crown Commented Jun 22, 2019 at 15:43
  • Chetan's answer is a specific example of the sort of thing you'll need to do, but the specifics depend on the actual theme. You shouldn't modify the parent theme at all. You just need to remove any hooks that are included in it from within the child theme. – Jacob Peattie Commented Jun 22, 2019 at 16:10
  • Absolutely, otherwise what is the point to the child theme... don't get me wrong, I've implemented both your codes, and to the letter, as a yardstick test and both codes yeild results; but I still have to force the parent function.php file by manually removing the require function. Basically it's not dequeueing automatically and permentally like any other functions? – Shadow Crown Commented Jun 22, 2019 at 19:01
  • You can’t un-require files from a parent theme. – Jacob Peattie Commented Jun 22, 2019 at 19:30
 |  Show 1 more comment

3 Answers 3

Reset to default 0

First remove action of customizer then add your action with new function name.

in your child-theme functions.php

add_action( 'init', 'remove_my_action');
function remove_my_action() {
     remove_action('customize_register', 'business_prime_settings_control',10);
}
require_once dirname( __FILE__ ) . '/inc/business-prime-customizer.php';

in your child-theme/inc/business-prime-customizer.php change name of function and callback

function business_prime_settings_control_child($wp_customize) {
    # here your all customizer settings code
}
add_action('customize_register', 'business_prime_settings_control_child');

Remove other extra actions and declared functions. otherwise you will get : Cannot redeclare Fatal error.

Just wanted to say thank you for your feedback it has, and without a boubt, been very helpful. Otherwise, one answer and its results have been better than the other.

Just a little feedback of my own explorations...

After exploring the net, there does seem to be a slight gap in undersatanding these things, more so referring to us novices/amatures and our questions, equally so, when the answers can be somewhat long and not to the point, so thanks for your patience there.

Child theme creations seem to be quite the moot subject and, based on the parent theme, difficult to ensure they work properly. Therefore, based on your patience and obvious abilities, need to keep working at it, as such I'll keep exploring.

I'm kind of thinking that the answers are within the development process of creating the parent themes itselves, if I choose to invest that much time and money into it, or commission someone. So what worked and yielded a greater result:

As expected and to reduce the impact on the parent files, I duplicated the required parent files into the child theme, then as per advice added the following with a few tweaks.

Child functions.php::

    add_action( 'init', 'remove_my_customizer');
    function remove_my_customizer() {
        remove_action('customize_register', 'business_prime_settings_control',100);
    }
        require_once dirname( __FILE__ ) . '/inc/business-prime-customizer.php';

    add_action( 'business_prime_settings_control_child', 'business_prime_child_customizer',10);

Based on other opinions, my parent theme, and to increase stability, I added the additional add action as a forced loop element. Then I replaced the necessary elements in the new child copy of the customizer::

    function business_prime_settings_control_child($wp_customize) {
        # child.php customizer settings code
    }
    add_action('customize_register', 'business_prime_settings_control_child');

This approach yielded the better result as none of my customizer tabs, coding or functions, where lost and it removed particular parent theme marketing banners.

This approach also worked for inc/functions.php file too::

    add_action( 'init', 'remove_my_functions');
    function remove_my_functions() {
        remove_action('functions_register', 'business_prime_functions',100);
}
        require_once dirname( __FILE__ ) . '/inc/business-prime-functions.php';

    add_action( 'business_prime_functions_child', 'business_prime_child_functions',10);

However, the aspect I was talking about being a "95% resolve" was surrounding the parent main functions.php file::

    #require get_template_directory() . '/inc/business-prime-customizer.php';
    #require get_template_directory() . '/inc/business-prime-functions.php';
    require get_template_directory() . '/inc/business-prime-sanitize-cb.php';

Because the child theme functions is not overriding the hierarchy of the parent theme, out of the nine inc/parent.php files in the directory, I had to manually block out the two necessary ones to force the code to work; which I guess I will have to do again on the next update.

Once again, thank you for time and you've been a great helping hand, keep up the good work.

Kind Regards, John

This should load the file from the child theme.

require get_stylesheet_directory() . '/inc/business-prime-customizer.php';

you should also set WP_DEBUG variable to true in wp-config.php file

if you want to load any file from the parent theme then you will use

require get_template_directory() . '/path_to_the_file.php';
or
require get_template_directory_uri() . '/path_to_the_css_file.css';

if you want to load file from the current child theme or active theme.

require get_stylesheet_directory() . '/path_to_the_file.php';
or
require get_stylesheet_directory_uri() . '/path_to_the_css_file.css';
发布评论

评论列表(0)

  1. 暂无评论