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.

Below is a DB query I am using to retrieve values and place them in a WP_List_Table. Values are currently being added by add_option, then those values are converted into a custom database table called wp_testimonials. I have no clue why I did it this way, but it works!

So now I need to create a conditional that says "if user is currently updating the option, then update the table after the option is updated, else don't do anything."

However, I do not know how to state this condition. My display table is located in a different file than where the options are being updated.

Below is the database query, which is currently updating every time I access the page and needs to be changed.

      $options = get_option('testimonials_settings');  
          if ( isset ($options)) {  
          global $wpdb;
          $table_name = $wpdb->prefix . "testimonials";
          $sql = "CREATE TABLE $table_name (
          id mediumint(9) NOT NULL AUTO_INCREMENT,
          name tinytext NOT NULL,
          company_name text NOT NULL,
          company_url text NOT NULL,
          testimonials_quote text NOT NULL,
          UNIQUE KEY id (id)
      );";
          require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
          dbDelta($sql);

         $name = $options['testimonials_name'];
         $company_name = $options['testimonials_website_name'];
         $company_url = $options['testimonials_website_url'];
         $testimonials_quote = $options['testimonials_quote'];
         $rows_affected = $wpdb->insert( $table_name, array( 'name' => $name, 'company_name' => $company_name, 'company_url' => $company_url, 'testimonials_quote' => $testimonials_quote ) );

      }
share|improve this question

1 Answer 1

up vote 1 down vote accepted

The function update_option includes action hooks that you can use.

Use update_option_{$option_key} as shown here:

function my_func($option, $value) {
  //this is called only when that particular option is updated
  //$option has the option key & $value has the value array
}
add_action('update_option_testimonials_settings', 'my_func', 10, 2);
share|improve this answer
    
Hi Mridul, I'm just a little confused here - do I need to substitute any of the values in your answer, I'm thinking "no" but I could be wrong. –  Rob Myrick Dec 18 '12 at 5:54
    
No the code example is complete by itself. I replaced {$option_key} with the actual name of the key there –  Mridul Aggarwal Dec 18 '12 at 6:18
    
OK, last time I tried that and it didn't work. I tested to see if I'm retrieving value correctly, and it seems that I am. Should I simply wrap your function around my existing code, as it was posted in the question? –  Rob Myrick Dec 18 '12 at 6:26
    
Nevermind, Mridul.....I changed add_option_{$option_key} to update_option_{$option_key} and it worked! Thanks so much... –  Rob Myrick Dec 18 '12 at 6:40
    
It should have worked with add_option too. but anyways i'm glad you got it working. –  Mridul Aggarwal Dec 18 '12 at 7:17

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.