Skip to content

Instantly share code, notes, and snippets.

@coulterpeterson
Last active June 24, 2021 16:43
Show Gist options
  • Save coulterpeterson/fee0d22bd83bb0e5616888cb412e2ed0 to your computer and use it in GitHub Desktop.
Save coulterpeterson/fee0d22bd83bb0e5616888cb412e2ed0 to your computer and use it in GitHub Desktop.
#WordPress #Plugin options page
<?php
// .... various plugin code, then the options page gets added:
// Add settings link to plugins page listing
add_action('admin_menu', 'myplugin_register_options_page');
add_action('admin_init', 'myplugin_register_settings');
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'myplugin_add_settings_link');
// Settings link on plugin page
function myplugin_add_settings_link($links)
{
$settings_link = '<a href="options-general.php?page=my-plugin">' . __('Settings') . '</a>';
array_push($links, $settings_link);
return $links;
}
// Register Settings For a Plugin so they are grouped together
function myplugin_register_settings()
{
add_option('myplugin_option_name', '');
register_setting('myplugin_options_group', 'myplugin_option_name', null);
}
// Create an options page
function myplugin_register_options_page()
{
add_options_page('My Plugin Settings', 'MyPlugin Settings', 'manage_options', 'my-plugin', 'myplugin_options_page');
}
// Display Settings on Option’s Page
function myplugin_options_page()
{
?>
<div>
<h2>My Plugin Settings</h2>
<form method="post" action="options.php">
<?php settings_fields('myplugin_options_group'); ?>
<h4>Description of option</h4>
<input type="text" id="myplugin_option_name" name="myplugin_option_name" value="<?php echo get_option('myplugin_option_name'); ?>" style="width:40%;" />
<?php submit_button(); ?>
</form>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment