Ecommerce Shopify WordPress Discussion

Cannot redeclare function error in wordpress

I have two plugins in which I am initializing the sdk for data logging. The main concern is that whenever I put the **sdk **initialization code in second plugin, I get a following error: PHP Fatal error: Cannot redeclare wpb_dynamic_init() (previously declared in C:\Users\WPB\Local Sites\wpb-sdk-site\app\public\wp-content\plugins\sdk-test-plugin\wpb-sdk\start.php:14) in C:\Users\WPB\Local Sites\wpb-sdk-site\app\public\wp-content\plugins\related-posts-thumbnails\wpb-sdk\start.php on line 18 The code I am using in the two plugins is: Plugin One: sdk-test-plugin if ( ! function_exists( 'stp_wpb' ) ) { // Create a helper function for easy SDK access. function stp_wpb() { global $stp_wpb; if ( ! isset( $stp_wpb ) ) { // Include SDK. require_once dirname(__FILE__) . '/wpb-sdk/start.php'; $stp_wpb = wpb_dynamic_init( array( 'id' => 10, 'slug' => 'sdk-test-plugin', 'type' => 'plugin', 'public_key' => '7e240de74fb1ed08fa08d38063f6a691462a815', 'is_premium' => false, 'has_addons' => false, 'has_paid_plans' => false, 'menu' => array() ) ); } return $stp_wpb; } // Init . stp_wpb(); // Signal that SDK was initiated. do_action( 'stp_wpb_loaded' ); } Second Plugin: related-posts-thumbnails if (!function_exists('rpt_wpb')) { // Create a helper function for easy SDK access. function rpt_wpb() { global $rpt_wpb; if (!isset($rpt_wpb)) { // Include SDK. require_once dirname(__FILE__) . '/wpb-sdk/start.php'; $rpt_wpb = wpb_dynamic_init(array( 'id' => 7, 'slug' => 'related-posts-thumbnails', 'type' => 'plugin', 'public_key' => '7e240de74fb1ed08fa08d38063f6a691462a815', 'is_premium' => false, 'has_addons' => false, 'has_paid_plans' => false, 'menu' => array() )); } return $rpt_wpb; } // Init . rpt_wpb(); // Signal that SDK was initiated. do_action('rpt_wpb_loaded'); } the start.php file (sdk file) require_once dirname(__FILE__) . '/require.php'; function wpb_dynamic_init($module) { $wpb = Logger::instance($module['id'], $module['slug'], true); // $wpb->dynamic_init($module); return $wpb; } the require.php file require_once dirname( __FILE__ ) . '/config.php'; require_once WPB_SDK_DIR_INCLUDES . '/class-wpb-sdk-logger.php'; And the logger class (class-wpb-sdk-logger.php) <?php /** * Logger responsible for gathering user logs and send. * In effort to provide better customer experience. * * @since 2.0.1 */ /** * Class responsible for logs data collection. */ class Logger { private static $_instances = array(); private $_module_id; private $_slug; /** * Class constructor. * * @return void */ private function __construct($module_id, $slug = false, $is_init = false) { if ( ! $is_init && ! is_numeric($module_id) && ! is_string($slug)) { return false; } $this->_module_id = $module_id; $this->_slug = $slug; $this->hooks(); } public static function instance($module_id, $slug = false, $is_init = false) { if (empty($module_id)) { return false; } if (!$is_init && true === $slug) { $is_init = true; } $key = 'm_' . $module_id; if (!isset(self::$_instances[$key])) { self::$_instances[$key] = new Logger($module_id, $slug, $is_init); } return self::$_instances[$key]; } } Now let me know how to fix this issue as I have created the different instances but still I get issue of Cannot redeclare wpb_dynamic_init()
I dont know how start.php is included, but probably it's included more than once. Quick fix, surrounding your wpb_dynamic_init with if (!function_exists('wpb_dynamic_init')) { // your function } Better solution, find why start.php is included many times. You can use debug_backtrace in start.php to visualize the calls-stack

December 30, 2023

Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.