Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Feed Refresh
- Plugin URI: #
- Description: Feed Refresh Beta.
- Version: 0.1
- Author: Dexter
- Author URI: #
- */
- // ------------------------------------------------------------------------
- // REQUIRE MINIMUM VERSION OF WORDPRESS:
- // ------------------------------------------------------------------------
- // THIS IS USEFUL IF YOU REQUIRE A MINIMUM VERSION OF WORDPRESS TO RUN YOUR
- // PLUGIN. IN THIS PLUGIN THE WP_EDITOR() FUNCTION REQUIRES WORDPRESS 3.3
- // OR ABOVE. ANYTHING LESS SHOWS A WARNING AND THE PLUGIN IS DEACTIVATED.
- // ------------------------------------------------------------------------
- function fr_requires_wordpress_version() {
- global $wp_version;
- $plugin = plugin_basename( __FILE__ );
- $plugin_data = get_plugin_data( __FILE__, false );
- if ( version_compare($wp_version, "3.3", "<" ) ) {
- if( is_plugin_active($plugin) ) {
- deactivate_plugins( $plugin );
- wp_die( "'".$plugin_data['Name']."' requires WordPress 3.3 or higher, and has been deactivated! Please upgrade WordPress and try again.<br /><br />Back to <a href='".admin_url()."'>WordPress admin</a>." );
- }
- }
- }
- add_action( 'admin_init', 'fr_requires_wordpress_version' );
- // ------------------------------------------------------------------------
- // PLUGIN PREFIX:
- // ------------------------------------------------------------------------
- // A PREFIX IS USED TO AVOID CONFLICTS WITH EXISTING PLUGIN FUNCTION NAMES.
- // WHEN CREATING A NEW PLUGIN, CHANGE THE PREFIX AND USE YOUR TEXT EDITORS
- // SEARCH/REPLACE FUNCTION TO RENAME THEM ALL QUICKLY.
- // ------------------------------------------------------------------------
- // 'fr_' prefix is derived from [p]plugin [o]ptions [s]tarter [k]it
- // ------------------------------------------------------------------------
- // REGISTER HOOKS & CALLBACK FUNCTIONS:
- // ------------------------------------------------------------------------
- // HOOKS TO SETUP DEFAULT PLUGIN OPTIONS, HANDLE CLEAN-UP OF OPTIONS WHEN
- // PLUGIN IS DEACTIVATED AND DELETED, INITIALISE PLUGIN, ADD OPTIONS PAGE.
- // ------------------------------------------------------------------------
- // Set-up Action and Filter Hooks
- register_activation_hook(__FILE__, 'fr_add_defaults');
- register_uninstall_hook(__FILE__, 'fr_delete_plugin_options');
- add_action('admin_init', 'fr_init' );
- add_action('admin_menu', 'fr_add_options_page');
- add_filter( 'plugin_action_links', 'fr_plugin_action_links', 10, 2 );
- // --------------------------------------------------------------------------------------
- // CALLBACK FUNCTION FOR: register_uninstall_hook(__FILE__, 'fr_delete_plugin_options')
- // --------------------------------------------------------------------------------------
- // THIS FUNCTION RUNS WHEN THE USER DEACTIVATES AND DELETES THE PLUGIN. IT SIMPLY DELETES
- // THE PLUGIN OPTIONS DB ENTRY (WHICH IS AN ARRAY STORING ALL THE PLUGIN OPTIONS).
- // --------------------------------------------------------------------------------------
- // Delete options table entries ONLY when plugin deactivated AND deleted
- function fr_delete_plugin_options() {
- delete_option('fr_options');
- }
- // ------------------------------------------------------------------------------
- // CALLBACK FUNCTION FOR: register_activation_hook(__FILE__, 'fr_add_defaults')
- // ------------------------------------------------------------------------------
- // THIS FUNCTION RUNS WHEN THE PLUGIN IS ACTIVATED. IF THERE ARE NO THEME OPTIONS
- // CURRENTLY SET, OR THE USER HAS SELECTED THE CHECKBOX TO RESET OPTIONS TO THEIR
- // DEFAULTS THEN THE OPTIONS ARE SET/RESET.
- //
- // OTHERWISE, THE PLUGIN OPTIONS REMAIN UNCHANGED.
- // ------------------------------------------------------------------------------
- // Define default option settings
- function fr_add_defaults() {
- $tmp = get_option('fr_options');
- if(($tmp['chk_default_options_db']=='1')||(!is_array($tmp))) {
- delete_option('fr_options'); // so we don't have to reset all the 'off' checkboxes too! (don't think this is needed but leave for now)
- $arr = array(
- "txt_1" => "Enter whatever you like here..",
- "txt_2" => "Enter whatever you like here..",
- "txt_3" => "Enter whatever you like here..",
- "txt_4" => "Enter whatever you like here..",
- "txt_5" => "Enter whatever you like here.."
- );
- update_option('fr_options', $arr);
- }
- }
- // ------------------------------------------------------------------------------
- // CALLBACK FUNCTION FOR: add_action('admin_init', 'fr_init' )
- // ------------------------------------------------------------------------------
- // THIS FUNCTION RUNS WHEN THE 'admin_init' HOOK FIRES, AND REGISTERS YOUR PLUGIN
- // SETTING WITH THE WORDPRESS SETTINGS API. YOU WON'T BE ABLE TO USE THE SETTINGS
- // API UNTIL YOU DO.
- // ------------------------------------------------------------------------------
- // Init plugin options to white list our options
- function fr_init(){
- register_setting( 'fr_plugin_options', 'fr_options', 'fr_validate_options' );
- }
- // ------------------------------------------------------------------------------
- // CALLBACK FUNCTION FOR: add_action('admin_menu', 'fr_add_options_page');
- // ------------------------------------------------------------------------------
- // THIS FUNCTION RUNS WHEN THE 'admin_menu' HOOK FIRES, AND ADDS A NEW OPTIONS
- // PAGE FOR YOUR PLUGIN TO THE SETTINGS MENU.
- // ------------------------------------------------------------------------------
- // Add menu page
- function fr_add_options_page() {
- add_menu_page('Feed Refresh Options Page', 'Feed Refresh', 'manage_options', __FILE__, 'fr_render_form');
- }
- // ------------------------------------------------------------------------------
- // CALLBACK FUNCTION SPECIFIED IN: add_options_page()
- // ------------------------------------------------------------------------------
- // THIS FUNCTION IS SPECIFIED IN add_options_page() AS THE CALLBACK FUNCTION THAT
- // ACTUALLY RENDER THE PLUGIN OPTIONS FORM AS A SUB-MENU UNDER THE EXISTING
- // SETTINGS ADMIN MENU.
- // ------------------------------------------------------------------------------
- // Render the Plugin options form
- function fr_render_form() {
- ?>
- <div class="wrap">
- <!-- Display Plugin Icon, Header, and Description -->
- <div class="icon32" id="icon-options-general"><br></div>
- <h2>Plugin Options Starter Kit</h2>
- <p>Below is a collection of sample controls you can use in your own Plugins. Or, you can analyse the code and learn how all the most common controls can be added to a Plugin options form. See the code for more details, it is fully commented.</p>
- <!-- Beginning of the Plugin Options Form -->
- <form method="post" action="options.php">
- <?php settings_fields('fr_plugin_options'); ?>
- <?php $options = get_option('fr_options'); ?>
- <!-- Table Structure Containing Form Controls -->
- <!-- Each Plugin Option Defined on a New Table Row -->
- <table class="form-table">
- <!-- Textbox Control -->
- <tr><th scope="row">Track 1</th><td><input type="text" size="200" name="fr_options[txt_1]" value="<?php echo $options['txt_1']; ?>" /></td></tr>
- <tr><th scope="row">Track 2</th><td><input type="text" size="200" name="fr_options[txt_2]" value="<?php echo $options['txt_2']; ?>" /></td></tr>
- <tr><th scope="row">Track 3</th><td><input type="text" size="200" name="fr_options[txt_3]" value="<?php echo $options['txt_3']; ?>" /></td></tr>
- <tr><th scope="row">Track 4</th><td><input type="text" size="200" name="fr_options[txt_4]" value="<?php echo $options['txt_4']; ?>" /></td></tr>
- <tr><th scope="row">Track 5</th><td><input type="text" size="200" name="fr_options[txt_5]" value="<?php echo $options['txt_5']; ?>" /></td></tr>
- <tr><td colspan="2"><div style="margin-top:10px;"></div></td></tr>
- <tr valign="top" style="border-top:#dddddd 1px solid;">
- <th scope="row">Database Options</th>
- <td>
- <label><input name="fr_options[chk_default_options_db]" type="checkbox" value="1" <?php if (isset($options['chk_default_options_db'])) { checked('1', $options['chk_default_options_db']); } ?> /> Restore defaults upon plugin deactivation/reactivation</label>
- <br /><span style="color:#666666;margin-left:2px;">Only check this if you want to reset plugin settings upon Plugin reactivation</span>
- </td>
- </tr>
- </table>
- <p class="submit">
- <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
- </p>
- </form>
- <p style="margin-top:15px;">
- <p style="font-style: italic;font-weight: bold;color: #26779a;">If you have found this starter kit at all useful, please consider making a <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XKZXD2BHQ5UB2" target="_blank" style="color:#72a1c6;">donation</a>. Thanks.</p>
- <span><a href="http://www.facebook.com/PressCoders" title="Our Facebook page" target="_blank"><img style="border:1px #ccc solid;" src="<?php echo plugins_url(); ?>/feed-refresh/images/facebook-icon.png" /></a></span>
- <span><a href="http://www.twitter.com/dgwyer" title="Follow on Twitter" target="_blank"><img style="border:1px #ccc solid;" src="<?php echo plugins_url(); ?>/feed-refresh/images/twitter-icon.png" /></a></span>
- <span><a href="http://www.presscoders.com" title="PressCoders.com" target="_blank"><img style="border:1px #ccc solid;" src="<?php echo plugins_url(); ?>/feed-refresh/images/pc-icon.png" /></a></span>
- </p>
- </div>
- <?php
- }
- // Sanitize and validate input. Accepts an array, return a sanitized array.
- function fr_validate_options($input) {
- // strip html from textboxes
- $input['txt_1'] = wp_filter_nohtml_kses($input['txt_1']); // Sanitize textbox input (strip html tags, and escape characters)
- $input['txt_2'] = wp_filter_nohtml_kses($input['txt_2']); // Sanitize textbox input (strip html tags, and escape characters)
- $input['txt_3'] = wp_filter_nohtml_kses($input['txt_3']); // Sanitize textbox input (strip html tags, and escape characters)
- $input['txt_4'] = wp_filter_nohtml_kses($input['txt_4']); // Sanitize textbox input (strip html tags, and escape characters)
- $input['txt_5'] = wp_filter_nohtml_kses($input['txt_5']); // Sanitize textbox input (strip html tags, and escape characters)
- return $input;
- }
- // Display a Settings link on the main Plugins page
- function fr_plugin_action_links( $links, $file ) {
- if ( $file == plugin_basename( __FILE__ ) ) {
- $fr_links = '<a href="'.get_admin_url().'admin.php?page=feed-refresh/feed-refresh.php">'.__('Settings').'</a>';
- // make the 'Settings' link appear first
- array_unshift( $links, $fr_links );
- }
- return $links;
- }
- // ------------------------------------------------------------------------------
- // SAMPLE USAGE FUNCTIONS:
- // ------------------------------------------------------------------------------
- // THE FOLLOWING FUNCTIONS SAMPLE USAGE OF THE PLUGINS OPTIONS DEFINED ABOVE. TRY
- // CHANGING THE DROPDOWN SELECT BOX VALUE AND SAVING THE CHANGES. THEN REFRESH
- // A PAGE ON YOUR SITE TO SEE THE UPDATED VALUE.
- // ------------------------------------------------------------------------------
- add_action('admin_init', 'feeds');
- function feeds() {
- $options = get_option('fr_options');
- $arr = array(
- $options['txt_1'],
- $options['txt_2'],
- $options['txt_3'],
- $options['txt_4'],
- $options['txt_5']
- );
- $filename = plugin_dir_path(__FILE__) . "test.php";
- $text = "";
- foreach($arr as $value)
- {
- $text .= $value."\n";
- }
- $fh = fopen($filename, "w") or die("Could not open file.");
- fwrite($fh, $text) or die("Could not write file!");
- fclose($fh);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement