Skip to content

Control programmatically

Konstantinos Pappas edited this page May 9, 2025 · 7 revisions

The plugin exposes both client-side and server-side APIs to control the cookie consent modal programmatically.

Client-side API

The plugin exposes the pressidiumCookieConsent object on the window object.

Show/hide modals

show()

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()

Hide the consent modal.

pressidiumCookieConsent.hide()

Example:

// Hide the consent modal
pressidiumCookieConsent.hide();

showSettings()

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>

hideSettings()

Hide the settings modal.

pressidiumCookieConsent.hideSettings()

Example:

// Hide the settings modal
pressidiumCookieConsent.hideSettings();

Manage cookies/scripts

accept()

Accept one (or more) cookie categories.

pressidiumCookieConsent.accept(acceptedCategories, optionalRejectedCategories)
  • acceptedCategoriesstring or string[]
  • 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']);

allowedCategory()

Check if the specified category is allowed (i.e. the user has given consent for that category).

pressidiumCookieConsent.allowedCategory(categoryName)
  • categoryNamestring

Example:

if (pressidiumCookieConsent.allowedCategory('analytics')) {
  // User has accepted the cookie consent with the 'analytics' category enabled
}

validCookie()

Check if the cookie with the specified name exists (and has a non-empty value).

pressidiumCookieConsent.validCookie(cookieName)
  • cookieNamestring

Example:

if (pressidiumCookieConsent.validCookie('_gid')) {
  // the `_gid` cookie is set
}

eraseCookies()

Erase the cookies with the specified name(s).

pressidiumCookieConsent.eraseCookies(cookieNames, optionalPath, optionalDomains)
  • cookieNamesstring[]
  • 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]);

loadScript()

Load a previously blocked script.

pressidiumCookieConsent.loadScript(path, callback, optionalCustomAttributes)
  • pathstring
  • callbackfunction
  • optionalCustomAttributesobject[]

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' },
]);

Events

pressidium-cookie-consent-accepted

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;
});

pressidium-cookie-consent-changed

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;
});

Server-side API

The plugin exposes global functions that you can use in your PHP code.

pressidium_cookie_consent_get_cookies()

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,
        ),
        // ...
    ),
    // ...
)

Hooks

The plugin exposes actions and filters that you can use in your PHP code.

pressidium_cookie_consent_logs_path filter

Caution

This filter will be included in version 1.9.0 of the Pressidium Cookie Consent plugin. It has not been released yet. The information provided here is subject to change.

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.

Clone this wiki locally