WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to augment a value to the wp_user_meta table, utilizing Gravity Forms. And I'm not having any luck. I had a version of this working on gform_pre_submission (though without the class-sniffing) but I want to do this after submission since there will be PayPal involved and don't want to post until the payment is verified.

I was trying to use cssClass as a filter so the other developers are free to create as many gravity forms as they want, provided they use the classes product_name and product_price

Unfortunately, my database is just filling up with fails right now.

// hook onto gravity forms and update payment history field
add_action('gform_after_submission', 'payment_history_handler', 10, 2);
function payment_history_handler($entry, $form){

$current_user = wp_get_current_user();
$payment_history = get_user_meta($current_user->ID, 'payment_history', true);

foreach($form['fields'] as &$field){
    if(strpos($field['cssClass'], 'payment_name') === true){
        $payment_name = $entry[$field['id']];;
    } else {
        $payment_name = 'fail';
    if(strpos($field['cssClass'], 'payment_price') === true){
        $payment_price = $entry[$field['id']];
    } else {
        $payment_price = 1;

if(!empty($payment_name) && !empty($payment_price)){
    $payment_history[] = array('date' => date('U'), 'name' => $payment_name, 'price' => $payment_price);
    update_user_meta($current_user->ID, 'payment_history', $payment_history);


The classes are in place, as seen by this sample of my output:

<div class='gform_body'>
  <ul id='gform_fields_4' class='gform_fields top_label description_below'>
    <li id='field_4_1' class='gfield    payment_name           gfield_contains_required' >
      <label class='gfield_label' for='input_4_1'>What Thing are you choosing?<span class='gfield_required'>*</span></label>
      <div class='ginput_container'>
        <select name='input_1' id='input_4_1'  class='medium gfield_select' tabindex='1' >
          <option value='The first one' >The first one</option>
          <option value='The second one' >The second one</option>
          <option value='The third one' >The third one</option>
    <li id='field_4_2' class='gfield    payment_price           gfield_contains_required' >
      <label class='gfield_label' for='input_4_2'>Price<span class='gfield_required'>*</span></label>
      <div class='ginput_container'>
        <input name='input_2' id='input_4_2' type='text'  value='' class='medium' tabindex='2'   />

Any help is greatly appreciated.

share|improve this question

closed as off-topic by mrwweb, Pieter Goosen, Johannes Pille, birgire, ialocin Sep 1 '14 at 6:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – mrwweb, Pieter Goosen, Johannes Pille, birgire, ialocin
If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 1 down vote accepted

It looks like your code is testing to see where every field on the form has class 'payment_name'. If you instead only want to find one field with class 'payment_name', then you need to break out of the for loop once you've found that field.

Also, strpos only returns false or an integer index into the haystack where it found the needle, not true; test instead for strpos($field['cssClass'], 'payment_name') !== false

share|improve this answer
Nice. This is working now. I'm actually looking for payment_name in one field and payment_price in another. I suppose once both of these are defined I can abort. Thanks. – GhostToast Jan 10 '13 at 22:59
In case anyone finds this, a good way to break was to move the if(!empty()) part of the code up into the loop and add a break statement beneath it, so as soon as both variables are defined, it does the work and stops looking. Thanks again @webaware – GhostToast Jan 10 '13 at 23:03

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