Home / WP Add new settings page for plugins/theme

WP Add new settings page for plugins/theme

<?php
// Add settings link on plugin listing page
function my_plugin_settings_link($links) { 
	$settings_link = '<a href="themes.php?page=invoice_settings">Settings</a>'; 
	array_unshift($links, $settings_link);
	return $links; 
}
 
$plugin = plugin_basename(__FILE__); 
add_filter("plugin_action_links_$plugin", 'my_plugin_settings_link' );
// End of Add settings link on plugin listing page

$run_now = new prism_invoice_settings();
class prism_invoice_settings{
	function __construct() {
		add_action('admin_menu', [$this, 'create_menu']);
	}

	private $settings_name = 'Invoice settings';
	private $settings_url = 'invoice_settings';

	// create custom plugin settings menu
	function create_menu() {

		//create new top-level menu
		add_submenu_page('themes.php', $this->settings_name, $this->settings_name, 'administrator', $this->settings_url, [$this, 'settings_page']);

		//call register settings function
		add_action( 'admin_init', [$this, 'plugin_settings'] );
	}


	function plugin_settings() {
		//register our settings
		register_setting( 'my-cool-plugin-settings-group', 'new_option_name' );
		register_setting( 'my-cool-plugin-settings-group', 'some_other_option' );
		register_setting( 'my-cool-plugin-settings-group', 'option_etc' );
	}

	function settings_page() {
		$return = get_option('ResellStorePricing');
		if (isset($_POST['UpdateSett'])) {
			extract($_POST);
			if ($Settings) {
				foreach ($Settings as $option_name=>$option_value) {
					if ( get_option( $option_name ) !== false ) {
						update_option( $option_name, $option_value );
					} else {
						$deprecated = null;
						$autoload = 'no';
						add_option( $option_name, $option_value, $deprecated, $autoload );
					}
				}
			}


			// Set Terms & conditions page
			$option_name = 'site_HeaderColor' ;
			if ( get_option( $option_name ) !== false ) {
				update_option( $option_name, $site_HeaderColor );
			} else {
				$deprecated = null;
				$autoload = 'no';
				add_option( $option_name, $site_HeaderColor, $deprecated, $autoload );
			}

		}
		?>
		<div class="wrap">
			<div id="icon-themes" class="icon32"></div>
			<?php settings_errors(); ?>
			
		</div><!-- /.wrap -->
		<?php
		$this->settings_basic();
	}


	function settings_basic(){ ?>
		<div class="wrap">
			<h2><?php echo $this->settings_name; ?></h2>
			<form method="POST" action="">
				<?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
				<?php do_settings_sections( 'my-cool-plugin-settings-group' ); ?>
				<input type="hidden" name="UpdateSett" value="">
				<table class="form-table">
					<tr valign="top">
						<th scope="row">Para Name</th>
						<td>
							<input type="url" name="Settings[HookUrlAS]" id="" value="<?php echo esc_attr(get_option('HookUrlAS')); ?>">
						</td>
					</tr>
					<!-- <tr> <td colspan="2"><hr></td> </tr> -->

					<tr valign="top">
						<th scope="row"></th>
						<td>
							<?php submit_button(); ?>
						</td>
					</tr>
				</table>
			</form>
		</div>
	<?php }
}

?>

all rights reserved