multiple instances of a class in wordpress plugin

I’m cross-posting this from the wordpress section, b/c I think the problem lies more in my lack of understanding of OOP, so maybe it should’ve been posted here in the first place.

In writing wordpress plugins I wanted to created a class that I could use in every plugin to take care of the mundane creation of a plugin options page: so add menu page, white list settings, spit out and validate options, etc. I thought basically, I wanted to re-use something I must need to write a class.

Read More

at its most base, i was expecting that i could say new KIA_Plugin_Options($args) in both plugins one and two (with different $args) and that each would crank out the options pages for their respective plugins. however, i have added the following class in 2 plugins and instead of creating the menus for each it only handles 1 and then dies. so parts of it work, but isn’t the re-usable panacea I was going for yet, so i feel like i am missing some important concept in OOP.

here’s the skeleton of my options class:

<?php

class KIA_Plugin_Options {

  private $page_title;
  private $menu_title;
  private $capability;
  private $menu_slug;
  private $setting_name;
  private $option_name;

  function KIA_Plugin_Options( $args ){

    //convert $args to array 
    if(!is_array($args)) $args['page_title'] = $args; 

    if(!isset($args['page_title'])) die (__('Plugin Options needs at least the plugin's page title parameter.','kia_plugin_options'));

    // Define the class variables
    $this->page_title = $args['page_title']; 
    $this->menu_title = isset($args['menu_title']) ? $args['menu_title'] : $this->page_title;
    $this->capability = isset($args['capability']) ? $args['capability'] : 'manage_options';
    $this->menu_slug = isset($args['menu_slug']) ? $args['menu_slug'] : sanitize_title_with_dashes($this->menu_title);
    $this->setting_name = isset($args['setting_name']) ? $args['setting_name'] : $this->menu_slug;
    $this->option_name = isset($args['option_name']) ? $args['option_name'] : $this->menu_slug;

    // Set-up Action and Filter Hooks
    register_activation_hook(__FILE__, array(&$this,'add_defaults_options'));
    register_uninstall_hook(__FILE__, array(&$this,'delete_plugin_options'));

    //add settings page
    add_action('admin_init', array(&$this,'register_settings' ));
    add_action('admin_menu', array(&$this,'add_options_page'));

    //add settings link to plugins page
    add_filter( 'plugin_action_links', array(&$this,'add_action_links'), 10, 2 );
  }


  // ------------------------------------------------------------------------------
  // CALLBACK FUNCTION FOR: register_activation_hook(__FILE__, 'add_defaults_options')
  // ------------------------------------------------------------------------------

  // Define default option settings
  function add_defaults_options() {
    if(get_option('kia_tell_a_friend_options')) return false;

    $defaults = array( 
              "emailsubject" => __( 'I saw this and thought of you!', 'kia_tell_a_friend'),
              "emailmsg" => __( 'I just saw this!', 'kia_tell_a_friend'),
              "twittermsg" => __( 'Check this out!', 'kia_tell_a_friend')
    );
    update_option('kia_tell_a_friend_options', $defaults);
  }


  // --------------------------------------------------------------------------------------
  // CALLBACK FUNCTION FOR: register_uninstall_hook(__FILE__, 'delete_plugin_options')
  // --------------------------------------------------------------------------------------

  // Delete options table entries ONLY when plugin deactivated AND deleted
  function delete_plugin_options() {
    $options = get_option('kia_tell_a_friend_options', true);
    if(isset($options['delete'])) delete_option('kia_tell_a_friend_options');
  }


  // ------------------------------------------------------------------------------
  // CALLBACK FUNCTION FOR: add_action('admin_init', array(&$this,'register_settings' ));
  // ------------------------------------------------------------------------------

  // Init plugin options to white list our options
  function register_settings(){ 
    register_setting( $this->setting_name, $this->option_name, array(&$this,'validate_options') );
  }

  // ------------------------------------------------------------------------------
  // CALLBACK FUNCTION FOR: add_action('admin_menu', array(&$this,'add_options_page'));
  // ------------------------------------------------------------------------------

  // Add menu page
  function add_options_page() {
     $page = add_options_page( $this->page_title , $this->menu_title, $this->capability, $this->menu_slug, array(&$this,'render_form'));

    /* Using registered $page handle to hook stylesheet loading */
    add_action( 'admin_print_styles-' . $page, array(&$this,'admin_style' ));
  }

  // ------------------------------------------------------------------------------
  // CALLBACK FUNCTION FOR: add_action( 'admin_print_styles-' . $page, array(&$this,'admin_styles' ));
  // ------------------------------------------------------------------------------

  // Add menu page styles
  function admin_style() {
    wp_enqueue_style('plugin-options',plugins_url('css/options-framework.css', __FILE__));
  }


  // ------------------------------------------------------------------------------
  // CALLBACK FUNCTION SPECIFIED IN: add_options_page()
  // ------------------------------------------------------------------------------

  // Render the Plugin options form
  function render_form() {
    echo "Bacon!!!";
  }

  // Sanitize and validate input. Accepts an array, return a sanitized array.
  function validate_options($input) {

    $clean = array();

     // strip html from textboxes
    $clean['pubid'] =  wp_filter_nohtml_kses($input['pubid']); // Sanitize text input (strip html tags, and escape characters)
    $clean['delete'] =  isset( $input['delete'] ) ? 'true' : 'false' ;  //checkbox

    $clean['emailsubject'] =  wp_filter_nohtml_kses($input['emailsubject']); // Sanitize text input (strip html tags, and escape characters)
    $clean['emailmsg'] =  wp_filter_post_kses($input['emailmsg']); // Sanitize textbox input (allow tags that area allowed in posts)

    $clean['twittermsg'] =  wp_filter_nohtml_kses($input['twittermsg']); // Sanitize text input (strip html tags, and escape characters)

    return $clean;
  }

  // Display a Settings link on the main Plugins page
  function add_action_links( $links, $file ) {

    if ( $file == plugin_basename( __FILE__ ) ) {
      $posk_links = '<a href="'.admin_url('options-general.php?page=tell-a-friend').'">'.__('Settings', 'kia_tell_a_friend').'</a>';
      // make the 'Settings' link appear first
      array_unshift( $links, $posk_links );
    }

    return $links;
  }


}

then in my first plugin i am calling (on the init hook)

$args = array ( 'page_title' => 'apple options page',
                        'menu_title' => 'apple options', 
                        'capability' => 'manage_options',
                        'menu_slug' => 'apple_options',
                        'setting_name' => 'apple_settings',
                        'option_name' => 'apple_db_options' );

    $this->options = new KIA_Plugin_Options( $args );

and on the second plugin

$args = array ( 'page_title' => 'bacon options page',
                    'menu_title' => 'bacon options', 
                    'capability' => 'manage_options',
                    'menu_slug' => 'bacon_options',
                    'setting_name' => 'bacon_settings',
                    'option_name' => 'bacon_db_options' );

$this->options = new KIA_Plugin_Options( $args );

both the plugins are contained in their own, respective classes. i can post one of the dummies if that’ll help, but it was getting to be a lot of code.

Related posts

Leave a Reply

1 comment

  1. From own experience I can say that it is possible to share bases classes per se with PHP, however keep in mind that WordPress itself is not object oriented.

    So when you register hooks, pages, callbacks and the like, take care that you and wordpress itself can easily distinguish between.

    For example in your plugin’s base class you make use of __FILE__ to register hooks. That value is the same in all instances, so not useful. Also for some of the hooks you need some other value. So perhaps start with something more lightweight first, e.g. w/o the activation /uninstall routines and get it to work. Just two pages in the admin, their menu entries etc.. Step by step.

    Then take a look into the Breadcrumb NavXT plugin. It does something similar, it has an AdminKit base class the concrete option page extends from. I personally won’t say it’s perfect, but this might be interesting for you to read:

    Also I suggest you not only wrench everything into a base-class but that you more create objects as you need them to keep things more apart and flexible:

    Hope this helps a bit to get things back on the run.