Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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:

  1. The Transients API
  2. Or the Options API

However, if your data is very complicated you might want a dedicated table. Without more information, that is the best I've got.

share|improve this answer
    
Edited my post. I think the transients api won't work because I need to retain the data permanently, and there will possibly be too much data(?) to store as an option –  Jack Mar 24 at 8:45
    
The options table "value" column is longtext, which is enormous, but again, without the specifics of your project I can't tell you much else. –  s_ha_dum Mar 24 at 13:30
    
♦ so is it ok just to dump say an array of arrays into a single option value? i.e. array(array($date, $msg, $post_id), array()…etc)? –  Jack Mar 24 at 18:21
    
@Jack : Yes, the string will be serialized. If you use Core functions to insert and retrieve the data the serialization will be transparent. –  s_ha_dum Mar 24 at 18:24
    
The thing is, that seems to mean that each time I want to add a new log item, I have to retrieve the entire log array, append to it, and then put it back. Isn't that going to be horribly inefficient? –  Jack Mar 25 at 12:07

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:

$logs = array();
        $query_args = array(
            'meta_key' => 'myresponsetimestamp',
            'meta_compare' => 'EXISTS',
            'order' => 'DESC',
            'orderby' => 'meta_value_num'
        );
        $query = new WP_Query($query_args);
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                $post_id = get_the_ID();
                $logs[] = array(
                    'postid' =>$post_id,
                    'time' => get_post_meta( $post_id, 'myresponsetimestamp', true ),
                    'response' =>get_post_meta( $post_id, 'myresponse', true )
                );
            }
        }
        wp_reset_postdata();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.