Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to add a "Source" field to my posts form so I can show from where I get the published info. Didn't know if this exists already because I'm just starting with WP. So I do some research and found those:

But isn't clear for me. I start with the latest one and install the plugin Custom Field Template as suggested, then go trough the guide but get stopped at step #4 because the code is missing or isn't displayed. Can any help me to advance in there? Also I'm using a custom theme but didn't know where I must write the code mentioned at step #4. Any advice, help? Suggestions are welcome too ;)

Cheers and thanks in advance

share|improve this question

1 Answer 1

up vote 1 down vote accepted

(Just read the third link in your question. It looks like a line of code is missing on step #4. I don't think there's really a need to use the plugin.)

There are two ways to do what you are requesting. 1) Learn what a metabox is, or 2) become acquainted with the custom fields box on the edit-post screen in the WordPress admin. Both methods use the same database table to store their information which is relative to a post.

If you are just starting out and this is for you (and not a client), I'd recommend method 2 - the custom field box on the edit-post screen (you may need to toggle its visibility from the "screen options" drop down menu near the top right corner of the screen).

Once it is visible, you'll need to create a name => value pair. In your case, give it a name of "source" and then type in the value as whatever that source should be. Remember the name that you use in the name field, as you'll want to make sure that it is the same name for every post entry (in this case "source"). Once you've created it the first time, it should also become available as a drop down option on subsequent post entries.

By doing this, what you are creating is a postmeta entry in the database which has just four columns (or settings): a unique ID, the post ID, the name, and the value (which you should have just created). I point this out, only because metaboxes (option 1 from above) creates the exact same entry in the database.

Now, to access this information from within your template, you need only use the get_post_meta($post_id, $meta_name); function to get your source information. Something like this should work from inside the WordPress loop:

$source_value = get_post_meta($post_id, 'source');
if( isset( $source_value ) ){
    echo $source_value;
}

This is the custom fields way of doing it.


On the other hand, if you want to create a cleaner presentation on your edit-post screen that will also have the name set for you, that's where you'd learn how to use a metabox.

If you want to experiment with metaboxes, you can either learn it straight by reading the WordPress Codex on metaboxes (how to add, edit, etc.) or use a metabox class or plugin. I've used Dimas's WP Alchemy Class and also RW MetaBox Class. Justin Taddock has a good tutorial on how to work straight with the built-in functions to create a metabox.

With any of these metabox approaches the way to retrieve information for use in your theme is essentially the same. Just pay attention to your meta_name value.

How's this for a starting point?

share|improve this answer
    
Excellent answer and very helpful and yes it help me to find a starting point. I´ll check all links you leave me and also give a try to your solution, by now is for me but what did you recommend in case the solution was for a client? –  ReynierPM Sep 7 '12 at 13:12
    
Definitely use metaboxes if you want to use the same functionality for a client site. It's much more intuitive for the client to just see a predefined field and enter the value there then having to set both the name and value of the meta data. I really like WP_Alchemy because it's got quite a bit of functionality, but the one major draw back is that the way that it stores information in the database makes it impossible to search for that meta data in any sort of query request. The RW Meta Box, though not as polished, inserts data in the database in such a way that it can be searched. –  Brent Sep 7 '12 at 20:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.