Skip to content

Instantly share code, notes, and snippets.

@5ally
Created Jun 30, 2020
Embed
What would you like to do?
<?php
add_action( 'admin_menu', 'my_plugin_add_admin_menu' );
function my_plugin_add_admin_menu() {
// Add a settings page under the Settings menu. I.e. It's accessible at Settings -> Test
add_options_page( 'Test', 'Test', 'manage_options', 'theme_amna', 'amna_theme_options_page' );
}
add_action( 'admin_init', 'my_plugin_init_settings' );
function my_plugin_init_settings() {
// For testing purposes, I used 'test' as the database option name.
register_setting( 'amna-fields-group', 'test' );
// Add a settings section.
add_settings_section( 'my-plugin-general-section', 'General', 'my_general_settings_section', 'theme_amna' );
// Add a settings field in the General section.
add_settings_field( 'my_field', 'My Field', 'my_field_settings_field', 'theme_amna', 'my-plugin-general-section', [
'label_for' => 'my-plugin-my_field',
] );
}
function my_general_settings_section( $section ) {
echo '<p>Add some info about the section, or use __return_false as the callback to not echo any section info.</p>';
}
// Simple callback for the My Field setting.
function my_field_settings_field( $args ) {
// Get the database option.
$options = (array) get_option( 'test' );
printf( '<input name="test[my_field]" value="%s" id="my-plugin-my_field" class="widefat" />',
esc_attr( $options['my_field'] ?? '' ) );
}
add_action( 'admin_enqueue_scripts', 'my_plugin_load_styles' );
function my_plugin_load_styles( $hook_suffix ) {
if ( $hook_suffix === get_plugin_page_hook( 'theme_amna', '' ) ) {
wp_enqueue_style( 'theme-options-css',
get_template_directory_uri() . '/inc/admin/assets/css/theme-options-css.css' );
}
}
function amna_theme_options_page() {
settings_errors( 'theme_amna' );
?>
<div class="wrap flex-tab">
<form action="options.php" method="post" class="amna-fields">
<?php
settings_fields( 'amna-fields-group' );
amna_do_settings_sections('theme_amna');
submit_button();
?>
</form>
</div>
<?php
}
// Note: I copied the code in wp-admin/includes/template.php and modified it
// based on your code.
function amna_do_settings_sections( $page ) {
global $wp_settings_sections, $wp_settings_fields;
if ( ! isset( $wp_settings_sections[ $page ] ) ) {
return;
}
foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
if ( $section['title'] ) {
echo "<h3>{$section['title']}</h3>\n";
}
if ( $section['callback'] ) {
call_user_func( $section['callback'], $section );
}
if ( ! isset( $wp_settings_fields ) ||
! isset( $wp_settings_fields[ $page ] ) ||
! isset( $wp_settings_fields[ $page ][ $section['id'] ] )
) {
continue;
}
echo '<div class="settings-form-wrapper">';
amna_do_settings_fields( $page, $section['id'] );
echo '</div>';
}
}
// Note: I copied the code in wp-admin/includes/template.php and modified it
// based on your code.
function amna_do_settings_fields( $page, $section ) {
global $wp_settings_fields;
if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) {
return;
}
foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) {
echo '<div class="settings-form-row flex-tab">';
echo '<p>';
if ( ! empty( $field['args']['label_for'] ) ) {
echo '<label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label>';
} else {
echo $field['title'];
}
call_user_func( $field['callback'], $field['args'] );
echo '</p>';
echo '</div>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment