Skip to content
Daniel M. Hendricks edited this page Aug 6, 2017 · 2 revisions

This document is intended to give some examples of using this plugin in conjunction with your own plugin/theme/code.

For more information on how to use the framework, please see the official Carbon Fields documentation.

Usage with Plugin/Theme

If you want to use this plugin to load Carbon Fields for use with your plugin or theme, you would likely want to do something like this:

function do_plugin_logic() {
   // Load plugin logic
}

add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );
add_action( 'carbon_fields_loaded', 'do_plugin_logic' );

For more information, please consult the Carbon Fields Carbon Fields Quickstart.

Version Checking

Since it is possible that an older version of the plugin may be installed and/or loaded as a dependency in a theme or plugin, you may wish to do version checking if your code requires a newer version.

Quick example:

function verify_dependencies( $deps ) {
  // Check if outdated version of Carbon Fields loaded
  $error = false;

  if(!defined('\\Carbon_Fields\\VERSION')) {
    $error = '<strong>' . __('My Plugin Name', 'my-text-domain') . ':</strong> ' . __('A fatal error occurred while trying to load dependencies.', 'my-text-domain');
  } else if( version_compare( \Carbon_Fields\VERSION, $deps['carbon_fields'], '<' ) ) {
    $error = '<strong>' . __('My Plugin Name', 'my-text-domain') . '] . ':</strong> ' . __('Danger, Will Robinson! An outdated version of Carbon Fields has been loaded: ' . \Carbon_Fields\VERSION) . ' (&gt;= ' . $deps['carbon_fields'], 'my-text-domain') . ' ' . __('required', 'my-text-domain') . ')';
  }

  if($error) {
    add_action( 'admin_notices', function() {
      $class = 'notice notice-error';

      printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $error ) );
    });
  }

  return !$error;
}

function do_plugin_logic() {
  if(verify_dependencies(['carbon_fields' => '2.0.0'])) {
    // Load plugin logic
  }
}

add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );
add_action( 'carbon_fields_loaded', 'do_plugin_logic' );
Clone this wiki locally