PHPackages                             publishpress/instance-protection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. publishpress/instance-protection

ActiveLibrary

publishpress/instance-protection
================================

Library for protecting WordPress plugins to run twice.

2.1.0(5mo ago)03.6k—8.3%1[2 issues](https://github.com/publishpress/library-instance-protection/issues)GPL-3.0-or-laterPHPPHP &gt;=7.2.5

Since May 2Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/publishpress/library-instance-protection)[ Packagist](https://packagist.org/packages/publishpress/instance-protection)[ Docs](http://publishpress.com/)[ RSS](/packages/publishpress-instance-protection/feed)WikiDiscussions development Synced 1mo ago

READMEChangelog (7)Dependencies (1)Versions (12)Used By (0)

PublishPress Instance Protection
================================

[](#publishpress-instance-protection)

This is a library for protecting WordPress plugins to run twice instances at the same time.

Installation
------------

[](#installation)

This library should be added as a composer requirement running the command:

```
composer require publishpress/instance-protection
```

How to use it
-------------

[](#how-to-use-it)

### Making sure the plugin do not break with more running instances

[](#making-sure-the-plugin-do-not-break-with-more-running-instances)

The first step, before even requiring this library, is making sure your plugin do not break if it has two or more instances running at the same time on the site.

This is easy to accomplish using constants defined after the plugin is loaded, and checking it before loading the plugin. If the constant is defined, we do not load the plugin again (and not even require its libraries).

This verification has to be out of any hook.

Make sure to rename the variables and constant according to your plugin. This example copy the PublishPress Authors code:

#### Free plugin example

[](#free-plugin-example)

```
if (! defined('PP_AUTHORS_LOADED')) {
    // include libraries, and dependencies
    // instantiate the plugin, hooks, etc

    define('PP_AUTHORS_LOADED', true);
}
```

#### Pro plugin example

[](#pro-plugin-example)

```
if (! defined('PP_AUTHORS_PRO_LOADED') && ! defined('PP_AUTHORS_LOADED')) {
    // include libraries, and dependencies
    // instantiate the plugin, hooks, etc
    // initialize the free plugin

    define('PP_AUTHORS_PRO_LOADED', true);
}
```

Please note that the Pro plugin checks two constants: its own constant, and the one defined in the free plugin. This way the Pro won't run if the free is already running as a stand alone plugin.

#### Functions

[](#functions)

Before defining functions on the global escope always add it inside a conditional using `function_exists`. If they are defined on a speicifc `includes.php` file for example, you can use only one conditional before including it, instead of adding the conditional to every function.

#### Classes

[](#classes)

Before declaring classes, follow the same approach on the previous topic, but using `class_exists`.

### Adding the admin notices library

[](#adding-the-admin-notices-library)

This library do not use composer's autoloader because it needs to be loaded before everything else in the plugins. But it has its own autoloader inside it, following the PSR-4 pattern.

You should always check if the vendor folder is on the expected path before trying to include anything from inside it. If not on the standard folder, make sure to give an option to the use to define a constant that gives the custom path of the vendor directory.

**Check the following example on how to include and instantiate the library, just make sure to add this code as the first thing that will be executed in your plugin, on the global escope, out of any hook.**

#### Free Plugin example

[](#free-plugin-example-1)

```
