replacement text key/value pairs // Contructor function AnyVar() { // Load current vars $this->vars = get_option('anyvar_vars'); if(is_admin()) { add_action('admin_menu',array(&$this,'anyvar_menu')); add_action('activate_'.basename (dirname (__FILE__)).'/'.basename (__FILE__), array(&$this,'activate')); } else { add_action('template_redirect',array(&$this,'anyvar_ob_start')); } } /* Admin Display Functions */ // Adds the AnyVar menu item function anyvar_menu() { $page = add_management_page('AnyVar Admin', 'AnyVar', $this->min_level, __FILE__, array(&$this,'anyvar_admin')); add_action("admin_print_styles-$page", array(&$this,'anyvar_css')); // Add required css file(s) add_action("admin_print_scripts-$page", array(&$this,'anyvar_head')); // Add required JS file(s) } // Loads the Wordpress forms.js script function anyvar_head() { wp_enqueue_script('admin-forms'); if ( user_can_richedit() ) { wp_enqueue_script('media-upload'); add_action('admin_head', 'wp_tiny_mce'); wp_enqueue_script('editor'); } } function anyvar_css() { wp_enqueue_style('thickbox'); } // Displays the admin pages function anyvar_admin() { $action = ''; if(isset($_REQUEST['action'])) $action = $_REQUEST['action']; if ( isset($_POST['deleteit']) && isset($_POST['delete']) ) $action = 'delete'; $this->display_heading(); // Strip the magic / wordpress added quotes $_POST['var_name'] = stripslashes(strtolower($_POST['var_name'])); $_POST['content'] = stripslashes($_POST['content']); switch ($action) { case 'add': // The add variable form has been submitted $error = $this->validate($_POST['var_name']); if($error == '') { $this->vars[$_POST['var_name']] = $_POST['content']; update_option('anyvar_vars',$this->vars); $this->display_message("[{$_POST['var_name']}] successfully added."); $this->display_list(); $this->display_form(); } else // There's an error. Display the error and the form again { $this->display_message($error); $this->display_form('add',$_POST['var_name'],$_POST['content']); } break; case 'edit': // Load the variable to be edited, display it in a form if(isset($this->vars[$_GET['var']])) { $this->display_form('edit',$_GET['var'],$this->vars[$_GET['var']]); } else { $this->display_message("Cannot edit variable because it doesn't exist."); $this->display_list(); $this->display_form(); } break; case 'edited': // The edit variable form has been submitted $error = $this->validate($_POST['var_name']); if($error == '') { // Update the exiting key instead of unsetting and replacing it. This preserves the array order $temp_array = $this->update_array_key($this->vars,$_POST['old_var_name'],$_POST['var_name']); $temp_array[$_POST['var_name']] = $_POST['content']; $this->vars = $temp_array; update_option('anyvar_vars',$this->vars); unset($temp_array); $this->display_message("[{$_POST['var_name']}] successfully edited."); $this->display_list(); $this->display_form(); } else // There's an error. Display the error and the form again { $this->display_message($error); $this->display_form('edit',$_POST['var_name'],$_POST['content']); } break; case 'delete': // Variable deletion form has been submitted if(is_array($_POST['delete']) && count($_POST['delete'])) // There are variables selected to be delete { $count = 0; foreach($_POST['delete'] as $unlucky_var) { if(isset($this->vars[$unlucky_var])) { $count++; unset($this->vars[$unlucky_var]); } } update_option('anyvar_vars',$this->vars); $this->display_message("$count variables deleted"); $this->display_list(); $this->display_form(); } else // No variables were selected for deletion { $this->display_list(); $this->display_form(); } break; default: // The standard page $this->display_list(); $this->display_form(); break; } } // Displays a message function display_message($message) { echo "
$message

"; } // Displays the heading function display_heading() { ?>

AnyVar - Manage Variables



vars as $var_name => $var_text) { $i++; if($i % 2 == 1) $class = "class='alternate'"; else $class = ''; echo ""; } ?>
  Var Name Var Tag Var Text
$var_name [$var_name]

The most effective way to invoke your variables is with tags.
Simply put a [var_name] tag anywhere in your HTML.

Please Note: tags must be lowercase.


A Variable

';?>

The variable name is used in variables tags, which are used to 'call' the variable. It may only contain lowercase letters, numbers and underscores.

This is the text that'll be 'pasted' in where ever you put the variable tag.

'; elseif(!preg_match('/^[a-z0-9_]*$/', $var_name)) $error_str .= 'ERROR: Variable Name can only contain lowercase letters, numbers & underscores (a-z,0-9,_)
'; return $error_str; } // Update / change the value of a given array key function update_array_key($array,$old_key,$new_key) { foreach($array as $key => $value) $new_array[ ($key === $old_key) ? $new_key : $key ] = $value; return $new_array; } // Add the AnyVar variables array to Wordpress options function activate() { if(!is_array($this->vars)) { $this->vars = array('example_var' => 'Example text. Delete this variable.'); add_option('anyvar_vars',$this->vars); } } /* Output functions */ // Start output buffering function anyvar_ob_start() { ob_start(array(&$this,'anyvar_callback')); } // Replace all existings variable tags with their text values function anyvar_callback($output_html) { $find = array_keys($this->vars); array_walk($find,create_function('&$value,$key', '$value = \'[\'.$value.\']\';')); return str_replace($find,array_values($this->vars),$output_html); } // Return the value of a variable function return_single_var($var_name) { if(isset($this->vars[$var_name])) return $this->vars[$var_name]; return ''; } } $anyvar = new AnyVar; // Fnction for outputting an AnyVar variable via PHP. This is mainly useful in templates. function anyvar($var_name) { global $anyvar; echo $anyvar->return_single_var($var_name); } ?>