Административная часть
Настройка административной части
Добавление функций
admin/class-my-plugin-admin.php
/**
* Регистрируем административное меню этого плагина в...
*/
public function add_plugin_admin_menu() {
/**
* Добавляем пункт меню плагина в "настройки".
*/
add_options_page( $this->plugin_name, $this->plugin_name, 'manage_options', $this->plugin_name, array($this, 'display_plugin_setup_page')
);
/**
* Добавляем пункт (страницу) верхнего уровня в меню админ-панели (боковая панель)
*/
add_menu_page("Система записи", "Система записи", "manage_options", $this->plugin_name, array($this, 'display_plugin_setup_page'), "dashicons-calendar-alt");
}
}
/**
* Добавляет путь к шаблону с html кодом формы админки
*/
public function display_plugin_setup_page() {
include_once( 'partials/appointment-dk-admin-display.php' );
}
/**
* Добавляем ссылку на странице "Плагины"
*/
public function add_action_links( $links ) {
$settings_link = array(
'<a href="' . admin_url( 'options-general.php?page=' . $this->plugin_name ) . '">' . __('Settings', $this->plugin_name) . '</a>',
);
return array_merge( $settings_link, $links );
}
// Пример валидации формы
public function validate($input) {
$valid = array();
$valid['footer_text'] = (isset($input['footer_text']) && !empty($input['footer_text'])) ? $input['footer_text'] : '';
return $valid;
}
// Пример обновления данных настроек (требуется регистрация)
public function options_update() {
register_setting($this->plugin_name, $this->plugin_name, array($this, 'validate'));
}
Регистрация функций
Файл includes/class-my-plugin.php
Функция define_admin_hooks()
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' );
// Регистрируем новые ссылки во вкладке "Плагины"
$plugin_basename = plugin_basename( plugin_dir_path( __DIR__ ) . $this->plugin_name . '.php' );
$this->loader->add_filter( 'plugin_action_links_' . $plugin_basename, $plugin_admin, 'add_action_links' );
$this->loader->add_action('admin_init', $plugin_admin, 'options_update');
Страница администрирования
admin/partials/my-plugin-admin-display.php
<--Пример->
<form method="post" name="my_options" action="options.php">
<?php
// Загрузить все значения элементов формы
$options = get_option($this->plugin_name);
// текущие состояние опций
$footer_text = $options['footer_text'];
// Выводит скрытые поля формы на странице настроек
settings_fields( $this->plugin_name );
do_settings_sections( $this->plugin_name );
?>
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
<fieldset>
<legend class="screen-reader-text"><span><?php _e('Text in the footer', $this->plugin_name);?></span></legend>
<label for="<?php echo $this->plugin_name;?>-footer_text">
<span><?php esc_attr_e('Text in the footer', $this->plugin_name);?></span>
</label>
<input type="text"
class="regular-text" id="<?php echo $this->plugin_name;?>-footer_text"
name="<?php echo $this->plugin_name;?>[footer_text]"
value="<?php if(!empty($footer_text)) esc_attr_e($footer_text, $this->plugin_name);?>"
placeholder="<?php esc_attr_e('Text in the footer', $this->plugin_name);?>"
/>
</fieldset>
<?php submit_button(__('Save all changes', $this->plugin_name), 'primary','submit', TRUE); ?>
</form>
Last updated
Was this helpful?