Where to store persistent widget variable?
-
Simple example:
lets say theres a widget with a counter,var count=0 // on some action count++
If the code runs from scratch each time page loads the click count will start as 1.
If the count should persist, and continually increase when any user views page, is there any good place to store some global variable data?
One solution is to have a hidden category, and store count value in a post there, and read and write it back with API call!
But there must be an easier way? -
For a counter like that you would need to create a custom widget that stores the counter in the database and increase it by 1 on the server side as well whenever the user renders it or performs whatever the action is. You can make custom socket.io events or create a new API route.
Incrementing the value in the database is a simple db call like
await db.incrObjectField('myCustomWidget', 'counter');