Skip to content

Instantly share code, notes, and snippets.

@Chrisedmo
Created January 29, 2016 21:19
Show Gist options
  • Save Chrisedmo/df0d9ab8e281c224a141 to your computer and use it in GitHub Desktop.
Save Chrisedmo/df0d9ab8e281c224a141 to your computer and use it in GitHub Desktop.
#WP: Global Options
/*===================================================================================
* Add global options
* =================================================================================*/
/**
*
* The page content surrounding the settings fields. Usually you use this to instruct non-techy people what to do.
*
*/
function theme_settings_page(){
?>
<div class="wrap">
<h1>Contact Info</h1>
<p>This information is used around the website, so changing these here will update them across the website.</p>
<form method="post" action="options.php">
<?php
settings_fields("section");
do_settings_sections("theme-options");
submit_button();
?>
</form>
</div>
<?php }
/**
*
* Next comes the settings fields to display. Use anything from inputs and textareas, to checkboxes multi-selects.
*
*/
// Phone
function display_support_phone_element(){ ?>
<input type="tel" name="support_phone" placeholder="Enter phone number" value="<?php echo get_option('support_phone'); ?>" size="35">
<?php }
// Fax
function display_support_fax_element(){ ?>
<input type="tel" name="support_fax" placeholder="Enter fax number" value="<?php echo get_option('support_fax'); ?>" size="35">
<?php }
// Email
function display_support_email_element(){ ?>
<input type="email" name="support_email" placeholder="Enter email address" value="<?php echo get_option('support_email'); ?>" size="35">
<?php }
/**
*
* Here you tell WP what to enqueue into the <form> area. You need:
*
* 1. add_settings_section
* 2. add_settings_field
* 3. register_setting
*
*/
function display_custom_info_fields(){
add_settings_section("section", "Company Information", null, "theme-options");
add_settings_field("support_phone", "Support Phone No.", "display_support_phone_element", "theme-options", "section");
add_settings_field("support_fax", "Support Fax No.", "display_support_fax_element", "theme-options", "section");
add_settings_field("support_email", "Support Email address", "display_support_email_element", "theme-options", "section");
register_setting("section", "support_phone");
register_setting("section", "support_fax");
register_setting("section", "support_email");
}
add_action("admin_init", "display_custom_info_fields");
/**
*
* Tie it all together by adding the settings page to wherever you like. For this example it will appear
* in Settings > Contact Info
*
*/
function add_custom_info_menu_item(){
add_options_page("Contact Info", "Contact Info", "manage_options", "contact-info", "theme_settings_page");
}
add_action("admin_menu", "add_custom_info_menu_item");
<!-- ==== Contact Information ==== -->
<?php
/**
* 1. First we can check if the support phone number is filled out.
* 2. If it is, let's show the phone number, otherwise we'll show a slightly famous message :).
* 3. Hit the pub to celebrate!
*/
if(get_option('support_phone')){ ?>
<!-- Phone number found, show the phone number as a link. -->
<p>Need support? Get us on <a href="tel:<?php echo get_option('support_phone'); ?>"><?php echo get_option('support_phone'); ?></a></p>
<?php } else { ?>
<!-- No phone number found, show fun message. -->
<p>Support silence is golden.</p>
<?php } ?>
<!-- /==== Contact Information ==== -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment