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

add custom field value directly from site WordPress

  • REFUNDED

Hi,
I'm building a site for a holiday we're planning with a big group of friends. We have planned many activities, and each person can make his own activity calendar. I'm trying to do this with the following approach:
- all participants should register on the site
- activities using a custom post type (title, description, location, date, time start, time end)
- users can add / delete activities
- user have a personal page where the can see their activity list

I want to make the 'sign up' pretty easy, without any backend access. When logged in the user should see a 'sign up' button next to the title. When he clicks this button, I want to submit the 'username' into a customfield (eg 'attendees') linked to that specific activity. When a user is attending a certain activity, there should be a button to cancel the sign up and delete the username in that custom field.

<strong>So what I'd like is:
- function that would submit a username into a customfield directly from the frontpage
- similar function that would delete it
</strong>

I guess some (part of a) plugin could do this, but I can't find anything that works for me. Should work with WP 3.0, btw.

<em>Bonus points:
1/ sign up/delete without reloading the page using jQuery or similar
2/ using the same button in same place for both actions (with jQuery or similar): grey badge -> not signed up; green badge -> signed up</em>

Answers (5)

2010-08-12

David Navarrete answers:

Hi is easy.. you have two kindes of custom fields, for post and for author.

you problem should be for author you need this function update_user_meta($user_id, 'name', 'value')

now for create a user i have this code.


http://pastebin.com/wFyCjG3r

you can call this code for jquery like this

$('a#save').click(function(){
datos = $('form[name=submit-user]').serialize();
$.ajax({
url : '/save-user/',
type : "GET",
data : datos,
sucess: function(msg){
alert('sucess');
}
});
return false;
})

if you need more help, tell me

pd: sorry for my bad english

cya


cotton comments:

thank you for your answer, but I would prefer to add the user(-id) to the post and not the post(-id) to the user. It makes more sense for the stuff I want to do on the site. For example: I would like to display the signed up users on the activity detail page and I think this will make it more difficult if the info about the attendees is stored in the user table.

I might have lead you in the wrong direction by saying that I would like to have a activity list page for every user. That does not have to be the profile page. I was thinking more in the lines of a page with custom template that would display only the posts that have a customfield with that certain username.

For example: http://example.com/eventlist/user/(specific username)

Maybe I'm making this too complicated :-)

2010-08-12

enodekciw answers:

Imo, custom fields is a bad idea for this.
You should use MySQL to archieve something like this.
When user signs up to an activity, it's username and activity ID is added to some new table.

It'll be easier to manipulate later, trust me.


cotton comments:

You are probably right, but I am not php/mysql savvy. If I would have to implement a feature like this it would probably take me several days of copy/paste and trial & error coding. I don't really need the whole thing to be editable later-on or have a nice UI in the backend for it.

Do you think putting it in the custom fields will make the site slow? Would it be better with an extra table?

2010-08-12

Joe Jenkins answers:

If you use the Gravity Forms plugin which, in my opinion is the best way to create forms with WordPress, this is built into it.

You can do all of this and much more.

[[LINK href="http://www.gravityforms.com"]]www.gravityforms.com[[/LINK]]

You'll notice that that isn't an affiliate link. I suggest it because I use it for all this type of thing.

Let me know if you need any further explanation.


cotton comments:

thank you for your answer.
I already use gravityforms. You can submit a new post with it, but it can not be used to append an existing post.

2010-08-12

Mykyta Savchenko answers:

Put this in your loop


<form action="" method="post">
<input type="text" name="Name" value="username1">
<input type="hidden" name="toPost" value="<?echo $post->ID; ?>">
<input type="submit" value="Submit" name="submit">
</form>


and this somewhere out of the loop:



<?php

if(isset($_POST['submit'])) //If submit is hit
{


$name = ','.mysql_real_escape_string($_POST['Name']); // fetch user input from the form. You could use user_id or username
$toPost = $_POST['toPost']; // our post_id to insert username


$result=mysql_query("UPDATE wp_postmeta SET meta_value=concat(meta_value, '$name') WHERE post_id=$toPost AND meta_key='Name'");
//SQL query: add to field "Name" new username

// uncomment for debug
//print mysql_affected_rows();
// print mysql_error();


if(!$result) die('Error: '.mysql_error());
else echo 'Inserted row with id='.mysql_insert_id();
}

?>



This will add the value of form to post you chose.
Finally you could insert user_id or his username and delete the textarea


cotton comments:

Thank you. Will try this out.


cotton comments:

Doesn't work apparently.


Mykyta Savchenko comments:

Try uncomenting strings that i've marked "debug" and post the error message. Also create field for test post named "Name"


cotton comments:

I got this output:
0Inserted row with id=0

2010-08-12

West Coast Design Co. answers:


Cotton,

Not sure how far $15 will get you but checkout;

[[LINK href="http://www.scriptlance.com/"]]http://www.scriptlance.com/[[/LINK]]

Cheers.


cotton comments:

I know $15 isn't that much, but this is a little unfunded personal project so I can't really spend much more on it. I'm hoping someone on here will have an easy solution. thanks.