Repository navigation
Control programmatically
The plugin exposes both client-side and server-side APIs to control the cookie consent modal programmatically.
The plugin exposes the pressidiumCookieConsent object on the window object.
Show the consent modal.
pressidiumCookieConsent.show(optionalDelay, createModal)-
optionalDelay— (optional)number -
createModal— (optional)boolean
Pass true to the createModal parameter to create a modal if it doesn’t exist.
Examples:
// Immediately show the consent modal
pressidiumCookieConsent.show();
// Wait 250 ms, then show the consent modal
pressidiumCookieConsent.show(250);
// Show the consent modal. If it doesn't already exist, create one
pressidiumCookieConsent.show(0, true);Hide the consent modal.
pressidiumCookieConsent.hide()Example:
// Hide the consent modal
pressidiumCookieConsent.hide();Show the settings modal.
pressidiumCookieConsent.showSettings(optionalDelay)-
optionalDelay— (optional)number
Examples:
// Immediately show the settings modal
pressidiumCookieConsent.showSettings();
// Wait 250 ms, then show the settings modal
pressidiumCookieConsent.showSettings(250);Keep in mind that by appending the data-cc="c-settings" attribute to a button (or link) within the consent modal, users can access the settings modal.
<!-- button with the `data-cc="c-settings"` attribute in the consent modal -->
<button type="button" aria-label="View cookie settings" data-cc="c-settings">Cookie settings</button>Caution
This method will be included in version 2.0.0 of the Pressidium Cookie Consent plugin. It has not been released yet. The information provided here is subject to change.
Show the preferences modal. Use this instead of showSettings().
pressidiumCookieConsent.showPreferences()Examples:
// Immediately show the preferences modal
pressidiumCookieConsent.showPreferences();Hide the settings modal.
pressidiumCookieConsent.hideSettings()Example:
// Hide the settings modal
pressidiumCookieConsent.hideSettings();Caution
This method will be included in version 2.0.0 of the Pressidium Cookie Consent plugin. It has not been released yet. The information provided here is subject to change.
Hide the preferences modal.
pressidiumCookieConsent.hidePreferences()Example:
// Hide the preferences modal
pressidiumCookieConsent.hidePreferences();Accept one (or more) cookie categories.
pressidiumCookieConsent.accept(acceptedCategories, optionalRejectedCategories)-
acceptedCategories—stringorstring[] -
optionalRejectedCategories— (optional)string[]
Examples:
// Accept all categories
pressidiumCookieConsent.accept('all');
// Accept none (reject all)
pressidiumCookieConsent.accept([]);
// Accept only the `analytics` category
pressidiumCookieConsent.accept('analytics');
// Accept the `analytics` and `targeting` categories
pressidiumCookieConsent.accept(['analytics', 'targeting']);
// Accept all currently selected categories inside modal
pressidiumCookieConsent.accept();
// Accept all except of the `analytics` category
pressidiumCookieConsent.accept('all', ['analytics']);
// Accept all except of the `analytics` and `targeting` categories
pressidiumCookieConsent.accept('all', ['analytics', 'targeting']);Check if the specified category is allowed (i.e. the user has given consent for that category).
pressidiumCookieConsent.allowedCategory(categoryName)-
categoryName—string
Example:
if (pressidiumCookieConsent.allowedCategory('analytics')) {
// User has accepted the cookie consent with the 'analytics' category enabled
}Caution
This method will be included in version 2.0.0 of the Pressidium Cookie Consent plugin. It has not been released yet. The information provided here is subject to change.
Check if the specified category was accepted.
In opt-out mode, before the user makes a choice, it return true for the categories that are enabled by default.
pressidiumCookieConsent.acceptedCategory(categoryName)-
categoryName—string
Example:
if (pressidiumCookieConsent.acceptedCategory('analytics')) {
// User has accepted the cookie consent with the 'analytics' category enabled
}Check if the cookie with the specified name exists (and has a non-empty value).
pressidiumCookieConsent.validCookie(cookieName)-
cookieName—string
Example:
if (pressidiumCookieConsent.validCookie('_gid')) {
// the `_gid` cookie is set
}Erase the cookies with the specified name(s).
pressidiumCookieConsent.eraseCookies(cookieNames, optionalPath, optionalDomains)-
cookieNames—string[] -
path— (optional)string -
domains— (optional)string[]
Examples:
// Erase `cc_cookie` if it exists
pressidiumCookieConsent.eraseCookies(['cc_cookie']);
// Erase these 2 cookies
pressidiumCookieConsent.eraseCookies(['cookie1', 'cookie2']);
// Pass the optional `path` parameter
pressidiumCookieConsent.eraseCookies(['cc_cookie'], '/demo');
// Pass the optional `path` and `domains` parameters
pressidiumCookieConsent.eraseCookies(['cc_cookie'], '/demo', [location.hostname]);Load a previously blocked script.
pressidiumCookieConsent.loadScript(path, callback, optionalCustomAttributes)-
path—string -
callback—function -
optionalCustomAttributes—object[]
Examples:
pressidiumCookieConsent.loadScript('https://www.google-analytics.com/analytics.js', () => {
// Script loaded, do something
});
pressidiumCookieConsent.loadScript('https://www.google-analytics.com/analytics.js', () => {
// Script loaded, do something
}, [
{ name: 'id', value: 'ga_id' },
{ name: 'another-attribute', value: 'value' },
]);A custom event that gets triggered:
- At the first moment that consent is given
- After every page load, if consent ("accept" or "reject" action) has already been given
window.addEventListener('pressidium-cookie-consent-accepted', (event) => {
// `cookie` contains the current value of the consent cookie
const { cookie } = event.detail;
});A custom event that gets triggered:
- When the user changes their preferences (i.e. accepts/rejects a cookie category), only if consent has already been given
window.addEventListener('pressidium-cookie-consent-accepted', (event) => {
// `cookie` contains the current value of the consent cookie
// `changedCategories` is an array of the categories whose state (accepted/rejected) just changed
const { cookie, changedCategories } = event.detail;
});The plugin exposes global functions that you can use in your PHP code.
Return an array containing all cookie categories and their cookies.
pressidium_cookie_consent_get_cookies()Returns
(array) an array of cookie categories, where each category is an array of all of its listed cookies.
Example
array(
'necessary' => array(
array(
'name' => 'pressidium_cookie_consent',
'domain' => 'example.com',
'expiration' => '6 months',
'path' => '/',
'description' => 'Pressidium Cookie Consent to store cookie consent preferences.',
'is_regex' => false,
),
// ...
),
// ...
)The plugin exposes actions and filters that you can use in your PHP code.
Filters the path to the log file.
apply_filters( 'pressidium_cookie_consent_logs_path', string $log_path )Parameters
$log_path
(string) Path to the log file.
Filters the host the Google tag gateway proxy sends requests to.
apply_filters( 'pressidium_cookie_consent_tag_gateway_host', string $host )Parameters
$host
(string) The *.fps.goog host the Google tag gateway proxy sends requests to.
Caution
This filter will be included in version 2.0.0 of the Pressidium Cookie Consent plugin. It has not been released yet. The information provided here is subject to change.
Filters the IP address used for rate limiting REST API endpoints.
apply_filters( 'pressidium_', string $ip_address )Parameters
$ip_address
(string) Client IP address.
Caution
This filter will be included in version 2.0.0 of the Pressidium Cookie Consent plugin. It has not been released yet. The information provided here is subject to change.
Filters the IP address used for geolocation.
apply_filters( 'pressidium_cookie_consent_geo_ip_address', string $ip_address )Parameters
$ip_address
(string) Client IP address.
Caution
This filter will be included in version 2.0.0 of the Pressidium Cookie Consent plugin. It has not been released yet. The information provided here is subject to change.
Filters the keys used to detect the country code from the $_SERVER superglobal for geolocation.
Request headers appear as HTTP_ followed by the header name in uppercase, with hyphens replaced by underscores (e.g. X-Country-Code → HTTP_X_COUNTRY_CODE).
apply_filters( 'pressidium_cookie_consent_geo_server_keys', string $server_keys )Parameters
$server_keys
(string) Indexed array of server keys. The first key found in the $_SERVER superglobal will be used to determine the country code.
Caution
This filter will be included in version 2.0.0 of the Pressidium Cookie Consent plugin. It has not been released yet. The information provided here is subject to change.
Filters the country code for geolocation.
apply_filters( 'pressidium_cookie_consent_geo_country_code', string $country_code, string $ip_address )Parameters
$country_code
(string) Country code or null if we couldn't detect a country.
$ip_address
(string) Client IP address.