How to restrict the Webflow comment section to admins only using Memberstack gating? Answered

Post author
Nicolai Amirzadeh

How can I make it so that the commenting section on the main page is only for admins? I see that it has an attribute, but it didn't work. I also tried gating it towards just a specific free plan I would give to my mods, but it's still showing up

Comments

6 comments

  • Comment author
    A J

    Hey Nicolai Amirzadeh, I was testing this feature and it seems adding data-ms-content="CONTENT_GROUP_ID", seems to hide the commenting option. The content_group_id being the gated content ID you have applied for admins only data. I did this to ensure comments can only be posted for a user who is in Plan B (shown in screenshots - view of non-plan b user and plan b user accordingly).

    If you want to hide all comments you can enable this attribute for the whole commenting section.

    In case you are still facing issue with this, can you share preview link of your website, so that we an troubleshoot this?

    0
  • Comment author
    Nicolai Amirzadeh

    Hey A J! This worked perfectly, thank you!!

    Do you have any idea how I can make this to work? I want one of the custom fields to populate here?

    I managed to get it work for a second, but then I refreshed and it went back

    0
  • Comment author
    A J

    Hey Nicolai Amirzadeh, in the custom attribute for role, instead of data-ms-member can you place data-ms-post-creator and keep the value as "role" just as it is. This should populate the role accurately.

    And also just as a side note, make sure to set the custom field Role's visibility as "Public" under "Advanced/Commenting" section if not done already.

    0
  • Comment author
    Nicolai Amirzadeh

    You're a legend mate, worked immediately 🫠

    I was wondering if you knew how I could hide the badge if the user doesn't have any roles?

    I just found the tutorial I tried doing it, but it didn't work.I added the code + attribute, but nothing. I also tried removing the text, but then just a blue box stays there.

    Here's the code in body:


    <!--  MEMBERSCRIPT #10 v0.2  HIDE ELEMENTS IF CUSTOM FIELD IS BLANK -->
    <script>
    document.addEventListener('DOMContentLoaded', function() {
      // Get the `_ms-mem` object from the local storage
      const msMem = JSON.parse(localStorage.getItem('_ms-mem'));  // Get all the elements that have the `ms-code-customfield` attribute
      const elements = document.querySelectorAll('[ms-code-customfield]');  // Iterate over each element
      elements.forEach(element => {
        // Get the value of the `ms-code-customfield` attribute
        const customField = element.getAttribute('ms-code-customfield');    // If customField starts with '!', we invert the logic
        if (customField.startsWith('!')) {
          const actualCustomField = customField.slice(1); // remove the '!' from the start      // If the custom field is empty, remove the element from the DOM
          if (msMem.customFields && msMem.customFields[actualCustomField]) {
            element.parentNode.removeChild(element);
          }
        } else {
          // Check if the user has the corresponding custom field in Memberstack
          if (!msMem.customFields || !msMem.customFields[customField]) {
            // If the custom field is empty, remove the element from the DOM
            element.parentNode.removeChild(element);
          }
        }
      });
    });
    </script

     

    0
  • Comment author
    A J

    But an even simpler way would be to have no text in the "Role" field on front-end, so only if memberstack has a custom field it will be visible and if not the text element will be blank.

    In my case I don't have any styling applied so removing the text also works.

    Hey Nicolai Amirzadeh, would you be open to changing the color of the text and applying some styling which does not show up when the role is kept empty?

    The issue here is the script works for the logged in member and not the commenter. And since the comments take time to load, it is also getting difficult to target them via custom code and delayed targeting is not reliable as well. I have tried various methods but its not that accurate. So until we get a solution via which this is handled automatically, you can opt to customize the design and you can show the difference via text color change / underline etc and keep the Role text block empty

    0
  • Comment author
    Nicolai Amirzadeh

    That's the smartest solution A J! Old me would have said no since it doesn't look as alethic good - but I know our members won't notice the difference 😅

    0

Please sign in to leave a comment.