0

I have built a plugin that allows a user to submit forms to a third party API via an API call. I'm wondering if it's possible to add in a hook right before (or after) the data gets sent to the third party, and return it for use in a function.

Ideally, a user could define there own function and hook into the hook I have set, grab the data and use it how they please, or manipulate it before it gets sent off to the API.

if(!empty($p['form_data']))
    {
    parse_str($p['form_data'], $fd);
    if(!empty($fd['list-id']))
        {
        // Create and store the variables needed to add a new subscriber        
        $email  = false;
        $lid  = empty($fd['list-id'];
        $api  = 'api_key';
        $mv = array();
        $optin  = 'true';

// Add custom hook, to capture user submitted data
do_action( 'get_form_data', $p['form_data'] );
        }
    }

And then ideally the user could write a function to use the data (basic example).

function catch_user_data( $data ) {
    $user_email = $data['email'];
    update_user_meta( '1' , 'user_email', $user_email );
}
add_action( 'get_form_data' , 'catch_user_data' );

I'm sure I'm doing something wrong here as the data doesn't get updated on form submission. Is the hook written wrong?

2

If you want to allow users to "manipulate it before it gets sent off to the API" I would suggest using a filter instead of an action.

if ( ! empty( $p['form_data'] ) ) {
    parse_str( $p['form_data'], $fd );
    if ( ! empty( $fd['list-id'] ) ) {
        // Create and store the variables needed to add a new subscriber        
        $email  = false;
        $lid  = empty( $fd['list-id'] );
        $api  = 'api_key';
        $mv = array();
        $optin  = 'true';

        // Add custom hook, to capture user submitted data
        $p['form_data'] = apply_filters( 'get_form_data', $p['form_data'] );
    }
}

This will be the code that enables users to manipulate this data.

function catch_user_data( $data ) {
    $user_email = $data['email'];
    update_user_meta( '1' , 'user_email', $user_email );
    return str_replace( 'find-something', 'replace-it', $data );
}
add_filter( 'get_form_data' , 'catch_user_data' );

More reading:
http://codex.wordpress.org/Function_Reference/apply_filters
http://codex.wordpress.org/Glossary#Action
http://codex.wordpress.org/Glossary#Filter

| improve this answer | |
  • Thank you for pointing me in the right direction. I am getting confused with all the actions and filters! I should go back and re-read the sections in the codex. – EHerman Apr 30 '14 at 18:21
  • 1
    @EHerman : this might help: wordpress.stackexchange.com/a/103644/21376 – s_ha_dum Apr 30 '14 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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