how to create custom input fields inside settings general page . in wordpresss?
functions.php
/*
* Add custom setting field to general settings page (Working hours section) start
*/
function my_custom_settings_init(){
register_setting('general','wrking_hours_main_text');
register_setting('general','moday_friday_hours');
register_setting('general','saturday_hours');
register_setting('general','sunday_holiday_hours');
// Add a section to the General Settings page
add_settings_section(
'working_hours_section',
'Working Hours',
'my_custom_section_callback',
'general'
);
// Add settings fields
add_settings_field(
'wrking_hours_main_text',
'Description Text',
'working_hours_description_callback',
'general',
'working_hours_section',
array('label_for' => 'wrking_hours_main_text')
);
add_settings_field(
'moday_friday_hours',
'Moday - Friday',
'custom_working_hours_field_callback',
'general',
'working_hours_section',
array('label_for' => 'moday_friday_hours')
);
add_settings_field(
'saturday_hours',
'Saturday Hours',
'custom_working_hours_field_callback',
'general',
'working_hours_section',
array('label_for'=>'saturday_hours')
);
add_settings_field(
'sunday_holiday_hours',
'Sunday and Holiday Hours',
'custom_working_hours_field_callback',
'general',
'working_hours_section',
array('label_for'=>'sunday_holiday_hours')
);
}
add_action('admin_init','my_custom_settings_init');
function my_custom_section_callback(){
echo '<p>Enter the working hours.</p>';
}
function working_hours_description_callback($args){
$option=get_option($args['label_for']);
echo '<input type="text" id="' . esc_attr($args['label_for']) . '" name="' . esc_attr($args['label_for']) . '" value="' . esc_attr($option) . '" class="regular-text" style="width:50em" />';
}
function custom_working_hours_field_callback($args){
$option=get_option($args['label_for']);
echo '<input type="text" id="' . esc_attr($args['label_for']) . '" name="' . esc_attr($args['label_for']) . '" value="' . esc_attr($option) . '" />';
}
// Add custom setting field to general settings page (Working hours section)End
and this is how you can access those custom fields inside header or footer
<div class="working-hours">
<?php echo esc_html(get_option('wrking_hours_main_text')); ?>
<br><br> Monday - Friday: <span class="text-right"><?php echo esc_html(get_option('moday_friday_hours')); ?> </span>
<br> Saturday: <span class="text-right"><?php echo esc_html(get_option('saturday_hours')) ?></span>
<br> Sunday and holidays: <span class="text-right"><?php echo esc_html(get_option('sunday_holiday_hours')); ?></span>
</div>
Comments
Post a Comment