Justin over at justintadlock.com made a post a few days ago about how to preset text in the WordPress post editor. It’s a great post, with an interesting filter detailed. In the comments, somebody mentioned that they’d like to be able to preset custom fields as well – something that seems like it shouldn’t work (Custom fields need a post id to work on, and new posts dont have a post id). Yesterday, the workaround hit me like a slap in the face while I was in the shower – so I decided to package up this, along with the original code that Justin published in a plugin.
It’s not the most elegant piece of code in the world, but it works on all the installs I’ve tried it on. I’ll try to put up a post detailing how it works soon, but in the meantime, feel free to download the plugin and give it a try.
ps2: don’t be (furtherly) confused by “xy being the pageID” and “mypagetype”. Those are mistakes.
I really meant “xy being the postID” and “myposttype”.
Hi Peter.
Luckily I can safely answer “no” to both questions. The user doesn’t even need to know about all this “is_child_of” stuff. I’ve been looking at your plugin code, and the solution is right there.
Obviously some stuff is there just to have different fields and values submitted by the admin.
I later found out about some very basic/childish/foundation concept: the $_GET.
The post-new url has the “parentId” already in it (is_child_of=xy), so I could grab it from there with something like ;
I just need to put together a basic stripped down version of your code, a simple function that sets meta_key “is_child_of” to the value of ”
And where to put it.
I know I’m learning something here, but it’s taking me ages, so if you can help, that’d be awesome.
BTW, mail is valid, so..we can go on with this here, or one on one. whatever you like best.
Code got stripped so, to make sense, I’m adding it here without the php tags
The post-new url has the “parentId” already in it (is_child_of=xy), so I could grab it from there with something like php echo $_GET["is_child_of"];
I just need to put together a basic stripped down version of your code, a simple function that sets meta_key “is_child_of” to the value of “php echo $_GET["is_child_of"];
Hi Peter, thanks so much for your help + taking the challenge.
Upon test: the first function works, the second one gives me the pale screen of sadness.
http://wordpress.pastebin.com/m13e8b081
I hate the pale screen with a passion.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You did it man! This single trick will finally make my whole project come true. I’ve been searching for this solution for like, ages. Coupled with more fields plugin, It totally makes for a much friendly cms environment. From here, it won’t take much for us to go live. I’ll show you when it’s done, so you’ll see what you helped doing. WP community rules.
On another note: why on earth would somebody wear such thing as a a pale screen t-shirt?
http://img145.imageshack.us/img145/8614/palescreentshirt.jpg
that’s beyond me, really.
Peter,
Thank you for this plugin. It will help me cut out about 7 steps out of my user created content system. I have got it working but I keep getting this error message:
Warning: Invalid argument supplied for foreach() in /home/*****/public_html/wp-content/plugins/default-post-content/default_post_content.php on line 53
Warning: Cannot modify header information – headers already sent by (output started at /home/*****/public_html/wp-content/plugins/default-post-content/default_post_content.php:53) in /home/*****/public_html/wp-includes/pluggable.php on line 865
I am not sure what it asking me to do here. Do you have any ideas?
John
April 18, 2009
Hi Peter. I’ve been searching left and right for a viable solution to insert custom_field values in a new post. I got here by reading your comment on justintadlock.com, so I have a basic understanding of your concept behind updating custom_fields on the post editor, before the post is saved. I’m not a programmer but WP got me learnin’ a bit, just by tinkerin’ with code and understanding the logics.
Here’s a further challenge: I’m planning to have “contribute_to_this_post” links on some posts.
Site contributors, if logged, can see and follow the link so a pre-populated post editor opens up for them.
Here comes the pain: I’m trying to establish a parent-child relation between the post they are contributing to and their contribution posts. Since there’s no such thing as parent/child posts, here’s my idea: using a “is_child_of” custom_field. The “contribute_to_this_post” link should be able to pass the postID onto the new post as the value for “is_child_of”.
Where I am now:
I’m using morefields plugin, so I have page types with the proper custom_fields ready to be filled.
I’ve got a function that gives me this url:
http://mysite.dev/wp-admin/post-new.php?type=my-post-type&is_child_of=xy
(xy being the pageID of the page where the contribute link is.)
That will effectively open a new editor, of type “mypagetype”, but then the value of is_child_of won’t go in the field.
Do you think your method could be a step closer to my solution? Any idea?
ps: Err..sorry for the lenghty comment.