https://github.com/mnestorov/wp-multisite-cookie-manager
Manage cookies across a WordPress multisite network.
https://github.com/mnestorov/wp-multisite-cookie-manager
wordpress wordpress-cookie wordpress-multisite wordpress-plugin wp wp-admin wp-multisite wp-plugin
Last synced: 11 months ago
JSON representation
Manage cookies across a WordPress multisite network.
- Host: GitHub
- URL: https://github.com/mnestorov/wp-multisite-cookie-manager
- Owner: mnestorov
- License: gpl-2.0
- Created: 2023-10-09T11:16:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-30T14:08:19.000Z (about 2 years ago)
- Last Synced: 2025-01-22T15:28:28.152Z (about 1 year ago)
- Topics: wordpress, wordpress-cookie, wordpress-multisite, wordpress-plugin, wp, wp-admin, wp-multisite, wp-plugin
- Language: PHP
- Homepage:
- Size: 192 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# WordPress - Multisite Cookie Manager
[](./LICENSE)
## Overview
**_Manage cookies across a WordPress multisite network with the Multisite Cookie Manager plugin._**
This plugin allows network administrators and site admins to manage cookie expiration settings, and identify all cookies being set across the multisite network. It provides a settings page within each site's admin area where you can specify cookie expiration times based on user roles and manage geo-location data as well as user sessions. A unique cookie will be set for each site in the network based on the specified settings.
## Features
- Unique cookie generation per site, named based on the site name and blog ID.
- Customizable cookie expiration times based on user roles and login status.
- Geo-location data collection and user session tracking.
- Logging and error handling for debugging.
- Export and import of cookie settings via JSON.
- Site admin settings page for managing cookies.
- Usage reporting for monitoring cookies across the network.
## Installation
1. Download the plugin files to your computer.
2. Using an FTP program, or your hosting control panel, upload the unzipped plugin folder to the `/wp-content/plugins/` directory of your WordPress multisite installation.
3. Navigate to the Plugins page within your WordPress dashboard on the site where you want to manage cookies.
4. Locate **MN - WordPress Multisite Cookie Manager** in the list of available plugins and click "Activate".
5. Once activated, a new menu item "Cookie Settings" will appear in the admin dashboard of the site where you activated the plugin. Now you can manage cookie settings and view cookie usage reports from this menu.
## Usage
### Setting Cookie Expiration Times
1. After activating the plugin, navigate to the **Network Admin -> Settings** page.
2. Click on **Cookie Settings** in the menu.
3. On the **Cookie Settings** page, you'll find a form to manage cookie expirations:
4. Under **Cookie Expirations**, text area, input a JSON object with user roles and corresponding expiration times in seconds.
**Example:**
```json
{
"administrator": 86400,
"editor": 43200,
"author": 21600,
"contributor": 10800,
"subscriber": 5400,
"logged_out": 1800
}
```
5. Click **Save Settings** to save your changes.
6. The plugin will automatically set cookies with the specified expiration times for each site in your network.
### Exporting and Importing Settings
- **Export:** Click on "Export Settings" to download a JSON file containing the current cookie expiration settings.
- **Import:** Choose a JSON file by clicking "Choose File" under "Import Settings", then click "Import Settings" to apply the settings from the file.
### Viewing Cookie Usage Reports
- Navigate to the "Cookie Usage Reports" submenu under "Cookie Settings".
- View the table showing the usage of each cookie across the network.
### Clear the Cookies from DB
- When you visit "Cookie Usage Reports" admin page, you'll see a "Clear Cookies" button above the table. Pressing this button will clear the cookies with the unique cookie name from the database.
## Functions
### mn_get_unique_cookie_name()
Generates a unique cookie name based on the site name and blog ID.
### mn_log_error($message, $error_type = E_USER_NOTICE)
Handles error logging based on the `WP_DEBUG` settings in the WordPress configuration.
### mn_register_cookie_settings_page()
Registers a new menu page in the network admin for managing cookie settings.
### mn_cookie_settings_page()
Displays the cookie settings page, handles form submission for updating settings, and exporting/importing settings.
### mn_get_cookie_expiration($default_expiration)
Determines the expiration time for a cookie based on the user's role.
### mn_set_custom_cookie()
Sets a custom cookie on page load with a name generated by `mn_get_unique_cookie_name()` and expiration determined by `mn_get_cookie_expiration()`.
### mn_create_cookie_usage_table()
Creates a new database table for logging cookie usage on plugin activation.
### mn_log_cookie_usage()
Logs cookie usage on page load.
### mn_write_cookie_usage_log_entries()
Writes log entries from transient to database hourly.
### mn_register_cookie_reporting_page()
Registers a submenu page for cookie usage reports.
### mn_cookie_reporting_page()
Displays cookie usage reports.
### mn_export_cookie_settings()
Exports cookie settings to a JSON file.
### mn_import_cookie_settings($json_settings)
Imports cookie settings from a JSON file.
### mn_get_geolocation_data()
Fetches the user's geo-location data using an external [API](https://app.ipgeolocation.io/).
### mn_clear_cookies()
Function to clear the cookies with the unique cookie name from the database.
### mn_handle_clear_cookies_request()
This function is triggered by the `admin_init` action. This function checks the nonce for security, calls `mn_clear_cookies()` to clear the cookies from the database, and displays a success message.
### mn_custom_plugin_styles()
The function is designed to inject custom CSS styling into the admin pages of your WordPress site without the need for an external stylesheet file. This is achieved by hooking the function into the `admin_head` action, which triggers the function to output a `` block within the `<head>` section of WordPress admin pages.
## Error Handling and Debugging
Enable `WP_DEBUG` in your WordPress configuration to catch errors during development. In your `wp-config.php` file:
```php
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
```
## Uninstallation
Upon uninstallation, the plugin will remove the `multisite_cookie_usage` table from the database.
## FAQs
### How do I set custom cookie expiration times?
Navigate to the **Settings->Cookie Settings** page on any site within your network. In the provided text area, input a JSON object specifying the expiration times for different user roles and for logged-out users. Click "Save Settings" to save your changes.
### How are cookies named?
Cookies are named following this pattern: `__site_name_blog_id`, where `site_name` is the name of the site with all spaces removed and converted to lowercase, and `blog_id` is the ID of the site within the network.
**Example:** For a site named "My Site" with a blog ID of 2, the cookie would be named `__mysite_2`.
### How can I view all cookies being set across the network?
Navigate to the **Settings->Cookie Settings->Cookie Usage Reports** on any site within your network to view a list of all unique cookies being set across the network along with the number of sites on which each cookie has been found.
### How can I export or import cookie settings?
On the **Settings->Cookie Settings** page, you'll find options to export your current cookie settings to a JSON file, or import settings from a JSON file. This allows for easy transfer of settings between different sites or for backup purposes.
### How can I style the settings and reports pages?
The plugin includes a function `mn_custom_plugin_styles()` that injects custom CSS into the admin pages. You can modify the CSS rules within this function to change the appearance of the plugin's settings and reports pages according to your preferences.
## Changelog
For a detailed list of changes and updates made to this project, please refer to our [Changelog](./CHANGELOG.md).
---
## License
This project is released under the [GPL-2.0+ License](http://www.gnu.org/licenses/gpl-2.0.txt).
