Useful WordPress Functions: Global Options

/*===================================================================================
* Add global options

Courtesy of Raelene Morey
https://gist.github.com/raewrites/bb060a35594d546464d2

Use get_option() function to display info ie: echo get_option(company_phone)
* =================================================================================*/

/**
 *
 * 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.
 *
 */

 // Business Name
function display_company_name_element(){ ?>
	
	<input type="text" name="company_name" placeholder="Official Company Name" value="<?php echo get_option('company_name'); ?>" size="35">

<?php }

 // Business Tagline
function display_company_tagline_element(){ ?>
	
	<input type="text" name="company_tagline" placeholder="Company Tagline" value="<?php echo get_option('company_tagline'); ?>" size="135">

<?php }

// Address
function display_company_address_element(){ ?>
	
	<input type="text" name="company_address" placeholder="Enter company address" value="<?php echo get_option('company_address'); ?>" size="135">

<?php }


// Phone
function display_company_phone_element(){ ?>
	
	<input type="tel" name="company_phone" placeholder="Enter phone number" value="<?php echo get_option('company_phone'); ?>" size="35">

<?php }

// Fax
function display_company_fax_element(){ ?>
	
	<input type="tel" name="company_fax" placeholder="Enter fax number" value="<?php echo get_option('company_fax'); ?>" size="35">

<?php }

// Email
function display_company_email_element(){ ?>
	
	<input type="email" name="company_email" placeholder="Enter email address" value="<?php echo get_option('company_email'); ?>" size="35">
	
<?php }

// Elevate Platform URL
function display_company_login_element(){ ?>
	
	<input type="url" name="company_login" placeholder="Paste Elevate Pltform login URL address" value="<?php echo get_option('company_login'); ?>" size="35">
	
<?php }

// Facebook
function display_company_facebook_element(){ ?>
	
	<input type="url" name="company_facebook" placeholder="Paste Facebook URL address" value="<?php echo get_option('company_facebook'); ?>" size="35">
	
<?php }

// twitter
function display_company_twitter_element(){ ?>
	
	<input type="url" name="company_twitter" placeholder="Paste twitter URL address" value="<?php echo get_option('company_twitter'); ?>" size="35">
	
<?php }

// LinkedIn
function display_company_linkedin_element(){ ?>
	
	<input type="url" name="company_linkedin" placeholder="Paste LinkedIn URL address" value="<?php echo get_option('company_linkedin'); ?>" size="35">
	
<?php }

// Google+
function display_company_google_element(){ ?>
	
	<input type="url" name="company_google" placeholder="Paste Google+ URL address" value="<?php echo get_option('company_google'); ?>" 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("company_name", "Official Company Name.", "display_company_name_element", "theme-options", "section");
	add_settings_field("company_tagline", "Official Company Tagline.", "display_company_tagline_element", "theme-options", "section");
	add_settings_field("company_address", "Company Address", "display_company_address_element", "theme-options", "section");
	add_settings_field("company_phone", "Company Phone No.", "display_company_phone_element", "theme-options", "section");
	add_settings_field("company_fax", "Company Fax No.", "display_company_fax_element", "theme-options", "section");
    add_settings_field("company_email", "Company Email address", "display_company_email_element", "theme-options", "section");
    add_settings_field("company_login", "Company Elevate Platfom login URL", "display_company_login_element", "theme-options", "section");
	add_settings_field("company_facebook", "Company Facebook URL", "display_company_facebook_element", "theme-options", "section");
    add_settings_field("company_twitter", "Company Twitter URL", "display_company_twitter_element", "theme-options", "section");
    add_settings_field("company_linkedin", "Company LinkedIn URL", "display_company_linkedin_element", "theme-options", "section");
    add_settings_field("company_google", "Company Google+ URL", "display_company_google_element", "theme-options", "section");

    register_setting("section", "company_name");
    register_setting("section", "company_tagline");
	register_setting("section", "company_address");
	register_setting("section", "company_phone");
	register_setting("section", "company_fax");
	register_setting("section", "company_email");
	register_setting("section", "company_login");
    register_setting("section", "company_facebook");
    register_setting("section", "company_twitter");
    register_setting("section", "company_linkedin");
    register_setting("section", "company_google");
	
}

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_menu_page("Contact Info", "Contact Info", "edit_pages", "contact-info", "theme_settings_page", null, 99);
	
}

add_action("admin_menu", "add_custom_info_menu_item");


// Add editor capability to save options for this particular menu item
add_filter( 'option_page_capability_section', 'bc_settings_permissions', 10, 1 );
function bc_settings_permissions( $capability ) {
    return 'edit_pages';
}


// Function to show the social media settings

function social_media_footerlinks($platform) {
    if ( get_option('company_' . $platform)) {
        echo "<div class='" . $platform . "'><a href='" . get_option('company_' . $platform) . "' target='_blank'><span class='fa fa-" . $platform . "'></span></a></div>";
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *