Skip to content

Commit

Permalink
Move options set up to Plugin class
Browse files Browse the repository at this point in the history
  • Loading branch information
SebKay committed Feb 5, 2024
1 parent 21b56ef commit 31f8034
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 106 deletions.
67 changes: 0 additions & 67 deletions inc/options-content.php

This file was deleted.

101 changes: 65 additions & 36 deletions inc/options.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
<?php

defined('ABSPATH') or exit;

/**
* Create options page
*/
function wpt_create_options_page()
{
if (! current_user_can('administrator')) {
return;
}

add_submenu_page(
'options-general.php',
WPT_PLUGIN_NAME,
WPT_PLUGIN_NAME,
'manage_options',
'wpt-settings',
fn () => include WPT_DIR_PATH.'/inc/options-content.php'
);
}

add_action('admin_menu', 'wpt_create_options_page');

/**
* Register custom options
*/
function wpt_register_options()
{
register_setting('wpt-options', 'wpt_text_option');
register_setting('wpt-options', 'wpt_radio_option');
register_setting('wpt-options', 'wpt_select_option');
}

add_action('admin_init', 'wpt_register_options');
<?php defined('ABSPATH') or exit; ?>

<div class="wrap">
<h1>
<?php echo WPT_PLUGIN_NAME; ?>
</h1>

<form method="post" action="options.php">
<?php settings_fields('wpt-options'); ?>
<?php do_settings_sections('wpt-options'); ?>

<h2 class="title">
<?php _e('API', 'wp-plugin-template'); ?>
</h2>

<table class="form-table">
<tr valign="top">
<th scope="row">
<?php _e('Text', 'wp-plugin-template'); ?>
</th>
<td>
<input type="text" name="wpt_text_option" value="<?php echo esc_attr(get_option('wpt_text_option')); ?>">
</td>
</tr>

<tr valign="top">
<th scope="row">
<?php _e('Radio', 'wp-plugin-template'); ?>
</th>
<td>
<label>
<input type="radio" name="wpt_radio_option" value="0" <?php checked(0, esc_attr(get_option('wpt_radio_option', 0))); ?>>
<span>
No
</span>
</label>
<br><br>
<label>
<input type="radio" name="wpt_radio_option" value="1" <?php checked(1, esc_attr(get_option('wpt_radio_option', 0))); ?>>
<span>
Yes
</span>
</label>
</td>
</tr>

<tr valign="top">
<th scope="row">
<?php _e('Select', 'wp-plugin-template'); ?>
</th>
<td>
<select name="wpt_select_option">
<option value="1" <?php selected(get_option('wpt_select_option'), 1); ?>>1</option>
<option value="10" <?php selected(get_option('wpt_select_option'), 10); ?>>10</option>
<option value="25" <?php selected(get_option('wpt_select_option'), 25); ?>>25</option>
</select>
</td>
</tr>
</table>

<hr>

<?php submit_button(); ?>
</form>
</div>
28 changes: 25 additions & 3 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,32 @@ public function logger(): Logger
return $this->logger ??= new Logger();
}

public function run(): void
public function options(): void
{
add_action('plugins_loaded', function () {
require_once WPT_DIR_PATH.'/inc/options.php';
\add_action('admin_menu', function () {
if (! \current_user_can('administrator')) {
return;
}

\add_submenu_page(
'options-general.php',
\WPT_PLUGIN_NAME,
\WPT_PLUGIN_NAME,
'manage_options',
'wpt-settings',
fn () => include \WPT_DIR_PATH.'/inc/options.php'
);
}, 10);

\add_action('admin_init', function () {
\register_setting('wpt-options', 'wpt_text_option');
\register_setting('wpt-options', 'wpt_radio_option');
\register_setting('wpt-options', 'wpt_select_option');
}, 10);
}

public function run(): void
{
\add_action('plugins_loaded', [$this, 'options'], 10);
}
}

0 comments on commit 31f8034

Please sign in to comment.