Home Forums Plugin Support WP User Frontend Pro edit $_POST values before posting into database

This topic is: resolved

This topic contains 6 replies, has 2 voices, and was last updated by  Sparhamster 6 years, 1 month ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #2013

    Sparhamster
    Participant
    Post count: 22

    Another (and last ;)) question:

    i have to edit a special form field before adding it to the database. in which part of the content i have to insert the code?

    for example:

    • i have to edit the price field from “12,90” to “12.90”, if some user write the price with “,”.
    • explode a link and build a new affiliate link before inserting it to the DB.

    thx

    #2015

    Tareq Hasan
    Keymaster
    Post count: 2831

    The default category topic may be enough for the code example. You might checkout the documentation.

    #2055

    Sparhamster
    Participant
    Post count: 22

    can u tell me, why it works with $_POST and not with the parameter $postarr?

    function wpuf_editpostdata( $postarr ) {

    $_POST[‘Preis'] = str_replace(“,”, “.”, $_POST[‘Preis']);
    $_POST[‘Stattpreis'] = str_replace(“,”, “.”, $_POST[‘Stattpreis']);
    array_push($_POST[‘Kategorie'], “1”);

    return $postarr;
    }

    add_filter( ‘wpuf_add_post_args', ‘wpuf_editpostdata' );

    it is possible to print_r in functions? every time i try to print_r an array like postarr, the site ist loading and loading ๐Ÿ˜‰

    and i try in that line: array_push($_POST[‘Kategorie'], “1”); to add the category id 1 to the category array..it does't work. which function i have to choose?

    #2058

    Tareq Hasan
    Keymaster
    Post count: 2831

    wpuf_add_post_args filter is for modifying the wp_insert_post() variables, those are standard post_status, post_title, post_content etc. So in your case, it's not the suitable place for adding filter. You need to add action on wpuf_add_post_after_insert and update the meta fields.

    If you are printing something, that means it's modifying the ajax response. And that breaks the JSON response, so it keeps loading.

    #2072

    Sparhamster
    Participant
    Post count: 22

    like that?

    function wpuf_editpostdata( $form_vars ) {

    $form_vars[‘Preis'] = str_replace(“,”, “.”, $form_vars[‘Preis']);
    $form_vars[‘Stattpreis'] = str_replace(“,”, “.”, $form_vars[‘Stattpreis']);

    return $form_vars;
    }

    add_filter( ‘wpuf_add_post_after_insert', ‘wpuf_editpostdata' );

    but it didn't work … $form_vars[‘Preis'] & $form_vars[‘Stattpreis'] are costum fields of the form.

    can u show me how the $form_vars oder $form_settings array is build?

    #2079

    Tareq Hasan
    Keymaster
    Post count: 2831

    Here is the correct approach:
    [php]
    function wpufe_edit_postdata( $post_id ) {
    if ( isset( $_POST[‘Preis'] ) ) {
    $replaced = str_replace( ‘,', ‘.', $_POST[‘Preis']);
    update_post_meta( $post_id, ‘Preis', $replaced );
    }
    }

    add_action( ‘wpuf_add_post_after_insert', ‘wpufe_edit_postdata' );
    [/php]

    The difference between action and filter is, action doesn't need/care about return value where filter is for modifying the value and return it. wpuf_add_post_after_insert is an action and your custom field is already saved before calling this action. As you have the access to $_POST value, so you replace your data and update the meta again.

    #2105

    Sparhamster
    Participant
    Post count: 22

    thx for the great support and the best form plugin for wp ๐Ÿ˜‰

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘edit $_POST values before posting into database’ is closed to new replies.