If you want to let users reopen the cookie preferences panel by clicking a custom link, you might try placing the following code inside a Wix HTML Embed block:
<a href="#" onClick="__ucCmp.showSecondLayer();">Privacy Settings</a>
This will not work
This code will not work inside a Wix HTML Embed. Clicking the link will have no visible effect.
Why it doesn't work
Wix loads all HTML Embed blocks inside an isolated iframe. Code running inside the iframe cannot access objects or functions that belong to the parent page.
The __ucCmp object is injected by Usercentrics into the parent page. When the iframe tries to call __ucCmp.showSecondLayer(), the function does not exist in that scope and nothing happens.
Solution
Use the browser's window.postMessage API to send a message from the iframe to the parent page. The parent page listens for that message and calls __ucCmp.showSecondLayer() in its own context, where it is available.
This requires two pieces of code — one inside the HTML Embed, and one in your Wix Custom Code settings.
Step 1 — Add this code to your HTML Embed block
In the Wix Editor, open your HTML Embed block, click Enter Code, and paste the following:
<a href="#" onclick="openPrivacySettings(); return false;">Privacy Settings</a>
<script>
function openPrivacySettings() {
window.parent.postMessage('openPrivacySettings', '*');
}
</script>Step 2 — Add the listener to your Wix Custom Code
Go to Settings → Custom Code in your Wix dashboard. Click + Add Custom Code and paste the following:
<script>
window.addEventListener('message', function(event) {
if (event.data === 'openPrivacySettings') {
if (typeof __ucCmp !== 'undefined') {
__ucCmp.showSecondLayer();
return;
}
var attempts = 0;
var interval = setInterval(function() {
attempts++;
if (typeof __ucCmp !== 'undefined') {
__ucCmp.showSecondLayer();
clearInterval(interval);
}
if (attempts >= 10) {
clearInterval(interval);
}
}, 500);
}
});
</script>Use the following settings when saving the code:
| Setting | Value |
|---|---|
| Place Code in | Body — end |
| Add Code to | All Pages (or the specific page where the embed appears) |
How it works
- The user clicks the Privacy Settings link inside the iframe.
- The iframe sends the message
'openPrivacySettings'to its parent window usingpostMessage. - The parent page's event listener receives the message.
__ucCmp.showSecondLayer()is called in the parent context, where it exists.- The Usercentrics privacy preferences panel opens.
Note
The retry logic in the parent page code (every 500 ms, up to 5 seconds) ensures the call succeeds even if Usercentrics has not fully initialized by the time the user clicks the link.
Comments
0 comments
Please sign in to leave a comment.