Ask your WordPress questions! Pay money and get answers fast! (more info)

Post Combined Info From Custom Fields To One Custom Field WordPress

Hi,

I'm collecting information for a combination of various custom fields, is it possible to push that info in a combined fashion to another custom field that will not be filled out by the user?

Example:

1: Street Number:
<input name="street_number" type="text" value="<?php echo esc_attr( $listing->street_number ); ?>" class="required" />
2: Street:
<input name="street" type="text" value="<?php echo esc_attr( $listing->street ); ?>" class="required" />
3: City:
<input name="city" type="text" value="<?php echo esc_attr( $listing->city ); ?>" class="required" />
4: State:
<input name="state" type="text" value="<?php echo esc_attr( $listing->state ); ?>" class="required" />


And then push the above collected info to here:

<input id="listing-address" name="address" type="text" value="<?php echo esc_attr( $listing->address ); ?>" />

Thanks in advance for your help.



Answers (3)

2012-12-30

Dbranes answers:

Hi, the usual way to do this is by using

add_post_meta($post_id, '_field_name', 'field_value');

where the underscore _ is used to hide the field from the user and the $post_id is the corresponding post id.

So you could use

add_post_meta($post_id, '_address', $address);

or

update_post_meta($post_id, '_address', $address);

where $address is constructed from the other values.


But where are you displaying this form?

Can you display the code you use to save and process the form?


2012-12-30

Naveen Chand answers:

<strong>Solution 1: Assuming you are using POST method in your form, this is how we can do:
</strong>
Joining two or more variables using $_POST:

<form method="POST" action="">
1: Street Number:

<input name="street_number" type="text" value="<?php echo esc_attr( $listing->street_number ); ?>" class="required" />

2: Street:

<input name="street" type="text" value="<?php echo esc_attr( $listing->street ); ?>" class="required" />

3: City:

<input name="city" type="text" value="<?php echo esc_attr( $listing->city ); ?>" class="required" />

4: State:

<input name="state" type="text" value="<?php echo esc_attr( $listing->state ); ?>" class="required" />
<input type="submit" name="submit"/>
</form>

if (!empty($_POST['submit']) {
$street_number = $_POST['street_number'];
$street = $_POST['street'];
$city = $_POST['city'];

$address=$street_number ." " .$street ." " .$city; //this is where we are joining them together.


You can now use $address in whichever way you want. You can send this to database using wordpress functions such as add_meta or update_meta.

<strong>Solution 2: Assuming you are using GET method.
</strong>
Joining two or more variables using $_GET:

<form method="GET" action="">
1: Street Number:

<input name="street_number" type="text" value="<?php echo esc_attr( $listing->street_number ); ?>" class="required" />

2: Street:

<input name="street" type="text" value="<?php echo esc_attr( $listing->street ); ?>" class="required" />

3: City:

<input name="city" type="text" value="<?php echo esc_attr( $listing->city ); ?>" class="required" />

4: State:

<input name="state" type="text" value="<?php echo esc_attr( $listing->state ); ?>" class="required" />
<input type="submit" name="submit"/>
</form>

if (!empty($_GET['submit']) {
$street_number = $_GET['street_number'];
$street = $_GET['street'];
$city = $_GET['city'];

$address=$street_number ." " .$street ." " .$city; //this is where we are joining them together.


Even in this case, you can now use $address in whichever way you want to. It contains all the data you require.


Edwin comments:

Hi,

Here is the full code:

<?php global $va_options; ?>

<?php do_action( 'appthemes_notices' ); ?>



<div id="register">


<div id="form-left">


<form id="create-listing" enctype="multipart/form-data" method="post" action="<?php echo va_get_listing_create_url(); ?>">
<?php wp_nonce_field( 'va_create_listing' ); ?>
<input type="hidden" name="action" value="<?php echo ( get_query_var('listing_edit') ? 'edit-listing' : 'new-listing' ); ?>" />
<input type="hidden" name="ID" value="<?php echo esc_attr( $listing->ID ); ?>" />



<fieldset id="essential-fields">

<h1 class="top-headline">List Your Practice and Increase Your Online Exposure<br /> To Gain More Clients For Your Practice.</h1>


<div class="form-field"><label>
<?php _e( 'Doctor Name', APP_TD ); ?></label>
<input name="post_title" type="text" value="<?php echo esc_attr( $listing->post_title ); ?>" class="required" />
</div>



<div class="form-field-remove">


<label>
<?php _e( 'Address, please use the following format: Street Number, Street, City, State', APP_TD ); ?></label>
<input id="listing-address" name="address" type="hidden" value="<?php echo esc_attr( $listing->address ); ?>" /><br />


</div>

<?php
// if categories are locked display only the current listing category
if ( va_categories_locked() )
$listing_cat = $listing->category;
else
$listing_cat = array();
?>

<div class="featured-head"><label><?php _e( 'Select Your City:', APP_TD ); ?></label></div>

<div class="form-field"><label>
<?php _e( '', APP_TD ); ?>
<?php wp_dropdown_categories( array(
'taxonomy' => VA_LISTING_CATEGORY,
'hide_empty' => false,
'hierarchical' => true,
'name' => VA_LISTING_CATEGORY,
'selected' => $listing->category,
'show_option_none' => __( 'Select City', APP_TD ),
'class' => 'required',
'orderby' => 'name',
'include' => $listing_cat
) ); ?>
</label></div>


</fieldset>




<div id="custom-fields">
<?php
if ( $listing->category ) {
the_listing_files_editor( $listing->ID );
va_render_form( (int) $listing->category, $listing->ID );
}
?>
</div>



<?php do_action( 'va_after_create_listing_form' ); ?>

<fieldset>
<div class="form-field"><input type="submit" class="form-button" value="<?php echo esc_attr( $action ); ?>" /></div>
</fieldset>

</form>


<?php

if (!empty($_POST['submit'])) {

$street_number = $_POST['app_streetnumber'];

$street = $_POST['app_streetname'];

$city = $_POST['app_city'];

$state = $_POST['app_state'];

$address=$street_number ." " .$street ." " .$city ." " .$state; //this is where we are joining them together.
}
?>

<?php update_post_meta($post_id, 'address', $address); ?>



<div id="listing-map"></div>
</div>

</div> <!-- form left -->





</div> <!-- register -->


Unfortunately currently the address information is not posted to the "address" custom field.



Edwin comments:

Also something that is important.

The joined fields need to be separated by a comma, however no comma is required between the street number and streetname.


Naveen Chand comments:

I have gone through your theme file and I am suggesting this new solution after I have looked at your theme. Ignore my previous solution.

<strong>Method 1:</strong>
What you need to do is.. inside the file, just above this line:

<input id="listing-address" name="address" type="hidden" value="<?php echo esc_attr( $listing->address ); ?>" /><br />


Add this code:

<input type="text" id="streetnumber" name="streetnumber"/><br/>
<input type="text" id="street" name="street"/><br/>
<input type="text" id="city" name="city"/><br/>
<input type="text" id="state" name="state" onchange="{document.form.address.value='Address: '+document.form.streetnumber.value+', '+document.form.street.value+', '+document.form.city.value+', '+document.form.state.value;}"
/><br/>


What I am trying to do here is: I am trying to auto-generate address box using the other boxes through client-side scripting using javascript. So the moment, one fills-up State and changes their cursor from that box, the Address box gets automatically updated. And you can submit your form as it is. No need to of any other php code and you can leave the template and theme functions as it is. No changes in them.

If the above doesnt work, then try giving the form a name like this:

<strong>Method 2</strong> (use only if the above doesnt work.
<blockquote>
<form id="create-listing" name="create-listing" enctype="multipart/form-data" method="post" action="<?php echo va_get_listing_create_url(); ?>">


And then, use the above method but inside <strong>onchange</strong> javascript, replace the word "form" with "create-listing".
use this method only if the first method doesnt work.
</blockquote>

Hope this works. Please let me know the errors that come if it doesnt.

2012-12-30

Arnav Joy answers:

try this


<?php

if (!empty($_GET['submit']) {

$street_number = $_GET['street_number'];

$street = $_GET['street'];

$city = $_GET['city'];



$address=$_REQUEST['street_number'] ." " .$_REQUEST['street'] ." " .$_REQUEST['city']. " " .$_REQUEST['state']; //this is where we are joining them together.

?>
<input id="listing-address" name="address" type="text" value="<?php echo esc_attr( $address ); ?>" />






Arnav Joy comments:

<?php

use this function in functions.php

add_action('publish_post','modify_custom_field');


function modify_custom_field($post_id) {

$street_number = get_post_meta($post_id,'street_number',true);

$street = get_post_meta($post_id,'street',true);

$city = get_post_meta($post_id,'city',true);

$state = get_post_meta($post_id,'state',true);

$address = $street_number ." " .$street ." " .$city. " " .$state;

update_post_meta($post_id,'address',$address);

return;
}



?>


Edwin comments:

I've updated the functions.php snippet:

add_action('publish_post','add_to_address');


function add_to_address($post_id) {

$street_number = get_post_meta($post_id,'app_streetnumber',true);

$street = get_post_meta($post_id,'app_streetname',true);

$city = get_post_meta($post_id,'app_city',true);

$state = get_post_meta($post_id,'app_state',true);

$address = $street_number ." " .$street ." " .$city. " " .$state;

update_post_meta($post_id,'address',$address);

return;
}


But that did not seem to work.


Arnav Joy comments:

your full code seems that you are adding post from the front end , so can you show where you have used code to insert post data to the table

you must be using wp_insert_post or something , right ?


so i need to check that place , can you share full code