I have a site with a custom home page, home.php. What’s the right way to present the editable home page options in the admin tool?
For example, there’s a few custom text fields on the home page that should be editable in the admin tool. I’m looking for the best way to set this up so an admin user can change these options.
UPDATE: imagine a site with a home page with a promotional blurb (text string) that I need to be able to let admin users (non-tech people) update easily. I’m trying to figure out how to present a screen in admin that says “What promotional text do you want on the home page? [______________]”? I thought about doing this via widget but that route is clunky and requires me to create a special sidebar just to hold the single widget. What’s the best way to approach this?
There are many solutions to this problem. And every one of them is correct, I guess. It depends on what you really want and what will be most friendly/clear to user/administrator of this page.
I usually use 3 ways to solve this. Which one of them I chose? It depends on situation. Sometimes I use 2 of them simultaneously.
1. Static front page solution.
When I can set static page as front page, I do it. Then create
front-page.php
page template and use it to define how home-page/front-page should look like.To display/allow to edit some custom fields on this page, I use Advanced Custom Fields plugin. It allows you to define custom fields set for front-page (and much more). Then I use
front-page.php
template to show these custom fields values, where they should be shown.If some of these values should be repeated on every page (in footer, in header, etc.) I get these values from Home Page like this:
It’s very easy and clean solution, when there are not many fields that should be repeated on other pages. It’s user friendly to, I guess. If you want to edit home page, you just have to go and do it – you can edit everything in home page editor.
2. Universal solution.
The other solution I use is to define some theme options. It is much better when you want to define look of page (logo image, backgrounds, and so on), and not only the content (especially if you mean the content of only one page).
I usually use OptionTree plugin to do this. But it’s not so hard to do this by yourself.
I don’t really like the idea, that you should edit content of the page in theme options, so I don’t use this solution very often.
3. When this content is always repeated on other pages
Sometimes you have to show some values on every page (or most of them) and not only on home page. Then the “nicest” solution, I think, is to define these areas as sidebars and allow user to edit them using widgets.
If this repeating content is just one line (copyright info line in footer, etc.) and it won’t be changed to often, I probably would use solution 1. But if it’s more complex or it will be changed often, this solution is best, I guess.
I’d suggest creating a metabox for the home.php page template. This is easily done with the Advanced Custom Fields (ACF) plugin. You can even use “ACF Lite” if you’d like to build these into your theme, instead of using the plugin. Here’s quick and easy way to implement exactly what you’re looking for.
I was able to build the metabox, and create a custom styled output into the template file in less than 5 minutes. Here’s the code I used, and the step by step process.
Add the code block Labeled “functions.php” to your active theme’s “function.php” file.
Go to the home page (this page must be using the home.php template file Edit Page->Attributes->Template dropdown).
Go to your “home.php” template file and add the following code to display the content of the custom metabox which in this case is given the “name” of ‘promo_text’.
Note: get_field is a function from the ACF plugin. If you’d prefer to use WP built-in function, you can use: “get_post_meta($post_id, $key, $single);” instead. Here’s an example of that.
Here’s what it looks like when editing the home page:
Larger Image link
Now you’ll see the metabox. Note you can easily do this without the plugin, you just have to include the plugin folder in your theme folder, and add an include() to your active theme’s functions.php file.
I highly recommend this plugin… it very intuitive, and has lots of documentation. If you use this plugin, and var_dump(), you can figure everything out very quickly.
I hope this helps!
It is hard to tell from your question exactly what you are doing but if you are using the Options API, then
get_option
accepts a second parameter for the default values. From the Codex:If you are using the Theme Customizations API, then
get_theme_mod
, likeget_option
, also accepts a second parameter for your default values.With either option, you will need to create the backend interface. If you use the Options API, then you create a configuration page
add_menu_page
and/oradd_submenu_page
, or one of these related functions.If you use the Theme Customizer, and I am not sure if that fits your project, there is a whole different set of functions and techniques.
If I understand your question correctly you want to create a theme options panel so a WordPress admin. can enter (for example) ‘hello, world!’ in a text box, hit Save Changes and on the front end of the website ‘hello, world!’ will be displayed wherever you decided to call it in your template.
You can do this using the Options API like s_ha_dum was talking about but my guess is if you thought his answer did not contain enough detail what you really probably mean is that it is more complicated than you were hoping for.
So my advice to you would be to install the Option Tree plugin which will allow you to quickly and easily create a theme options panel (under the Appearance menu) with a text field that says “What promotional text do you want on the home page?”. Then all you have to do is call the value of that option in your home.php template using the Option Tree function.
Hope this helps!
If I was in your place, I would have added the custom word to the theme’s admin/config page. This would be easier for me because:
The way ahead would be to add a new hook.
You can either go with my preferred way here or the general plugin hook way detailed here
The way I would approach it (and I have something similar right now), is the following:
Create a new menu entry either in the settings menu (settings -> home page settings), or create a new homepage menu item, under Comments for example, or at the endo of your menu, or where ever you like. Then in that new menu item, create your page.
In the page mentioned in step 1, you create all your admin settings for home page (current and future settins).
Make sure the page (and its menu links) are only available to admin users via a role check
If you like, you can make your solution above as part of a plugin that specifically creates the extra settings page, and move it away from the theme. Because this is not a theme-related problem, but custimization of the admin area.
For me, as I mentioned earlier, I am doing someting along those lines, and created a plugin. This helps me in the future, if I want to change my theme, I do not need to go over what needs to be removed and kept in my code.
Hope this helps.