edit $_POST values before posting into database

This topic contains 6 reply and 2 voices, and was last updated by Sparhamster 11 years ago
Viewing 6 Posts - 1 through 6 (of 6 total)
Author Posts
April 11, 2013 at 12:06 am 2013
Sparhamster 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
April 11, 2013 at 12:51 am 2015
Tareq Hasan Tareq Hasan

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

April 11, 2013 at 11:17 pm 2055
Sparhamster Sparhamster

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?

April 12, 2013 at 1:18 am 2058
Tareq Hasan Tareq Hasan

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.

April 12, 2013 at 4:55 am 2072
Sparhamster Sparhamster

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?

April 12, 2013 at 10:36 am 2079
Tareq Hasan Tareq Hasan

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.

April 12, 2013 at 3:22 pm 2105
Sparhamster Sparhamster

thx for the great support and the best form plugin for wp 😉

Viewing 6 Posts - 1 through 6 (of 6 total)