I have a plugin that sends data to a server as part of a transition_post_status
action. The server sends a response each time. I want to keep a log of these responses (permanently) that I can display in a table to the user somewhere, like so:
Date | Response
------------------
1/1 | OK
1/2 | Status code 2
What’s the best way to do this? Should I create a new database table, or is there a more convenient way?
Some background: I’ve tried using the WP_Logging() class, but it doesn’t seem to work correctly when called through an action.
It is not clear exactly what you are doing. For example, I am not sure what the data is that you are saving or where/when it needs to be displayed, but it sounds like you need:
However, if your data is very complicated you might want a dedicated table. Without more information, that is the best I’ve got.
How I’ve done it in the end is as follows:
Because it’s triggered on
post_status_transition
, each log can be effectively linked to a particular post. Therefore, I can use add/update_post_meta to store the response and timestamp as meta fields for that post.To retrieve all logs, I can simply do: