Things I need to accomplish:
- Establish a "site variable" where we can set what category a blog belongs in. These categories are Option 1, Option 2, Option 3, and Option 4.
- When one of the categories is selected, it generates the breadcrumb on the blog’s home page and also the first half of the secondary breadcrumbs in archive or single pages.
- Make it so the blog home is automatically the second part of the breadcrumb link path.
Here are the screenshots that show the custom option called Site Information:
Writes to the wp_options database table
Here is the code so far:
class MySettingsPage
{
private $options;
function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Site Information',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}
function create_admin_page()
{
// Set class property
$this->options = get_option( 'site_information_options' );
?>
<div class="wrap">
<h1>Site Information</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'site-information_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
function page_init()
{
register_setting(
'site-information_group',
'site_information_options',
array( $this, 'sanitize' )
);
add_settings_section(
'setting_section_id',
'Custom Settings',
array( $this, 'print_section_info' ),
'my-setting-admin'
);
add_settings_field(
'parent_category',
'Parent Category',
array( $this, 'parent_category_callback' ),
'my-setting-admin',
'setting_section_id'
);
add_settings_field(
'subcategory',
'Subcategory',
array( $this, 'subcategory_callback' ),
'my-setting-admin',
'setting_section_id'
);
add_settings_field(
'blog_owner_email',
'Blog Owner Email',
array( $this, 'blog_owner_email_callback' ),
'my-setting-admin',
'setting_section_id'
);
}
function sanitize( $input )
{
$new_input = array();
if( isset( $input['parent_category'] ) )
$new_input['parent_category'] = sanitize_text_field( $input['parent_category'] );
if( isset( $input['subcategory'] ) )
$new_input['subcategory'] = sanitize_text_field( $input['subcategory'] );
if( isset( $input['blog_owner_email'] ) )
$new_input['blog_owner_email'] = sanitize_text_field( $input['blog_owner_email'] );
return $new_input;
}
function print_section_info()
{
print 'Enter your settings below:';
}
function parent_category_callback()
{
printf(
'<select id="parent_category" name="site_information_options[parent_category]" value="%s" >
<option>Select...</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>',
isset( $this->options['parent_category'] ) ? esc_attr( $this->options['parent_category']) : ''
);
}
function subcategory_callback()
{
printf(
'<select id="subcategory" name="site_information_options[subcategory]" value="%s" >
<option>Select...</option>
<option>Option 1</option>
<option>Option 2</option>
</select>',
isset( $this->options['subcategory'] ) ? esc_attr( $this->options['subcategory']) : ''
);
}
function blog_owner_email_callback()
{
printf(
'<input type="text" id="blog_owner_email" name="site_information_options[blog_owner_email]" value="%s" />',
isset( $this->options['blog_owner_email'] ) ? esc_attr( $this->options['blog_owner_email']) : ''
);
}
}
if( is_admin())
$my_settings_page = new MySettingsPage();