Creating the options pages is something we’ve touched upon previously here on [wp], and whilst developing my soon-to-be-released WordPress [theme], Biblioteca, I wanted to add some options to the post writing page, and in this post I’m going to be sharing how I did it.
How the in-post options work
Basically, we’re going to be adding some boxes to the post and page writing pages that give our users a number of options, including customising the <title> tag, the meta description and the URL of a featured image:
These options get displayed immediately below the post/page writing area, and the user can enter text. This text, when the post is saved gets saved as custom fields. For example, on this post I’ll enter some text “Hello. This is a test.” in the “Image” field. The results are below:
I want to change my front page meta and title.please help me anyone, how can I manage by function hybrid_document_title();
Thanks so much for this tutorial!
Thanks for the tutorial. I tried to implement your code and seems to be working fine, only that I am getting two sets of custom fields: I get my own custom write panel with custom fields and the one that comes with WordPress. For some reason when I add a custom field in my custom write panel, it also automatically adds it into a default custom field panel, so I have two custom fields with the same name and content. Any ideas why and how to resolve this problem? I am using the latest version of WordPress.
Does anybody know how to loop this? Say I want to do a slider for pages with the meta box set to TRUE or something, and then I want to grab all pages or posts with that condition?
Hi, I’ve stumbled across your code and it is the perfect solution for what I need to do but when I try putting it in to my functions.php file it gives me the following error
Warning: Cannot modify header information – headers already sent by (output started at /usr/var/www/localhost/htdocs/oowebsite/wp-includes/plugin.php:395) in /usr/var/www/localhost/htdocs/oowebsite/wp-admin/theme-editor.php on line 89
I’m inserting it directly at the top of the functions file to call it as early as possible, am I putting it in the wrong place? Any feeback very much obliged.
i must say that simply gr8 tip 🙂
Hi, I really like the way this is done. Would it be recommended to insert a custom field with keywords as well or is this not needed anymore?
[…] that were as straight-forward as I was hoping there would be. The best tutorial I found was located here. The problem with this tutorial was that it didn’t spell it out clearly enough for me. It was […]
Thank you very much for this! I have changed it a bit to suit my needs by adding extra metaboxes. These all display fine but only the first set (the first 5) save! Of course there is something I need to change to make it save the extras but alas, I am just a tinkerer not a master… $meta_boxes = array( 'url' => array( 'name' => 'URL', 'title' => __('Video URL:', 'hybrid'), 'type' => 'text' ), 'title' => array( 'name' => 'Title', 'title' => __('Title:', 'hybrid'), 'type' => 'text' ), 'description' => array( 'name' => 'Description', 'title' => __('Description:', 'hybrid'), 'type' => 'text' ), 'preroll' => array( 'name' => 'Preroll', 'title' => __('Preroll?', 'hybrid'),'options' => array('','Yes'), 'type' => 'select' ), 'link' => array( 'name' => 'link', 'title' => __('link:', 'hybrid'), 'type' => 'tag' ), 'url1' => array( 'name' => 'URL1', 'title' => __('Video URL:', 'hybrid'), 'type' => 'text' ), 'title1' => array( 'name' => 'Title1', 'title' => __('Title:', 'hybrid'), 'type' => 'text' ), 'description1' => array( 'name' => 'Description1', 'title' => __('Description:', 'hybrid'), 'type' => 'text' ), 'preroll1' => array( 'name' => 'Preroll1', 'title' => __('Preroll?', 'hybrid'),'options' => array('','Yes'), 'type' => 'select' ), 'link1' => array( 'name' => 'link1', 'title'… Read more »
im hoping you can help me out with this one. i have had this code in my theme from theme hybrid for over a year now. and noticed no problems. now i have been developing a theme for entry into the wordpress theme directory.
after going through all the usually stuff i came to debug the theme. i keep getting an error saying this
“Notice: Undefined index: post_type in……”
and
“Notice: Undefined index: Title_noncename in ……”
now this issue is coming from the hybrid_save_meta_data function, heres the code for reference
does anyone have a solution to this?
i have a okay understanding of php but this is a real issue and i cant figure it out.
How to use select:
'password' => array(
'name' => 'password',
'title' => __('Password Protection'),
'options' => array(
'No',
'Yes'
),
'type' => 'select' )
thanks Chris but how you call an if statement here?
i need to make a show/hide dropdown,
if is show
//get php file
else
//do nothing
thanks a lot!
[…] Each offer is a post but we only want one line of text briefly describing the offer. We've built a custom meta box to allow an easy way of adding custom fields to posts. The custom field contains information about […]
Regarding only showing custom meta boxes when a certain page template is selected I have this solution which works great:
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file == 'attorneys.php') { // this will only show on pages that have been saved with the attorneys template
Just replace “attorneys.php” with the name of your template file,
Include the custom meta box code and close out the open brace.
thanks a lot for this help.
i try to use it with qtranslate with ( [:en] ) and ( ) tags but with no luck.
is a way to make it work with qtranslate tags also?
thanks a lot again!
philip
What
return apply_filters( ‘hybrid_post_meta_boxes’, $meta_boxes );
do ?
I’m interested why you use apply_filter instead of add_filter ?
Great post! Very helpful.
Quick question though…
Is there any way to make the meta box show only when a specific page template is selected?
Thanks for the reply! It’s funny you linked to that post. I’ve been following ThemeShaper tutorials in building my little WP framework over the last couple of months.
As far as making a meta box show on a Page Write admin page only when a specific page template is selected, I ended up just using javascript.
I figured any solution with PHP would not allow the user to see the new meta box until they actually saved the page and came back to editing it again (which wouldn’t make sense).
Yeah, that’s probably better. Do share!
Fantastic tutorial!
Just one question. I would like to put a brief description under each input field as a reminder to what each input does.
How would I do that?
Thanks,
Karl
Literally just add in a paragraph with what you want to say:
<p>This does so and so.</p>
Hello,
Do you know how to use the select option ?
I want to create a select meta boxe with a true/false choice.
Thx for your help.
Any ideas how to do this ?
Could give me an example ?
Thx for your help.
[…] the first post – WPShout – is actually a link to WPShout. How? Using Biblioteca’s custom write meta box thingys (I still don’t know the technical term!) I can create a new post which contains the link and […]
Hi Alex,
I love the tutorial, opens up many possibilities within the CMS.
However, when I copy and paste your code into my functions file it works a treat, but when I change the names of the meta boxes in the array they are not saved as custom fields. Everything shows up as it should on screen in the ‘write page’ area, but on save nothing happens.
Any ideas?
Here is an example of one line – ‘header paragraph’ => array( ‘name’ => ‘Header Paragraph’, ‘title’ => __(‘Header Paragraph’, ‘hybrid’), ‘type’ => ‘text’ ),
Many thanks,
Brian
I’ll have to have a look into this problem people are having – it’s not happening to me, that’s for sure! It certainly is a great way to offer custom fields.
If I’m thinking right then get_post_meta is the only way to pull a custom field, no?
A better option would be to use WordPress’ own WYSIWYG editor.
Fair point. Now you mention it, those other options do ring a bell 🙂
Calling JS within functions is easy – depending how you’re doing it you can either close out the php and call the JS as normal or echo it. Google those for a better explanation :).
[…] in the sidebar of our blog.HypothesisUse of custom fields to pull data from posts. Preferably custom write panels to spice it up a bit.Loop (preferably without replacing the loop already in use) to pull a random […]
[…] and it inherits all of Biblioteca’s advanced features – a nice theme options page, in post SEO options, threaded comments, a ton of widget ready area etc.The theme would look great on a personal blog or […]
[…] Create An In-Post (Theme) Options (Meta) Box in WordPress […]
I’m not aware of being able to add something like that straight into the image uploading panel (you could… *horror* hack the backend…), but another way to do it would be to auto grab the image – something like:
// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}
With that in the functions.php file and then grab the image with:
<img src="<?php echo catch_that_image() ?>" alt=""/>
Thanks for the heads up. Some idiot (erm..) copied the wrong code.
[…] Create An In-Post (Theme) Options (Meta) Box in WordPress […]
Wow! this is very nice tips for modify WordPress feature. Thank you so much!