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??

cjbj
  • 15,004
  • 16
  • 42
  • 89
user2834491
  • 31
  • 1
  • 2

2 Answers2

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.

Sivaraj-v
  • 111
  • 3
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

Tunji
  • 2,961
  • 1
  • 19
  • 28