Every WordPress developer that does custom themeing runs into the times when you have to have a spot to enter some kind of variable text…but you don’t want to call a single page or post just to get this little section of data.
It’s tempting just to put it directly into the code and tell the customer they have to call you to change it, but that method is lazy or worse… inept. Not to mention it will probably bite you in the end. The client will call needing some text edit at the worst time and you will only wind up resenting the call and the task.
So… here is a QUICK way to give yourself a little “whatever” area for custom theme options. This options page will appear in your WordPress admin panel.
Just paste this into your functions.php file and your set with a backend page that lets you create and update any custom options you can dream up. I left it skinny and very generic for easy editing. If you would like a more detailed explanation of each snippet just leave a comment below.
function custom_options_menu() { add_menu_page('Custom Options', 'Custom Options', 'manage_options', 'custom_options','top_level_display'); } function top_level_display() { //844AM if(isset($_POST['has_submitted_custom_options'])) { //117PM $first_custom_option = $_POST['first_custom_option']; $all_custom_options['first_custom_option'] = $first_custom_option; update_option( 'all_custom_options', $all_custom_options ); }//117PM $all_custom_options = get_option( 'all_custom_options' ); ?> <style> </style> <h1>Custom Options</h1> <form method="post"> <input type="text" name="first_custom_option" value="<? echo stripslashes($all_custom_options['first_custom_option']); ?>" /> <input type="submit" value="Submit" /> <input type="hidden" name="has_submitted_custom_options" id="custom_options_submitted" value="true" /> </form> <? } //844AM add_action('admin_menu', 'custom_options_menu');
Then just pull it down anywhere in your template files.
<? $all_custom_options = get_option( 'all_custom_options' ); ?> <div><? echo $all_custom_options['first_custom_option']; ?></div>