I am trying to implement divi custom module inside WordPress website, built with divi. Currently, I am using Local (localwp) as a way for local development. The custom module will be used to fetch data from MongoDB and display all the data (similar to the blog posts module, for those who are familiar with divi). However I am running into some serious issues, when it comes to integrating MongoDB with PHP. At first, I forgot to include the autoload.php file from the vendor, but right now, the problem seems different.
Here is full log of the error:
Fatal error: Uncaught Error: Class "MongoDB\Driver\Manager" not found in C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\plugins\test\vendor\mongodb\mongodb\src\Client.php:118 Stack trace: #0 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\plugins\test\includes\modules\Exams\Exams.php(55): MongoDB\Client->__construct('mongodb://produ...') #1 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\plugins\test\includes\modules\Exams\Exams.php(39): Exams->get_mongodb_data() #2 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\class-et-builder-element.php(3433): Exams->render(Array, '', 'test_exams', '', '', '', '', '') #3 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(433): ET_Builder_Element->render(Array, '', 'test_exams') #4 [internal function]: do_shortcode_tag(Array) #5 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(273): preg_replace_callback('/\[(\[?)(et_pb...', 'do_shortcode_ta...', '[et_pb_text _bu...') #6 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\main-structure-elements.php(3784): do_shortcode('[et_pb_text _bu...') #7 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\class-et-builder-element.php(3433): ET_Builder_Column->render(Array, '[et_pb_text _bu...', 'et_pb_column', '', '', '', '', '') #8 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(433): ET_Builder_Element->_render(Array, '[et_pb_text bu...', 'et_pb_column') #9 [internal function]: do_shortcode_tag(Array) #10 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(273): preg_replace_callback('/\[(\[?)(et_pb...', 'do_shortcode_ta...', '[et_pb_column t...') #11 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\main-structure-elements.php(2274): do_shortcode('[et_pb_column t...') #12 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\class-et-builder-element.php(3433): ET_Builder_Row->render(Array, '[et_pb_column t...', 'et_pb_row', '', '', '', '', '') #13 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(433): ET_Builder_Element->render(Array, '[et_pb_column t...', 'et_pb_row') #14 [internal function]: do_shortcode_tag(Array) #15 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(273): preg_replace_callback('/\[(\[?)(et_pb...', 'do_shortcode_ta...', '[et_pb_row colu...') #16 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\main-structure-elements.php(1606): do_shortcode('[et_pb_row colu...') #17 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\class-et-builder-element.php(3433): ET_Builder_Section->render(Array, '[et_pb_row colu...', 'et_pb_section', '', '', '', '', '') #18 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(433): ET_Builder_Element->render(Array, '[et_pb_row colu...', 'et_pb_section') #19 [internal function]: do_shortcode_tag(Array) #20 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(273): preg_replace_callback('/\[(\[?)(et_pb...', 'do_shortcode_ta...', '<div class="et-...') #21 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\class-wp-hook.php(324): do_shortcode('<div class="et-...') #22 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\plugin.php(205): WP_Hook->apply_filters('<div class="et-...', Array) #23 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\core.php(26): apply_filters('et_builder_rend...', '[et_pb_section ...') #24 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\frontend-builder\theme-builder\frontend.php(347): et_builder_render_layout('[et_pb_section ...') #25 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\frontend-builder\theme-builder\frontend.php(506): et_theme_builder_frontend_render_layout('et_body_layout', 8) #26 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\frontend-builder\theme-builder\frontend-body-template.php(10): et_theme_builder_frontend_render_body(8, true, false) #27 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\template-loader.php(106): include('C:\Users\qhuser...') #28 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-blog-header.php(19): require_once('C:\Users\qhuser...') #29 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\index.php(17): require('C:\Users\qhuser...') #30 {main} thrown in C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\plugins\test\vendor\mongodb\mongodb\src\Client.php on line 118
Here is how my file is structured:
<?php
require __DIR__ . "../../../../vendor/autoload.php";
// require 'vendor/autoload.php';
use MongoDB\Client;
class Exams extends ET_Builder_Module
{
public $slug = 'test_exams';
public $vb_support = 'on';
protected $module_credits = array(
'module_uri' => '',
'author' => 'Ilia',
'author_uri' => '',
);
public function init()
{
$this->name = esc_html__('Exams', 'test-test');
}
public function get_fields()
{
return array(
'content' => array(
'label' => esc_html__('Content', 'test-test'),
'type' => 'tiny_mce',
'option_category' => 'basic_option',
'description' => esc_html__('Content entered here will appear inside the module.', 'test-test'),
'toggle_slug' => 'main_content',
),
);
}
public function render($attrs, $content = null, $render_slug)
{
$mongodb_data = $this->get_mongodb_data();
// Pass MongoDB data to React component
$props = array(
'content' => $content,
'mongodbData' => $mongodb_data,
);
return $this->shortcode_content($props);
// return sprintf( '<h1>%1$s</h1>', $this->props['content'] );
}
private function get_mongodb_data()
{
$uri = "mongodb://production:23yPum0%7B4O4y%3Eg%27%26b%3Fz%21@37.60.245.139:27017/?authSource=admin";
$client = new Client($uri);
$db = $client->selectDatabase("testyourself");
$collection = $db->selectCollection("examtypes");
$result = $collection->find();
$data = [];
foreach ($result as $document) {
$data[] = $document;
}
return $data;
}
}
new Exams ?>
<? phpinfo() ?>
Here is my file structure:
The wordpress website, created via Local, doesn't seem to include any support for the php_mongodb, when I call the php_info() function, but I am not sure if it even needs to.
At first, I integrated support for that plugin inside my xampp, and I am sure that mongodb is supported there, when I run the php -i | Select-String "mongo"
command in powershell inside that directory, it shows that the necessary packages are there:
Then I proceeded to start adding support for mongo inside the Local created website, but I don't think it is needed, because only the plugin will use it (in my opinion).
So right now, I have come to a dead end with this error.
This is how my vendor is structured:
And here is the part inside the vendor, where the error is thrown:
I've got it figured out!
I wanted to share the solution in case it helps someone else down the line:
When you're building a website for local development (like I did with Localwp), and you're enhancing the functionality of the WordPress site (such as adding a custom plugin with the npx create-divi-extension command), it's essential to ensure that any additional libraries and extensions you include in the custom plugin (like MongoDB in my case) are also supported by the actual Localwp website.
For Windows users, here's what you need to do: if you add extension=php_mongodb.dll in the php.ini file of the plugin and place the php_mongodb.dll file (with the correct version and type safety) in the /ext folder, which the plugin uses, you also need to add that extension to the appropriate php.ini & /ext folders of the Localwp website. These folders can be found here: C:\Users...\AppData\Roaming\Local\lightning-services\ & C:\Users...\AppData\Roaming\Local\run.
I hope this helps someone out there!