Skip to content

Instantly share code, notes, and snippets.

@alexkingorg
Created February 6, 2014 21:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexkingorg/8852391 to your computer and use it in GitHub Desktop.
Save alexkingorg/8852391 to your computer and use it in GitHub Desktop.
Some starter WordPress code for saving pages/posts as favorite.
<?php
/*
Plugin Name: Favorites
Plugin URI:
Description: Let users save posts/pages as favorites - then access them through a quick menu.
Version: 1.0
Author: Crowd Favorite
Author URI: http://crowdfavorite.com
*/
// ini_set('display_errors', '1'); ini_set('error_reporting', E_ALL);
if (!defined('PLUGINDIR')) {
define('PLUGINDIR','wp-content/plugins');
}
load_plugin_textdomain('cf_favorites');
function cffp_favorites_list() {
// TODO show list
}
function cffp_list_pages() {
// TODO - conditionally add to page list
}
function cffp_add_favorite($url, $title) {
// TODO - should use ID instead?
}
function cffp_request_handler() {
if (!empty($_GET['cf_action'])) {
switch ($_GET['cf_action']) {
case 'cffp_js':
cffp_js();
break;
case 'cffp_css':
cffp_css();
break;
case 'cffp_admin_js':
cffp_admin_js();
break;
case 'cffp_admin_css':
cffp_admin_css();
die();
break;
}
}
if (!empty($_POST['cf_action'])) {
switch ($_POST['cf_action']) {
case 'cffp_add_favorite':
// TODO
// - catch title and URL
die();
break;
case 'cffp_update_settings':
// TODO
// - catch array of titles and URLs and save new order
cffp_save_settings();
wp_redirect(trailingslashit(get_bloginfo('wpurl')).'wp-admin/options-general.php?page=cf-favorites.php&updated=true');
die();
break;
}
}
}
add_action('init', 'cffp_request_handler');
function cffp_js() {
header('Content-type: text/javascript');
// TODO
die();
}
function cffp_css() {
header('Content-type: text/css');
// TODO
die();
}
function cffp_admin_js() {
header('Content-type: text/javascript');
// TODO
die();
}
function cffp_admin_css() {
header('Content-type: text/css');
?>
fieldset.options div.option {
background: #EAF3FA;
margin-bottom: 8px;
padding: 10px;
}
fieldset.options div.option label {
display: block;
float: left;
font-weight: bold;
margin-right: 10px;
width: 150px;
}
fieldset.options div.option span.help {
color: #666;
font-size: 11px;
margin-left: 8px;
}
<?php
die();
}
function cffp_enqueue_assets() {
$u = trailingslashit(get_bloginfo('url'));
if (is_admin()) {
wp_enqueue_script('cffp_admin_js', $u.'?cf_action=cffp_admin_js', 'jquery');
wp_enqueue_style('cffp_admin_css', $u.'?cf_action=cffp_admin_css');
}
else {
wp_enqueue_script('cffp_js', $u.'?cf_action=cffp_js', 'jquery');
wp_enqueue_style('cffp_css', $u.'?cf_action=cffp_css');
}
}
// Run at 'wp' when conditionals like is_single() are available.
add_action('wp', 'cffp_enqueue_assets');
$cffp_settings = array(
'cffp_' => array(
'type' => 'string',
'label' => '',
'default' => '',
'help' => '',
),
'cffp_' => array(
'type' => 'int',
'label' => '',
'default' => 5,
'help' => '',
),
'cffp_' => array(
'type' => 'select',
'label' => '',
'default' => '',
'help' => '',
'options' => array(
'' => ''
),
),
);
function cffp_setting($option) {
$value = get_option($option);
if (empty($value)) {
global $cffp_settings;
$value = $cffp_settings[$option]['default'];
}
return $value;
}
function cffp_admin_menu() {
if (current_user_can('manage_options')) {
add_options_page(
__('Favorites Settings', 'cf_favorites')
, __('Favorites', 'cf_favorites')
, 10
, basename(__FILE__)
, 'cffp_settings_form'
);
}
}
add_action('admin_menu', 'cffp_admin_menu');
function cffp_plugin_action_links($links, $file) {
$plugin_file = basename(__FILE__);
if ($file == $plugin_file) {
$settings_link = '<a href="options-general.php?page='.$plugin_file.'">'.__('Settings', 'cf_favorites').'</a>';
array_unshift($links, $settings_link);
}
return $links;
}
add_filter('plugin_action_links', 'cffp_plugin_action_links', 10, 2);
if (!function_exists('cf_settings_field')) {
function cf_settings_field($key, $config) {
$option = get_option($key);
if (empty($option) && !empty($config['default'])) {
$option = $config['default'];
}
$label = '<label for="'.$key.'">'.$config['label'].'</label>';
$htlp = '<span class="help">'.$config['help'].'</span>';
switch ($config['type']) {
case 'select':
$output = $label.'<select name="'.$key.'" id="'.$key.'">';
foreach ($config['options'] as $val => $display) {
$option == $val ? $sel = ' selected="selected"' : $sel = '';
$output .= '<option value="'.$val.'"'.$sel.'>'.$display.'</option>';
}
$output .= '</select>'.$help;
break;
case 'string':
case 'int':
default:
$output = $label.'<input name="'.$key.'" id="'.$key.'" value="'.$option.'" />'.$help;
break;
}
return '<div class="option">'.$output.'<div class="clear"></div></div>';
}
}
function cffp_settings_form() {
global $cffp_settings;
// TODO - show list of favorites on admin page, allow removal - reorder of them
print('
<div class="wrap">
<h2>'.__('Favorites Settings', 'cf_favorites').'</h2>
<form id="cffp_settings_form" name="cffp_settings_form" action="'.get_bloginfo('wpurl').'/wp-admin/options-general.php" method="post">
<input type="hidden" name="cf_action" value="cffp_update_settings" />
<fieldset class="options">
');
foreach ($cffp_settings as $key => $config) {
echo cf_settings_field($key, $config);
}
print('
</fieldset>
<p class="submit">
<input type="submit" name="submit" value="'.__('Save Settings', 'cf_favorites').'" />
</p>
</form>
</div>
');
}
function cffp_save_settings() {
if (!current_user_can('manage_options')) {
return;
}
global $cffp_settings;
foreach ($cffp_settings as $key => $option) {
$value = '';
switch ($option['type']) {
case 'int':
$value = intval($_POST[$key]);
break;
case 'select':
$test = stripslashes($_POST[$key]);
if (isset($option['options'][$test])) {
$value = $test;
}
break;
case 'string':
default:
$value = stripslashes($_POST[$key]);
break;
}
update_option($key, $value);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment