WordPress - Creating our plugin settings page with inputs

<div class="wrap"> 
<h2>MarketoForms Settings</h2>
<?php settings_errors(); ?>
<form method="POST" action="options.php">
<?php
settings_fields( 'marketoforms_settings' );
do_settings_sections( 'marketoforms_settings' );
?>
<?php submit_button(); ?>
</form>
</div>
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
add_action('admin_menu', array($this, 'addAdminMenuItems'), 9);
add_action('admin_init', array( $this, 'registerAndBuildFields' ));
}
// Register a new section in the "marketoforms_settings" page.
// add_settings_section( string $id, string $title, callable $callback, string $page )
add_settings_section(
'marketoforms_connection_settings',
__( 'In here we set the connection settings.', $this->plugin_name ), array($this, 'settingsCallback'),
'marketoforms_settings'
);
// Register a new field in the "marketoforms_connection_settings" section, inside the "marketoforms_settings" page.
unset($args);
$args = array (
'type' => 'input',
'subtype' => 'text',
'id' => 'marketoforms_rest_api_base',
'name' => 'marketoforms_rest_api_base',
'required' => 'true',
'get_options_list' => '',
'value_type'=>'normal',
'wp_data' => 'option',
);
add_settings_field(
'marketoforms_rest_api_base',
__('REST API base URL', $this->plugin_name),
array( $this, 'renderField' ),
'marketoforms_settings',
'marketoforms_connection_settings',
$args
);
register_setting(
'marketoforms_settings',
'marketoforms_rest_api_base',
array(
'type' => 'string',
'description' => 'URL needed for connecting to Marketo',
//'sanitize_callback' => array($this, 'calidateRestApiBase'),
//'show_in_rest' => false,
'default' => '',
)
);
public function registerAndBuildFields() {// Register a new section in the "marketoforms_settings" page.
add_settings_section(
'marketoforms_connection_settings',
__( 'In here we set the connection settings.', $this->plugin_name ), array($this, 'settingsCallback'),
'marketoforms_settings'
);

// Register a new field in the "marketoforms_connection_settings" section, inside the "marketoforms_settings" page.
unset($args);
$args = array (
'type' => 'input',
'subtype' => 'text',
'id' => 'marketoforms_rest_api_base',
'name' => 'marketoforms_rest_api_base',
'required' => 'true',
'get_options_list' => '',
'value_type'=>'normal',
'wp_data' => 'option',
);

add_settings_field(
'marketoforms_rest_api_base',
__('REST API base URL', $this->plugin_name),
array( $this, 'renderField' ),
'marketoforms_settings',
'marketoforms_connection_settings',
$args
);
register_setting(
'marketoforms_settings',
'marketoforms_rest_api_base',
array(
'type' => 'string',
'description' => 'URL needed for connecting to Marketo',
'sanitize_callback' => array($this, 'validateUrl'),
//'show_in_rest' => false,
'default' => '',
)
);
}
public function renderField( $args ) {
if( $args['wp_data'] == 'option' ) {
$wp_data_value = get_option( $args['name'] );
} elseif( $args['wp_data'] == 'post_meta' ) {
$wp_data_value = get_post_meta($args['post_id'], $args['name'], true );
}
switch ( $args['type'] ) {
case 'input':
$value = ( $args['value_type'] == 'serialized' ) ? serialize($wp_data_value) : $wp_data_value;
if( $args['subtype'] != 'checkbox' ) {
$prependStart = (isset($args['prepend_value'])) ? '<div class="input-prepend"> <span class="add-on">'.$args['prepend_value'].'</span>' : '';
$prependEnd = (isset($args['prepend_value'])) ? '</div>' : '';
$step = (isset($args['step'])) ? 'step="'.$args['step'].'"' : '';
$min = (isset($args['min'])) ? 'min="'.$args['min'].'"' : '';
$max = (isset($args['max'])) ? 'max="'.$args['max'].'"' : '';
if( isset( $args['disabled'] ) ) {
// hide the actual input bc if it was just a disabled input the informaiton saved in the database would be wrong - bc it would pass empty values and wipe the actual information
echo $prependStart.'<input type="'.$args['subtype'].'" id="'.$args['id'].'_disabled" '.$step.' '.$max.' '.$min.' name="'.$args['name'].'_disabled" size="40" disabled value="' . esc_attr($value) . '" /><input type="hidden" id="'.$args['id'].'" '.$step.' '.$max.' '.$min.' name="'.$args['name'].'" size="40" value="' . esc_attr($value) . '" />'.$prependEnd;
} else {
echo $prependStart.'<input type="'.$args['subtype'].'" id="'.$args['id'].'" required="'.$args['required'].'" '.$step.' '.$max.' '.$min.' name="'.$args['name'].'" size="40" value="' . esc_attr($value) . '" />'.$prependEnd;
}
/*<input required="required" '.$disabled.' type="number" step="any" id="'.$this->plugin_name.'_cost2" name="'.$this->plugin_name.'_cost2" value="' . esc_attr( $cost ) . '" size="25" /><input type="hidden" id="'.$this->plugin_name.'_cost" step="any" name="'.$this->plugin_name.'_cost" value="' . esc_attr( $cost ) . '" />*/
} else {
$checked = ($value) ? 'checked' : '';
echo '<input type="'.$args['subtype'].'" id="'.$args['id'].'" required="'.$args['required'].'" name="'.$args['name'].'" size="40" value="1" '.$checked.' />';
}
break;
default:
# code...
break;
}
}
public function validateUrl($value) {
$url = filter_var($value, FILTER_SANITIZE_URL);
if( ! filter_var($url, FILTER_VALIDATE_URL)) {
//add_settings_error( string $setting, string $code, string $message, string $type = 'error' )
add_settings_error( 'marketoforms_rest_api_base', 'marketoforms_rest_api_base', 'URL not valid' );
return false;
}
return $url;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store