Skip to content

Instantly share code, notes, and snippets.

@jazzsequence
Created May 24, 2013 23:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jazzsequence/5647108 to your computer and use it in GitHub Desktop.
Save jazzsequence/5647108 to your computer and use it in GitHub Desktop.
Demonstration plugin for my Introduction to WordPress Plugin Development course for Pluralsight. Download the full source here: http://cl.ly/3u153a2N1v32
<?php
/*
Plugin Name: Pluralsight plugin development demo
Description: This is a demonstration plugin for the Introduction to WordPress Plugin Development course on Pluralsight.
Author: Chris Reynolds
Version: 1.0
*/
function ps_plugindev_options_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php _e( 'Pluralsight Plugin Development Demo', 'ps-demo' ); ?></h2>
<p><?php _e( 'This is a demonstration plugin for Introduction to WordPress Plugin Development. This plugin will add a sharing link underneath the post content.', 'ps-demo' ); ?></p>
<form method="POST" action="options.php">
<?php
settings_fields( 'ps_plugindev_settings' );
?>
<table class="form-table">
<tbody>
<?php ps_plugindev_do_options(); ?>
</tbody>
</table>
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'ps-demo' ); ?>" />
<input type="hidden" name="ps-plugindev-submit" value="Y" />
</form>
</div>
<?php
}
function ps_plugindev_menu() {
add_submenu_page( 'options-general.php', __( 'Plugin Demo', 'ps-demo' ), __( 'Plugin Demo', 'ps-demo' ), 'administrator', 'ps_plugindev', 'ps_plugindev_options_page' );
}
add_action( 'admin_menu', 'ps_plugindev_menu' );
function ps_plugindev_init() {
register_setting( 'ps_plugindev_settings', 'ps_plugindev', 'ps_plugindev_validate' );
}
add_action( 'admin_init', 'ps_plugindev_init' );
function ps_plugindev_do_options() {
$options = get_option( 'ps_plugindev' );
ob_start();
?>
<tr valign="top"><th scope="row"><?php _e( 'Sharing text', 'ps-demo' ); ?></th>
<td>
<?php
if ( $options['sharing-text'] ) {
$options['sharing-text'] = wp_kses($options['sharing-text']);
} else {
$options['sharing-text'] = __( 'Share this post', 'ps-demo' );
}
?>
<input type="text" id="sharing-text" name="ps_plugindev[sharing-text]" width="30" value="<?php echo $options['sharing-text']; ?>" /><br />
<label class="description" for="ps_plugindev[sharing-text]"><?php _e( 'Allows you to change the text that displays next to the sharing buttons', 'ps-demo' ); ?></label>
</td>
</tr>
<tr valign="top"><th scope="row"><?php _e( 'What services do you want to share to?', 'ps-demo' ); ?></th>
<td>
<?php
foreach ( ps_plugindev_services() as $service ) {
$label = $service['label'];
$value = $service['value'];
echo '<label><input type="checkbox" name="ps_plugindev[' . $value . '] value="1" ';
switch ($value) {
case 'google' :
if ( isset( $options['google'] ) ) { checked( 'on', $options['google'] ); }
break;
case 'twitter' :
if ( isset( $options['twitter'] ) ) { checked( 'on', $options['twitter'] ); }
break;
case 'facebook' :
if ( isset( $options['facebook'] ) ) { checked( 'on', $options['facebook'] ); }
break;
case 'stumbleupon' :
if ( isset( $options['stumbleupon'] ) ) { checked( 'on', $options['stumbleupon'] ); }
break;
case 'digg' :
if ( isset( $options['digg'] ) ) { checked( 'on', $options['digg'] ); }
break;
case 'email' :
if ( isset( $options['email'] ) ) { checked( 'on', $options['email'] ); }
break;
}
echo '/> ' . esc_attr($label) . '</label><br />';
}
?>
</td>
<?php
}
function ps_plugindev_services() {
$services = array(
'google' => array(
'value' => 'google',
'label' => __( 'Google+', 'ps-demo' )
),
'twitter' => array(
'value' => 'twitter',
'label' => __( 'Twitter', 'ps-demo' )
),
'facebook' => array(
'value' => 'facebook',
'label' => __( 'Facebook', 'ps-demo' )
),
'stumbleupon' => array(
'value' => 'stumbleupon',
'label' => __( 'StumbleUpon', 'ps-demo' )
),
'digg' => array(
'value' => 'digg',
'label' => __( 'Digg', 'ps-demo' )
),
'email' => array(
'value' => 'email',
'label' => __( 'Email', 'ps-demo' )
),
);
return $services;
}
function ps_plugindev_content_filter( $content ) {
$options = get_option( 'ps_plugindev' );
if ( !$options['sharing-text'] ) {
$options['sharing-text'] = __( 'Share this post', 'ps-demo' );
}
$new_content = '<div class="ps-plugindev-sharing postmeta"><strong>' . $options['sharing-text'] . '</strong><br />';
if ( isset( $options['google'] ) ) {
$new_content .= ps_plugindev_do_google_button();
}
if ( isset( $options['twitter'] ) ) {
$new_content .= ps_plugindev_do_twitter_button();
}
if ( isset( $options['facebook'] ) ) {
$new_content .= ps_plugindev_do_facebook_button();
}
if ( isset( $options['stumbleupon'] ) ) {
$new_content .= ps_plugindev_do_stumbleupon_button();
}
if ( isset( $options['digg'] ) ) {
$new_content .= ps_plugindev_do_digg_button();
}
if ( isset( $options['email'] ) ) {
$new_content .= ps_plugindev_do_email_button();
}
$new_content .= '</div>';
return $content . $new_content;
}
add_filter( 'the_content', 'ps_plugindev_content_filter' );
function ps_plugindev_do_google_button() {
$button = '<div id="google"><g:plusone size="medium"></g:plusone></div>';
return $button;
}
function ps_plugindev_do_twitter_button() {
$button = '<div id="twitter"><a href="https://twitter.com/share" class="twitter-share-button">Tweet</a></div>';
$button .= '<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\'://platform.twitter.com/widgets.js\';fjs.parentNode.insertBefore(js,fjs);}}(document, \'script\', \'twitter-wjs\');</script>';
return $button;
}
function ps_plugindev_do_facebook_button() {
global $post;
$button = '<div id="facebook" class="fb-like" data-href="' . get_permalink( $post->ID ) . '" data-send="false" data-layout="button_count" data-width="100" data-show-faces="false"></div>';
return $button;
}
function ps_plugindev_do_stumbleupon_button() {
$button = '<div id="stumbleupon"><su:badge layout="1"></su:badge></div>';
return $button;
}
function ps_plugindev_do_digg_button() {
global $post;
$link = urlencode( get_permalink( $post->ID ) );
$image = plugins_url( 'img/100x20-digg-button.gif', __FILE__ );
$button = '<div id="digg"><a href="http://digg.com/submit?phase=2&url=' . $link . '"><img src="' . $image . '" height="16" /></a></div>';
return $button;
}
function ps_plugindev_do_email_button() {
global $post;
$title = get_the_title( $post->ID );
$image = plugins_url( 'img/email-icon.gif', __FILE__ );
$button = '<div id="email"><a href="mailto:?subject=' . $title . '&body=This is a cool post: &lt;a href=' . get_permalink( $post->ID ) . '&gt;' . $title . '&lt;/a&gt;"><img src="' . $image . '" /> ' . __( 'Email this', 'ps-demo' ) . '</a></div>';
return $button;
}
function ps_plugindev_head_scripts() {
$options = get_option( 'ps_plugindev' );
if ( isset( $options['facebook'] ) && !is_admin() ) {
?>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=448961491860552";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<?php
}
if ( isset( $options['stumbleupon'] ) && !is_admin() ) {
?>
<script type="text/javascript">
(function() {
var li = document.createElement('script'); li.type = 'text/javascript'; li.async = true;
li.src = ('https:' == document.location.protocol ? 'https:' : 'http:') + '//platform.stumbleupon.com/1/widgets.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(li, s);
})();
</script>
<?php
}
}
add_action( 'wp_head', 'ps_plugindev_head_scripts' );
function ps_plugindev_scripts() {
$options = get_option( 'ps_plugindev' );
if ( !is_admin() ) {
if ( isset( $options['google'] ) ) {
wp_enqueue_script( 'googleplus', 'https://apis.google.com/js/plusone.js' );
}
wp_enqueue_style( 'ps-plugindev', plugins_url( 'css/style.css', __FILE__ ) );
}
}
add_action( 'wp_print_scripts', 'ps_plugindev_scripts' );
function ps_plugindev_validate($input) {
$input['sharing-text'] = wp_filter_nohtml_kses( $input['sharing-text'] );
foreach ( ps_plugindev_services() as $service ) {
$value = $service['value'];
switch ($value) {
case 'google' :
if ( !array_key_exists( $input['google'], $value ) )
$input['google'] = $input['google'];
break;
case 'twitter' :
if ( !array_key_exists( $input['twitter'], $value ) )
$input['twitter'] = $input['twitter'];
break;
case 'facebook' :
if ( !array_key_exists( $input['facebook'], $value ) )
$input['facebook'] = $input['facebook'];
break;
case 'stumbleupon' :
if ( !array_key_exists( $input['stumbleupon'], $value ) )
$input['stumbleupon'] = $input['stumbleupon'];
break;
case 'digg' :
if ( !array_key_exists( $input['digg'], $value ) )
$input['digg'] = $input['digg'];
break;
case 'email' :
if ( !array_key_exists( $input['email'], $value ) )
$input['email'] = $input['email'];
break;
}
}
return $input;
}
/* some styles for the sharing buttons */
.ps-plugindev-sharing #facebook,
.ps-plugindev-sharing #stumbleupon,
.ps-plugindev-sharing #digg,
.ps-plugindev-sharing #email{
float: left;
margin-right: 20px;
}
.ps-plugindev-sharing #google,
.ps-plugindev-sharing #twitter {
float: left;
}
.ps-plugindev-sharing {
clear: both;
}
@Elrashid
Copy link

Elrashid commented Sep 3, 2017

WordPress update :Missing argument 2 for wp_kses()
Old
$options['sharing-text'] = wp_kses($options['sharing-text']);
New
$options['sharing-text'] = wp_kses($options['sharing-text'], array());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment