Skip to content
Andy Fragen edited this page Nov 20, 2021 · 10 revisions

JSON config file format

Here you can download an example wp-dependencies.json configuration file.

Rules:

  • This file must be named wp-dependencies.json and it must be in the root directory of your plugin or theme.
  • You may use a shorthand uri such as <owner>/<repo> as the JSON slug value.
  • You may use required or optional as interchangable opposites. If a plugin is required then it is not optional, and the reverse is true.
  • You may also use a specific git hosts token for installing your plugins from a private GitHub, GitLab, Bitbucket, etc. repository.
  • Bitbucket can only use the combination of username:password as an authentication token. Additionally the colon : must be a separator. You can create additional passwords that only have read privileges for this purpose. Create a read-only app password for this. The username is not an email address.
[
  {
    "name": "Query Monitor",
    "host": "wordpress",
    "slug": "query-monitor/query-monitor.php",
    "uri": "https://wordpress.org/plugins/query-monitor/",
    "required": true
  },
  {
    "name": "Git Updater",
    "host": "github",
    "slug": "git-updater/git-updater.php",
    "uri": "afragen/git-updater",
    "branch": "master",
    "optional": false,
    "token": null
  },
  {
    "name": "Test Plugin Notags",
    "host": "bitbucket",
    "slug": "test-plugin-notags/test-plugin-notags.php",
    "uri": "https://bitbucket.org/afragen/test-plugin-notags",
    "branch": "master",
    "required": false,
    "token": "my-bitbucket-username:my-readonly-app-password"
  },
  {
    "name": "Test Gitlab Plugin2",
    "host": "gitlab",
    "slug": "test-gitlab-plugin2/test-gitlab-plugin2.php",
    "uri": "https://gitlab.com/afragen/test-gitlab-plugin2",
    "branch": "develop",
    "optional": true,
    "token": null
  },
  {
    "name": "Test Direct Plugin Download",
    "host": "direct",
    "slug": "test-direct-plugin/test-plugin.php",
    "uri": "https://direct-download.com/path/to.zip",
    "required": false
  }
]

NB JSON files are potentially visible to anyone on the web, if you are concerned about security, try to take a look at one of the following solutions

Associative Array config

If you want to programmatically add dependencies you can send an associative array directly like the following:

include_once( __DIR__ . '/vendor/autoload.php' );

$config = [
  [
    'name'    => 'Query Monitor',
    'host'    => 'wordpress',
    'slug'    => 'query-monitor/query-monitor.php',
    'uri'     => 'https://wordpress.org/plugins/query-monitor/',
    'required' => true,
  ],
  [
    'name'     => 'Debug Bar',
    'host'     => 'wordpress',
    'slug'     => 'debug-bar/debug-bar.php',
    'uri'      => 'https://wordpress.org/plugins/debug-bar/',
    'optional' => true,
  ],
];

WP_Dependency_Installer::instance( __DIR__ )->register( $config )->run();

where $config is an associative array as in identical format as:

json_decode( << your wp-dependencies.json file >> )

Dynamic Array / JSON config

Alternatively, if you are a lazy people, you can also use this simple solution to avoid manual conversions of your JSON file into an associative array:

require_once __DIR__ . '/vendor/autoload.php';

$config = <<<JSON
[
  {
    "name": "Git Updater",
    "host": "github",
    "slug": "git-updater/git-updater.php",
    "uri": "afragen/git-updater",
    "branch": "develop",
    "required": true,
    "token": null
  },
  {
    "name": "WP Debugging",
    "host": "WordPress",
    "slug": "wp-debugging/wp-debugging.php",
    "uri": "https://wordpress.org/plugins/wp-debugging/",
    "required": false
  },
  {
    "name": "Local Development",
    "host": "WordPress",
    "slug": "local-development/local-development.php",
    "uri": "https://wordpress.org/plugins/local-development/",
    "required": true
  }
]
JSON;
$config = json_decode( $config, true );

WP_Dependency_Installer::instance( __DIR__ )->register( $config )->run();

Pro tip:

For advanced configurations, this library also provides some useful Actions and Hooks that will allow you to further customize your plugin.

For example, you can display your plugin friendly name aside the dismissable notifications, like the following:

Before

After

Simply by adding the following filter:

/**
 * Group Plugin Installer
 *
 * @author  Andy Fragen
 * @license MIT
 * @link    https://github.com/afragen/group-plugin-installer
 * @package group-plugin-installer
 */

require_once __DIR__ . '/vendor/autoload.php';
WP_Dependency_Installer::instance( __DIR__ )->run();

add_filter(
  'wp_dependency_dismiss_label',
  function( $label, $source ) {
    $label = basename(__DIR__) !== $source ? $label : __( 'Group Plugin Installer', 'group-plugin-installer' );
    return $label;
  },
  10,
  2
);