Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:32:59+00:00 2026-06-06T17:32:59+00:00

I’m cross-posting this from the wordpress section, b/c I think the problem lies more

  • 0

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:33:01+00:00Added an answer on June 6, 2026 at 5:33 pm

    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:

    • http://plugins.trac.wordpress.org/browser/breadcrumb-navxt/trunk

    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:

    • WordPress Plugin: How do I avoid “tight coupling”?
    • Multiple Custom Metabox Help

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.