Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Last active December 14, 2015 08:28
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 franz-josef-kaiser/5057727 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/5057727 to your computer and use it in GitHub Desktop.
Example Plugin to show how to add a numeric/integer media settings field.
<?php
/**
* Plugin Name: (WPM Vienna) Numeric Media Settings field
* Description: Example Plugin to show how to add a numeric/integer media settings field.
* Author: Franz Josef Kaiser
* Author URl: http://unserkaiser.com
* Plugin URl: https://github.com/wecodemore/wcm_limitor
* Version: 1.0
* License: MIT
*
* Copyright: © Franz Josef Kaiser 2013
*/
register_uninstall_hook( __FILE__, array( 'WPM_VIE_MS', 'on_uninstall' ) );
register_activation_hook( __FILE__, array( 'WPM_VIE_MS', 'on_activation' ) );
add_action( 'plugins_loaded', array( 'WPM_VIE_MS', 'init' ) );
class WPM_VIE_MS
{
/**
* Instance of the class
* @static
* @var object
*/
protected static $instance;
/**
* ID for the DB option
* @static
* @var string
*/
public static $id = 'wpm_vie_int_field';
/**
* Default for the DB option
* @static
* @var int
*/
public static $default = 10;
/**
* INIT - instantiates the class
* and checks if we're not in the middle of an import
* @static
* @return object|WPM_VIE_MS
*/
public static function init()
{
if ( defined( 'WP_LOAD_IMPORTERS' ) )
return;
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
# =========== Setup/Install tasks
/**
* Uninstall
* Deletes all options/media settings
* @static
* @return void
*/
public static function on_uninstall()
{
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
delete_option( self::$id );
}
/**
* Activation
* Sets up defaults
* @static
* @return void
*/
public static function on_activation()
{
! get_option( self::$id )
AND add_option(
self::$id
,self::$default
);
}
# =========== Add actions
/**
* Constructor
* @return \WPM_VIE_MS
*/
public function __construct()
{
add_action( 'adminmenu', array( $this, 'add_settings_field' ) );
}
/**
* Registers and adds settings fields for the
* Media Settings screen. Adds size limit fields.
* @wphook adminmenu
* @return void
*/
public function add_settings_field()
{
if ( 'options-media' !== get_current_screen()->id )
return;
$label = __( 'New Settings Field', 'wpm_vie_ms_textdomain' );
register_setting(
'media'
,'max_img_mp'
,array( $this, 'sanitize_field' )
);
add_settings_field(
get_class( $this )
,$label
,array( $this, 'settings_field' )
,'media'
,'default'
,array( 'label' => $label )
);
}
/**
* Registers and adds settings fields for the
* Media Settings screen. Adds the WPM Vienna field.
* @wphook adminmenu
* @return void
*/
public function settings_field( $args )
{
$id = self::$id;
?>
<fieldset>
<legend class="screen-reader-text">
<span><?php echo $args['label']; ?></span>
</legend>
<input type="number"
name="<?php echo $id; ?>"
id="<?php echo $id; ?>"
value="<?php form_option( $id ); ?>"
step="1"
min="0"
class="small-text"
/>
<label for="<?php echo $id; ?>">
<?php _e( 'WPM Vienna Avatar width', 'wpm_vie_ms_textdomain' ); ?>
</label>
</fieldset>
<?php
}
/**
* Santize callback for the setting field.
* Returns only integers.
* @param int $value
* @return int $value
*/
public function sanitize_field( $value )
{
return filter_var(
$value
,FILTER_SANITIZE_NUMBER_INT
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment