Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of adding custom screen options to WordPress
<?php
/*
Plugin Name: Screen Options Tutorial
Plugin URI: http://pmg.co/adding-wordpress-screen-options
Description: An example of how to add custom screen options
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Christopher Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class PMG_Options_Tut
{
/**
* The ajax action
*/
const ACTION = 'pmg_sotut_save';
/**
* Our nonce name
*/
const NONCE = 'pmg_sotut_nonce';
/**
* Init function. Called from outside the class. Adds actions and such.
*
* @access public
* @return null
*/
public static function init()
{
add_action(
'load-post-new.php',
array(get_class(), 'load')
);
add_action(
'wp_ajax_' . self::ACTION,
array(get_class(), 'ajax')
);
}
/**
* Hooked into `load-post-new.php`. Adds an option and
* hooks into a few other actions/filters
*
* @access public
* @return null
*/
public static function load()
{
add_filter(
'screen_settings',
array(get_class(), 'add_field'),
10,
2
);
add_action(
'admin_head',
array(get_class(), 'head')
);
add_filter(
'enter_title_here',
array(get_class(), 'title')
);
}
/**
* Hooked into `screen_settings`. Adds the field to the settings area
*
* @access public
* @return string The settings fields
*/
public static function add_field($rv, $screen)
{
$val = get_user_option(
sprintf('default_title_%s', sanitize_key($screen->id)),
get_current_user_id()
);
$rv .= '<div class="pmg-sotut-container">';
$rv .= '<h5>' . __('Default Title') . '</h5>';
$rv .= '<p><input type="text" class="normal-text" id="pmg-sotut-field" ' .
'value="' . esc_attr($val) . '" /></p>';
$rv .= wp_nonce_field(self::NONCE, self::NONCE, false, false);
$rv .= '</div>';
return $rv;
}
/**
* Hooked into `admin_head`. Spits out some JS to save the info
*
* @access public
* @return null
*/
public static function head()
{
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('input#pmg-sotut-field').blur(function() {
jQuery.post(
ajaxurl,
{
title: jQuery(this).val(),
nonce: jQuery('input#<?php echo esc_js(self::NONCE); ?>').val(),
screen: '<?php echo esc_js(get_current_screen()->id); ?>',
action: '<?php echo self::ACTION; ?>'
}
);
});
});
</script>
<?php
}
/**
* Hooked into `wp_ajax_self::ACTION` Handles saving the fields and such
*
* @access public
* @return null
*/
public static function ajax()
{
check_ajax_referer(self::NONCE, 'nonce');
$screen = isset($_POST['screen']) ? $_POST['screen'] : false;
$title = isset($_POST['title']) ? $_POST['title'] : false;
if(!$screen || !($user = wp_get_current_user()))
{
die(0);
}
if(!$screen = sanitize_key($screen))
{
die(0);
}
update_user_option(
$user->ID,
"default_title_{$screen}",
esc_attr(strip_tags($title))
);
die('1');
}
/**
* Hooked into `enter_title_here`. Replaces the title with the user's
* preference (if it exists).
*
* @access public
* @return string The Default title
*/
public static function title($t)
{
if(!$user = wp_get_current_user())
return $t;
$id = sanitize_key(get_current_screen()->id);
if($title = get_user_option("default_title_{$id}", $user->ID))
{
$t = esc_attr($title);
}
return $t;
}
} // end class
PMG_Options_Tut::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.