• Hi,

    I have several Gutenberg blocks on my pages. These blocks contain instructions, where users can move forward to the next block every week. I would like to add a textarea-type input field at the end of each block where users can write personal notes for themselves about studying. These notes should be visible only to the user. Ideally, the notes should be stored somehow (e.g., in cookies) so that the user can review them even weeks later. How can I achieve this without heavy PHP coding?

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • You can achieve this functionality using JavaScript and localStorage (a client-side storage method that persists even after the browser is closed). Below is a step-by-step guide on how to implement this for Gutenberg blocks: Steps:

    1. Identify Your Blocks: Ensure your Gutenberg blocks have unique identifiers (like data-id or classes) to differentiate them. For example, use a class like study-block on each block.
    2. Add a Textarea: Add a textarea element at the end of each block using a simple JavaScript script. The textarea will be linked to each block via its unique identifier.
    3. Store Notes in localStorage: Use JavaScript to save the content of the textarea to localStorage whenever the user types something. You can associate the stored notes with the block’s unique identifier.
    4. Retrieve Notes on Page Load: When the page loads, retrieve the notes from localStorage and populate the corresponding textarea.

    Implementation:

    Add the following script to your WordPress theme or a custom JavaScript file. HTML Structure in Your Blocks

    Ensure each block has a unique identifier, like this:

    <div class="study-block" data-block-id="block-1">
      <p>Instructions for week 1</p>
      <textarea class="user-notes" placeholder="Write your notes here..."></textarea>
    </div>
    <div class="study-block" data-block-id="block-2">
      <p>Instructions for week 2</p>
      <textarea class="user-notes" placeholder="Write your notes here..."></textarea>
    </div>
    

    JavaScript Code

    document.addEventListener("DOMContentLoaded", () => {
      // Select all study blocks
      const blocks = document.querySelectorAll(".study-block");
    
      blocks.forEach((block) => {
        const blockId = block.getAttribute("data-block-id");
        const textarea = block.querySelector(".user-notes");
    
        // Load saved notes from localStorage
        const savedNotes = localStorage.getItem(notes_${blockId});
        if (savedNotes) {
          textarea.value = savedNotes;
        }
    
        // Save notes to localStorage on input
        textarea.addEventListener("input", () => {
          localStorage.setItem(notes_${blockId}, textarea.value);
        });
      });
    });
    

    Key Points:

    1. Data-Block-ID:
      Each block must have a unique data-block-id. This ensures that notes are stored and retrieved for the correct block.
    2. LocalStorage:
      This method stores data in the user’s browser. It’s persistent across sessions but is user-specific and browser-specific.
    3. No Heavy PHP:
      This approach avoids server-side coding and relies entirely on JavaScript, making it lightweight.

    Limitations:

    • Browser Dependency: Notes are stored per browser and device. If the user switches devices or clears their browser storage, the notes will be lost.
    • Security: Ensure you trust the user environment since localStorage is not encrypted.

    This solution is simple, efficient, and avoids heavy PHP coding while meeting your requirements.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.