Classipress: Adding Custom Fields
Ok been a lot of questions to adding extra fields to the form. Now pre 2.8 this was a bit of a complicated process but thanks to the new system this works with your posts custom fields you can add as many fields as you need to relatively easy. This first tutorial will show you how to make a “basic” text field. For some of you this will get you on the road and show you the process enough that you can go from here to do anything you need. For the rest i will be posting more tutorials.
Step 1
Ok the files we will be working with are these.
single.php
functions.php
post-form.php
form-process.php
Step 2
Ok grab the post-form.php file. This file is the file that your users will see when they are posting their ad. Really what we are doing here is creating the field that they will insert their information into. Find this section in the file:

Ok now we will add out new field right under to “location” field.
Here is the basic code to do this:
<label for="XX"><?php _e('XX'); ?></label>
<input type="text" id="XX" name="XX" maxlength="100" value="<?php echo $XX; ?>" />
Say we want our new field to be for people to put the “Make” of a vehicle. We would change the code to look like this:
<label for="make"><?php _e('Make'); ?></label>
<input type="text" id="make" name="make" maxlength="100" value="<?php echo $make; ?>" />
Or for a dropdown you would use
<label for="model">Holden Model</label>
<select name='make' id='model' class='model' >
<option value=''>Select One</option>
<option>Manaro</option>
<option>Ute</option>
<option>Commodore</option>
</select>
So now your code in the file should look like this:

Now your site will look like this:

Remember that this will not function yet.
Now we are through with this file so lets move on.
Step 3
Open the form-process.php file. This file processes the form information. This is where everything must be perfect. They will be a few places in this file we need to edit. To help you with later edits we will place all the new field operations under the “location” operations.
Find this in your file:

Copy this code:
$make = cp_filter($_POST['make']);
and insert it just under the location tag. Now your file should look like this:

Now lets look for this section in the same file:

Now copy this code:
add_post_meta($post_id, 'make', $make, true);
and add it once again just under the location tag so it will look like this:

Now we are all done with this file.
Step 4
Lets open the functions.php file. What we are doing to this file is adding the search terms. We want the system to know to search into this field for user searches. If you do not want the search to use this field for query then no need to edit this file. We only have one thing to edit here so this will not take very long.
Ok lets find this section:

All you need to do is add the “make” at the end like this:

Step 5
Ok now lets open the single.php file. We now need to place out new field in the right location so people can see it when they are viewing the ad. This step is totally up to you and can be added just about anywhere in the single.php file. But for today lets add it under the Tags and Stats section.
Ok lest find this:

The use this code:
<strong><?php _e('Make'); ?>:</strong> <?php echo get_post_meta($post->ID, "make", true); ?>
And insert it under the “stats” to make it look like this:

And thats all. Now you have just added a new field to your theme. You may add as many has you want to in this same manner.

/rating_on.png)

Hi EH,
Followed everything to the tee. The custom field appears everywhere (post-form on the website, post-edit page in wordpess dashboard etc.).
Problem: The field value does not appear; neither in the post, nor in the submitted form.
Example: Let us say the user types HONDA in front of ‘Make’ as in your example, the field ‘Make’ remains blank in the post. There is no way for me to find the value, other than writing an email to the user and asking them about the make, then going to the wordpress dashboard and adding the make value in front of the field and updating the post.
Kindly comment/help. Many thanks.
Is there a way where we can show custom fields based on category selected by author to submit an article.
Eg: Automobiles, Electronic Items as categories
1) When author selected automobile as a category custom fields should be vehicle name, Type, VIN number, Model: , color etc.
2) When author selects Electronic as a category , custom fields should be price, description.
Thanks in advance.
–Kumar
Hi !
Thanks for this great tuts !
I didn’t know that you have to do all these steps to add a custom field to the post… i thought you need only the step 1
Could you explain me (an for others too) how i can display a drop down list and not an input textfield to enter the custom field…
Thanks in advance.
Julien
Ill add it to the tutorial soon.