Home › Forums › Front-end Issues › Something broke – Clear all inputs ?
Hello i am using Front-end Form in wordpress 3.8.1 and there is a problem new users see the filled up form of old users. There was a short time when this was working ok until this day. I am using this code to send the info:
add-information.php
<article id="post-<?php the_ID(); ?>" <?php post_class('entry clearfix'); ?> role="article">
<div class="post_content clearfix">
<?php
$options = array(
'post_id' => 'new', // post id to get field groups from and save data to
'field_groups' => array( 27 ), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => '',
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Wyślij', // value for submit field
'updated_message' => 'Wpis został dodany prawidłowo.', // default updated message. Can be false to show no message
);
//Sprawdzenie id zalogowanego użytkownika
if ( is_user_logged_in() ) {
$user_ID = get_current_user_id();
//Funckja licząca statusy wpisów.
global $wp_query;
$curauth = $user_ID;
$post_count_draft = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '" . $curauth . "' AND post_type = 'post' AND post_status = 'draft'");
$post_count_publish = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '" . $curauth . "' AND post_type = 'post' AND post_status = 'publish'");
//Jeśli więcej niż 1 wpis, zakaż dodawania więcej.
if (($post_count_draft > 0) OR ($post_count_publish > 0)) {
echo '<h2 class="notice">Wpis został dodany prawidłowo.</h2>';
echo '<h2 class="notice">Możesz tylko dodać jeden wpis, wróć na stronę główną.</h2>';
}
else {
acf_form( $options );
}
}
else {
echo '<h2 class="warning">Tylko dla zalogowanych użytkowników.</h2>';
echo wp_login_form();
echo wp_register('', '');
}
?>
</div> <!-- end .post_content -->
</article> <!-- end .entry -->
And here is functions.php
if ( function_exists( 'add_image_size' ) )
add_theme_support( 'post-thumbnails' );
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'galeria1', 9999, 630, false );
}
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'okladka', 630, 210, false );
}
function add_new_post( $post_id )
{
if( $post_id == 'new' ) {
// Create a new post
$post = array(
'post_title' => $_POST["fields"]['field_52e8ce62f6982'],
'post_status' => 'draft',
'post_category' => array(6),
'post_type' => 'post'
);
// insert the post
$post_id = wp_insert_post( $post );
return $post_id;
}
}
add_filter('acf/pre_save_post' , 'add_new_post' );
function acf_set_featured_image( $value, $post_id, $field ){
// echo '<pre>'. print_r($value,1).'<br/>'.print_r($_POST,1).'</pre>'; die();
if($value != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta($post_id, '_thumbnail_id', $value);
}
return $value;
}
// acf/update_value/name={$field_name} - filter for a specific field based on it's name
add_filter('acf/update_value/name=zdjecie1', 'acf_set_featured_image', 10, 3);
function acf_set_miejsce( $value, $post_id, $field ){
// echo '<pre>'. print_r($value,1).'<br/>'.print_r($field,1).'</pre>'; die();
if($value['address'] != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta($post_id, 'codespacing_progress_map_address', $value['address']);
}
if($value['lat'] != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta($post_id, 'codespacing_progress_map_lat', $value['lat']);
}
if($value['lng'] != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta($post_id, 'codespacing_progress_map_lng', $value['lng']);
}
return $value;
}
// acf/update_value/name={$field_name} - filter for a specific field based on it's name
add_filter('acf/update_value/name=miejsce_kradziezy', 'acf_set_miejsce', 10, 3);
How to make that fields will be cleared everytime new user enter them. There is no option to edit posts they can only add them ?
This issue can occur when using new
as the post_id parameter.
If your code has allowed ACF to save any data to new
, then this data will always load into the form.
The easiest solution is to edit your wp_options table and remove all entries that start with new_
.
Thanks
E
Ok so when this issue occur only when using new parameter is there a way to fix this from happening maybe some changes to code ? if i change new to ‘abc’ it will still do abc_ and load info to from ? How can i prevent it from loading back to form ?
Your filter which adds the function add_new_post
will change the $post_id from ‘new’ to the newly inserted ID.
This issue will not happen if your filter correctly modifies the $post_id.
Thanks
E
Can You please help me fixing the code i have paid a guy to write the code i don’t know anything about php 🙁
If i change it:
from:
'post_id' => 'new',
to
'post_id' => 'abcnew',
and
from
if( $post_id == 'new' ) {
to
if( $post_id == 'abcnew' ) {
It will still sometimes happen ?
Changing these ID’s is 1 way to solve the issue, however please read my last reply as it also answers this question.
Thanks
E
This issue will not happen if your filter correctly modifies the $post_id.
So if i change the post_id to abcnew the filter will now correctly modifie the post_id ? As i posted before i have paid for the help because i don’t have any knowlage in php
Your function expects the $post_id parameter to be set as ‘new’. You can see this here:
function add_new_post( $post_id )
{
if( $post_id == 'new' ) {
In short, you do NOT need to change the $post_id parameter.
The easiest solution is to edit your wp_options table and remove all entries that start with new_.
OK but if i delete the new_ from wp_options table it will fix my problem one for all or it will come back some day ?
It will fix it forever assuming that your pre_save_post
filter works as expected.
Perhaps it is best to ask your web developer if the issue arises again.
Thanks
E
The topic ‘Something broke – Clear all inputs ?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.