Skip to content

Commit f371c35

Browse files
committed
Added version checking example
1 parent 3b6417a commit f371c35

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,43 @@ add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );
3030
add_action( 'carbon_fields_loaded', 'load_my_magic' );
3131
```
3232

33+
## Version Checking
34+
35+
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.
36+
37+
**Quick example:**
38+
39+
```
40+
add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );
41+
add_action( 'carbon_fields_loaded', 'load_my_magic' );
42+
43+
if(verify_dependencies(['carbon_fields' => '2.0.0'])) {
44+
// Execute my super-awesome plugin logic
45+
}
46+
47+
function verify_dependencies( $deps ) {
48+
// Check if outdated version of Carbon Fields loaded
49+
$error = false;
50+
51+
if(!defined('\\Carbon_Fields\\VERSION')) {
52+
$error = '<strong>' . __('My Plugin Name', 'my-text-domain') . ':</strong> ' . __('A fatal error occurred while trying to load dependencies.', 'my-text-domain');
53+
} else if( version_compare( \Carbon_Fields\VERSION, $deps['carbon_fields'], '<' ) ) {
54+
$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') . ')';
55+
}
56+
57+
if($error) {
58+
add_action( 'admin_notices', function() {
59+
$class = 'notice notice-error';
60+
$message = __( 'Irks! An error has occurred.', 'sample-text-domain' );
61+
62+
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
63+
});
64+
}
65+
66+
return !$error;
67+
}
68+
```
69+
3370
## Frequently Asked Questions
3471

3572
*Q: I get a fatal error when I activate this plugin. What do I do?*
@@ -46,5 +83,5 @@ A: Install [GitHub Updater](https://github.com/afragen/github-updater).
4683

4784
## Changelog
4885

49-
**2.0.3 (master)**
86+
**2.0.3**
5087
* Initial commit

0 commit comments

Comments
 (0)