3

I created a plugin to override a theme's function. As I learn that function in plugin loads first but I got an error

Fatal error: Cannot redeclare wooc_extra_register_fields() (previously declared in ****/themes/****/functions.php:247) in ***/plugins/custom-plugin/custom-plugin.php on line 89

Not sure what I am doing wrong. Also put theme's functions need to be override in if !function exist. So what is the right way to override a theme function wrap in !function exist using a plugin??

2

2 Answers 2

1
In wp-settings.php, WordPress first checks for any must-use plugins (plugins in the optional mu-plugins folder) and loads those.

Then, if you're running a multisite installation, it checks for plugins that are network-activated and loads those.

0
1

For your situation it would be best to wrap the plugin in function_exists

if ( ! function_exists( 'wooc_extra_register_fields' ) ) {
    function wooc_extra_register_fields() {
    }
}

in the parent theme, as this makes the functions of the theme pluggable. You can then create a child theme and redeclare the function in the child theme's functions.php

2
  • I already do wrap theme function on !function_exists. but the functions in theme are loaded before plugin so it doesn't help Commented Dec 16, 2016 at 10:22
  • Tunji is correct. If you put the code in the child theme, it's functions.php will be loaded before the parent's functions.php
    – johnh10
    Commented Dec 16, 2016 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.