Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Front end user submission of content WordPress

Hi everybody,

I need to allow registered Wordpress users to submit content that is put into "draft" status awaiting moderation by an administrator. As an administrator I can then approve or decline the content based upon in-house criteria.

I have created a custom post type (classified ads) with the following fields,

Title (default Wordpress editor)
Content(default Wordpress editor)

Year*
Mileage*
Number of Owners*
Image field 1*
Image field 2*
Image field 3*
Image field 4*
Image field 5*

# Chasis number
# Registration number
# First name
# Last name
# Phone number

* Fields marked with an asterisk are part of a custom meta box that I have created for this custom post type.

# Fields marked with a hash are required fields but do not get published as part of the post once approved and are there for administrative purposes only.

I have tried using Gravity Forms to accomplish this task but Gravity Forms only allows you to select fields that are "custom fields" and in this instance my custom meta box fields don't show up as "custom fields".

I am using the WPAlchemy MetaBox class by: http://www.farinspace.com/wpalchemy-metabox/

If there's anything else I can provide you to help in solving this issue for me just let me know.

Wordpressing

Answers (6)

2011-08-22

Ryan Riatno answers:

Actually there's a plugin for that [[LINK href="http://wordpress.org/extend/plugins/tdo-mini-forms/"]]http://wordpress.org/extend/plugins/tdo-mini-forms/[[/LINK]].


Ryan Riatno comments:

Here's some of tutorials :
[[LINK href="http://www.wpbeginner.com/plugins/how-to-allow-users-to-submit-news-posts-to-your-wordpress-site/"]]WPBeginner[[/LINK]]
[[LINK href="http://thedeadone.net/blog/tutorial-video-for-tdo-mini-forms/"]]TheDeadone[[/LINK]]

Let me know if you have any questions about the plugin setup :)


Wordpressing comments:

Hi Ryan,

Thanks for your reply. I've tried TDO Mini Forms last night and it didn't support the custom post type setup I have nor the fields I've created as part of the custom meta box that are associates with this particular custom post type.

Thank you for your suggestion however.

2011-08-22

Maor Barazany answers:

TDO can do this, but I'm not sure it fully supports Custom Post Types, I think I saw there were issues with it.


You may also use the method described in this post [[LINK href="http://wpshout.com/wordpress-submit-posts-from-frontend/"]]http://wpshout.com/wordpress-submit-posts-from-frontend/[[/LINK]]

You may also use the [[LINK href="http://wordpress.org/extend/plugins/pods/"]]PODs framework[[/LINK]], but if you need a single frontend form to submit and that it, then PODS will be overkill for that.


Wordpressing comments:

Hi Maor,

Thanks for you reply.

The WPShout.com method looks like it's on the right track however how do I marry up the fields I've created in my custom meta box to the form?

I can see the form supports all the usual suspects like title, description, categories and tags but they are standard Wordpress fields.


Maor Barazany comments:

Every field in your custom metabox is actually (in most cases) a custom fields. In other place, one meta_filed holds a serialized array of several key/value custom fields.
I'm not familiar with WPAlchemy MetaBox class, I usually use another MB class I have.
Once you know what are the custom fields that should be updated, you can just use the $postid you get returned from the wp_insert_post, and use the [[LINK href="http://codex.wordpress.org/Custom_Fields"]]Custom Fields[[/LINK]] functions (such as add_post_meta / update_post_meta etc. to insert the relevant custom fields (=fields in tyour metaboxes) as well.


Wordpressing comments:

The fields apart of my metabox actually don't show up under the custom fields section. So I'm not sure how to marry them up with the form...




Wordpressing comments:

Actually I think I've figured out why they don't show up - it has to do with prefixing an underscore _ to the class name which hides them from the custom fields drop down.

Are you able to show me a code example of how I use the add_post_meta in the context of the form example shown as WPShout.com?


Maor Barazany comments:

Thats good they are not being shown there.... That means that the custom field name probably starts with undercsore sign <em>_fieldname</em> and thus won't be shown to the user as a regular custom field and will confuse him..
You should look in the docs of the MetaBox class, or try to search in the db for a unique value you have in one of the fields, and then you can do a small "reverse engineer" in order to reveal the logics in the fields' names.


Maor Barazany comments:

As I read a bit in the link of the MB class, you may [[LINK href="http://www.farinspace.com/wpalchemy-metabox/#template"]]see here[[/LINK]]

from reading this -


// instead of using helper functions, you can also use ...
$meta = get_post_meta(get_the_ID(), $custom_metabox->get_the_id(), TRUE);

// or ... (same as above)
// $meta = $custom_metabox->the_meta();

echo $meta['name'];
echo $meta['description'];

foreach ($meta['authors'] as $author)
{
echo $author;
}

foreach ($meta['links'] as $link)
{
echo $link['title'];
echo $link['url'];
if ($link['nofollow']) echo 'is-nofollow';
echo $link['target'];
}


It appears that the the id of the custom field is the ID of the metabox you gave when defining it -


$custom_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta',
'title' => 'My Custom Meta',
'templa


Then, the functions returns an associative array of keys, each key in that array is, I suupose, a field in the metabox.
So you just have to read in the frontend all the custom fileds and gather them in assoc arrays of each metabox, and than pass to WP the ID of the post and the ID of the MB, and store it with add_post_meta.

Be careful with handling input fields from the frontend. You must always sanitize, clean check the data is being received from the user, to avoid SQL injections and such things...


Maor Barazany comments:

Code example can be something like this:

Instead the line -

wp_insert_post($post);


Use this function with variable assign -


$newpost = wp_insert_post($post);


So now $newpost has the id of the post that was just inserted.

Now, you will pass custom fields -


<?php add_post_meta($newpost , $meta_key, $meta_value); ?>


Where $meta_key is the key you have from your MB class, and also the value.

2011-08-22

Clifford P answers:

Not sure if this post could help point you in the right direction: [[LINK href="http://wpquestions.com/question/show/id/2855"]]http://wpquestions.com/question/show/id/2855[[/LINK]]

2011-08-22

Andrew Wong answers:

Try something like this:


<?php // Adding posting form and input into WordPress
$new_post = array();
$new_post['post_title'] = $_POST['post_title'];
$new_post['post_content'] = $_POST['post_content'];
$new_post['tags_input'] = $_POST['post_tags'];
$new_post['post_category'] = array($_POST['cat']);
$new_post['post_status'] = 'publish';
$new_post['post_type'] = 'musics';
$meta_box = $_POST['meta'];
if (!isset($_POST['submit'])) {
?>

<!-- The form -->
<form method="post" action="">
<table>
<tr><th><label for="id_meta">Meta Box:</label></th><td><input id="id_meta" type="text" name="meta" maxlength="100" /></td></tr>
<tr><th><label for="id_category">Category:</label></th><td><?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?></td></tr>
<tr><th><label for="id_tags">Tags:</label></th><td><input id="id_tags" type="text" name="post_tags" maxlength="100" /></td></tr>
<tr><th><label for="id_title">Title:</label></th><td><input id="id_title" type="text" name="post_title" maxlength="100" /></td></tr>
<tr><th><label for="id_text">Message:</label></th><td><textarea id="id_text" rows="10" cols="40" name="post_content"></textarea></td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Submit" name="submit"/></td>
</tr>
</table>
</form>


<?php // Updated or error message
} else {
echo '<p>';
$new_post_ID = wp_insert_post( $new_post );
if( $new_post_ID ) {
add_post_meta($new_post_ID, 'meta', $meta_box);
echo 'Post updated!';
} else { ?>
An error occured while submitting your letter. Please go back and try again.</p>
<?php } echo '</p>'; }
?>


That should support post type.


Wordpressing comments:

Hi Andrew,

So if my meta_box has for example several fields would I do something like this,

$meta_box = $_POST['field1'];
$meta_box = $_POST['field2'];
$meta_box = $_POST['field3'];

If so, do I need to change the following where it says 'meta' ?

if( $new_post_ID ) {

add_post_meta($new_post_ID, 'meta', $meta_box);


Andrew Wong comments:

Hi Wordpressing,

It goes like this:

$meta_box1 = $_POST['field1'];
$meta_box2 = $_POST['field2'];
$meta_box3 = $_POST['field3'];


Then for the updating part:

add_post_meta($new_post_ID, 'field1', $meta_box1);
add_post_meta($new_post_ID, 'field2', $meta_box2);
add_post_meta($new_post_ID, 'field3', $meta_box3);


Should be like that.


Wordpressing comments:

Hi Andy,

That makes sense. So I gave it a try and it doesn't work. The title and description works.

If I have a look in my file that controls the metabox

I have for example;


<?php $mb->the_field('age'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/>


But if I try and do,

$meta_box1 = $_POST['age'];

and

add_post_meta($new_post_ID, 'age', $meta_box1);

and

<input id="id_meta" type="text" name="age" maxlength="100" />

But despite whatever value I put into the input field, it doesn't get carried across.

Any thoughts?

Feel like I'm on the right path... but...

2011-08-23

ej_emman answers:

Ahm,
Are you using Andrews suggestion. if yes, you dont need the meta_box1. Just add <input> field age to his created form, nothing else you need to add.

If you create another form you have to create an array value for $new_post_ID.


Wordpressing comments:

EJ,

I am trying to use Andrews method. But I don't really understand what you are talking about.

In my functions.php file I have defined a custom meta box as follows (using the WPAlchemy MetaBox Class [[LINK href="http://www.farinspace.com/wpalchemy-metabox/"]]here[[/LINK]];

$custom_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta', // underscore prefix hides fields from the custom fields area
'title' => 'Vehicle Information',
'template' => TEMPLATEPATH . '/WPAlchemy/custom/simple_meta.php',
'types' => array('stock_list','classifieds','page','post')
));


Inside the simple_meta.php file referenced above which appears in the post editing screen I have fields defined like so;

<?php $mb->the_field('age'); ?>

<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/>

<?php $mb->the_field('mileage'); ?>

<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/>

<?php $mb->the_field('owners'); ?>

<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/>


I need to know how to represent the above fields found in my metabox as seen in the form code posted by Andrew Wong.

Can you show me an example based upon the naming structure I have just provided?

2011-08-23

Ivaylo Draganov answers:

Hello,

I did just that recently for a job listing website. You can see the form here:
http://work.ip4all.com/post/

It is based on Jeff Starr's little known plugin:
[[LINK href="http://perishablepress.com/user-submitted-posts/"]]http://perishablepress.com/user-submitted-posts/[[/LINK]]

But I've modified the code to handle custom post types, custom fields(created with WPAlchemy) and custom taxonomies. Download the plugin and give it a shot. If you wish I can send you my modified version to see how I've tweaked the code.


Wordpressing comments:

Hi Ivaylo,

This sounds promising. Since I am using WPAlchemy for all my metabox creations I'd sure like to have a look at your code to see how I can get it to work.


Ivaylo Draganov comments:

I've changed just two files from the original plugin:
[[LINK href="http://pastebin.com/T8Gz7nXR"]]submission-form.php[[/LINK]]
[[LINK href="http://pastebin.com/GFXT3r2V"]]user-submitted-posts.php[[/LINK]]

Diff the files to see where the changes are.