Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Last active July 25, 2018 15:37
Show Gist options
  • Save betweenbrain/5281ed4585664619d1026c5157ba68e2 to your computer and use it in GitHub Desktop.
Save betweenbrain/5281ed4585664619d1026c5157ba68e2 to your computer and use it in GitHub Desktop.
Sample WP Plugin that supports admin options
<?php
/**
* Plugin Name: AAA Test Plugin
* Description: A sample plugin that supports admin options.
* Author: Matt Thomas
* Version: 0.0.1
*
* @package WordPress
*/
/**
* Add link to this plugin's settings page
*
* @param [type] $links
* @return void
*/
$plugin = plugin_basename( __FILE__ );
add_filter(
"plugin_action_links_$plugin", function( $links ) {
$settings_link = '<a href="options-general.php?page=aaa-test-plugin">' . __( 'Settings' ) . '</a>';
array_push( $links, $settings_link );
return $links;
}
);
/**
* Callback that adds options to the settings page.
*
* @return void
*/
function plugin_options_page() {
?>
<div>
<h2>My custom plugin</h2>
Options relating to the Custom Plugin.
<form action="options.php" method="post">
<?php settings_fields( 'plugin_options' ); ?>
<?php do_settings_sections( 'plugin' ); ?>
<input name="Submit" type="submit" value="<?php esc_attr_e( 'Save Changes' ); ?>" />
</form></div>
<?php
}
/**
* Adds sub-menu to settings admin menu item
*
* @return void
*/
add_action(
'admin_menu', function () {
add_options_page(
$menu_title = 'Custom Plugin Menu', // Link text under settings.
$page_title = 'Custom Plugin Page', // Plugin page title.
$capability = 'manage_options', // User must have this capability.
$menu_slug = 'aaa-test-plugin', // Plugin slug (unique alias).
$function = 'plugin_options_page' // Function that generates plugin options.
);
}
);
/**
* Outputs text for the main settings
*
* @return void
*/
function plugin_section_text() {
echo 'Foo bar baz qux!';
}
/**
* Outputs the setting field.
*
* @return void
*/
function plugin_setting_string() {
$options = get_option( 'plugin_options' );
echo "<input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
}
add_action(
'admin_init', function () {
register_setting(
'plugin_options', // Group ID, same as settings_fields() value.
'plugin_options', // Options name.
'plugin_options_validate' // Function callnack to validate options.
);
add_settings_section(
'plugin_main', // Section unique ID.
'Main Settings', // Rendered section title.
'plugin_section_text', // Function callback to output section text.
'plugin' // Page name, must match do_settings_sections() value.
);
add_settings_field(
'plugin_text_string', // Field unique ID.
'Plugin Text Input', // Field title.
'plugin_setting_string', // Function callback to output the field.
'plugin', // Page name this is attached to (same as do_settings()).
'plugin_main' // ID of the settings section this is part of (same as add_settings_section() first argument).
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment