Skip to content

Instantly share code, notes, and snippets.

@Ratko-Solaja
Created November 5, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratko-Solaja/4a333b6131708ef60bb2a43d6b9eefda to your computer and use it in GitHub Desktop.
Save Ratko-Solaja/4a333b6131708ef60bb2a43d6b9eefda to your computer and use it in GitHub Desktop.
Sandbox
/**
* Sandbox our settings.
*
* @since 1.0.0
*/
public function sandbox_register_setting( $input ) {
$new_input = array();
if ( isset( $input ) ) {
// Loop trough each input and sanitize the value if the input id isn't post-types
foreach ( $input as $key => $value ) {
if ( $key == 'post-types' ) {
$new_input[ $key ] = $value;
} else {
$new_input[ $key ] = sanitize_text_field( $value );
}
}
}
return $new_input;
}
/**
* Sandbox our section for the settings.
*
* @since 1.0.0
*/
public function sandbox_add_settings_section() {
return;
}
/**
* Sandbox our single checkboxes.
*
* @since 1.0.0
*/
public function sandbox_add_settings_field_single_checkbox( $args ) {
$field_id = $args['label_for'];
$field_description = $args['description'];
$options = get_option( $this->plugin_name . '-settings' );
$option = 0;
if ( ! empty( $options[ $field_id ] ) ) {
$option = $options[ $field_id ];
}
?>
<label for="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>">
<input type="checkbox" name="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>" id="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>" <?php checked( $option, true, 1 ); ?> value="1" />
<span class="description"><?php echo esc_html( $field_description ); ?></span>
</label>
<?php
}
/**
* Sandbox our multiple checkboxes
*
* @since 1.0.0
*/
public function sandbox_add_settings_field_multiple_checkbox( $args ) {
$field_id = $args['label_for'];
$field_description = $args['description'];
$options = get_option( $this->plugin_name . '-settings' );
$option = array();
if ( ! empty( $options[ $field_id ] ) ) {
$option = $options[ $field_id ];
}
if ( $field_id == 'post-types' ) {
$args = array(
'public' => true
);
$post_types = get_post_types( $args, 'objects' );
foreach ( $post_types as $post_type ) {
if ( $post_type->name != 'attachment' ) {
if ( in_array( $post_type->name, $option ) ) {
$checked = 'checked="checked"';
} else {
$checked = '';
}
?>
<fieldset>
<label for="<?php echo $this->plugin_name . '-settings[' . $field_id . '][' . $post_type->name . ']'; ?>">
<input type="checkbox" name="<?php echo $this->plugin_name . '-settings[' . $field_id . '][]'; ?>" id="<?php echo $this->plugin_name . '-settings[' . $field_id . '][' . $post_type->name . ']'; ?>" value="<?php echo esc_attr( $post_type->name ); ?>" <?php echo $checked; ?> />
<span class="description"><?php echo esc_html( $post_type->label ); ?></span>
</label>
</fieldset>
<?php
}
}
} else {
$field_args = $args['options'];
foreach ( $field_args as $field_arg_key => $field_arg_value ) {
if ( in_array( $field_arg_key, $option ) ) {
$checked = 'checked="checked"';
} else {
$checked = '';
}
?>
<fieldset>
<label for="<?php echo $this->plugin_name . '-settings[' . $field_id . '][' . $field_arg_key . ']'; ?>">
<input type="checkbox" name="<?php echo $this->plugin_name . '-settings[' . $field_id . '][]'; ?>" id="<?php echo $this->plugin_name . '-settings[' . $field_id . '][' . $field_arg_key . ']'; ?>" value="<?php echo esc_attr( $field_arg_key ); ?>" <?php echo $checked; ?> />
<span class="description"><?php echo esc_html( $field_arg_value ); ?></span>
</label>
</fieldset>
<?php
}
}
?>
<p class="description"><?php echo esc_html( $field_description ); ?></p>
<?php
}
/**
* Sandbox our inputs with text
*
* @since 1.0.0
*/
public function sandbox_add_settings_field_input_text( $args ) {
$field_id = $args['label_for'];
$field_default = $args['default'];
$options = get_option( $this->plugin_name . '-settings' );
$option = $field_default;
if ( ! empty( $options[ $field_id ] ) ) {
$option = $options[ $field_id ];
}
?>
<input type="text" name="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>" id="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>" value="<?php echo esc_attr( $option ); ?>" class="regular-text" />
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment