How to properly include CMB2 in sage 9

sage9
composer

#1

So I installed CMB2 with composer, but from looking at their github issues seems they aren’t actually supporting autoloading, so do we just require that sucker? or am I missing something?


#2

Are you installing CMB2 with Composer within your Sage project directory? Or are you installing it as a plugin from the root of your WordPress (Bedrock?) project?

Since Sage uses composer-installers, it will install packages with "type": "wordpress-plugin" (i.e. CMB2) in ./wp-content/plugins relative to Sage’s root—which of course won’t work for us.

To install it as a package in Sage, add this to your composer.json to override where it’s installing CMB2 and to autoload it:

  //...
  "autoload": {
    //...
    "files": [
      "vendor/webdevstudios/cmb2/init.php"
    ]
  },
  //...
  "extra": {
    "installer-paths": {
      "vendor/webdevstudios/cmb2": ["webdevstudios/cmb2"]
    }
  },
  //...

Once you’ve made this change, run composer install to reinstall CMB2 in the right place and composer dumpautoload to make sure it works.

If you install it as a plugin at the WP/Bedrock level, you don’t need to worry about any of this.


#3

Thanks, Knowler. I’m using Composer, this got it all sorted.


#4

Hi. I’ve installed CMB2 in my project using the composer.json method you describe above but I can’t get examlpe-functions.php to load into my template. I’ve checked composer/autoload_files.php and CMB2 is showing up there so am presuming it’s loading when I start my project (yarn start)?

I’m trying to adding the following code:
require_once( dirname(__FILE__) . '/cmb2/examlpe-functions.php');
in my app/setup.php file but I get a 500 error when I refresh the browser… This is just so I can test that CMB2 is working, I would like to have a custom metabox file load from my assets folder once I know its working properly.

The above code worked when I used it in my function.php file using Sage 9.0.0-beta but this new version (Sage 9.0.1) is taking me some time to figure out…

Any help would be appreciated.


#5

require_once( dirname(__FILE__) . '/cmb2/examlpe-functions.php');

You have misspelled “example” here, which may be an issue?

It’s unclear to me where the example-functions.php file comes from. Did you create it? Is it part of the CMB2 package? If the later, then your require snippet won’t work: Because you’ve put that snippet in app/setup.php, PHP is trying to require app/cmb2/examlpe-functions.php, which doesn’t exist. If that file is part of the CMB2 package, then it was installed in the vendor directory with the rest of the package.