0

Okay = been working on the plugin I posted about over here, and having am having a couple troubles.

I decided to go the route of writing a plugin from scratch. As I noted, its an customized ajax gallery / portfolio, and I'm trying to work its management into wp-admin. The goal is finally to do the query/sorting with wp, and deliver xml or json back to the httprequest via this plugin.

First things first though - when the plugin is first activated I'm trying to create a new table, and insert some test data to it (I'm following the wordpress codex guidelines.) When the plugin is successfully activated, however, nothing is happening. I'm getting no errors (anymore), but the table is not getting inserted, and no data is being added.

I realize the codex script is procedural and I'm using a class, but I think everything is scoped properly. Actually. I don't know if it is, specifically the global $wpdb. I'm still pretty new to php - it seems to have some scoping quirks that I don't quite grok yet.

At any rate - this is what I've got so far. Very simple, but no go (yet).

<?php  
    /* 
    Plugin Name: B99 Portfolio
    Plugin URI: http://www.joshbosworth.com/
    Description: B99 Portfolio data logic
    Version: 0.1
    Author: Josh Bosworth
    Author URI: http://www.joshbosworth.com 
    */  

    global $b99_pf_db_version;
    $b99_pf_db_version = "1.0";

    if ( !class_exists( 'B99_Portfolio')){
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    class B99_Portfolio
    {
        public function B99_Portfolio(){}

        public function __construct(){
            add_action('admin_menu', array( &$this, 'b99_portfolio_admin_actions' ));
        }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //              admin 
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        public function b99_portfolio_admin_actions(){
            add_media_page( 'B99 Portfolio', 'B99 Portfolio',  'manage_options', 'B99_Portfolio', array( &$this, 'b99_portfolio_admin'));
        }

        public function b99_portfolio_admin(){
            if (!current_user_can('manage_options'))  
                {
                        wp_die( __('You do not have sufficient permissions to access this page.') );
                }
                include('b99-portfolio-admin.php');
        }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //              db table 
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        var $table_name;

        public function b99_pf_install(){  
            global $wpdb;

            $this->table_name = $wpdb->prefix . "b99_pf";

            $sql = "CREATE TABLE " . $this->table_name . " ( //changed to class var
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
                title tinytext NOT NULL,
                cat tinytext DEFAULT NOT NULL,  //changed from 'default'
                tag tinytext NOT NULL,
                note text NOT NULL,
                date tinytext NOT NULL,
                hr_path tinytext NOT NULL,
                lr_path tinytext NOT NULL, 
                UNIQUE KEY id (id)
            );";

            dbDelta($sql);

            add_option("b99_pf_db_version", $b99_pf_db_version);
        }

        public function b99_pf_install_data(){
            global $wpdb;

            $title  = 'Testing Furiously';
            $cat    = 'illustration,design';
            $tag    = 'sketch';
            $note   = 'Seriously Testing';
            $date   = 'june 2008';
            $hr_path= 'hr/testing.furiously-hr.jpg';
            $lr_path= 'lr/testing.furiously-lr.jpg';

            $rows_affected = $wpdb->insert( $this->table_name, array( 'time'  => current_time('mysql'), //changed table_name to class var
                                                                'title' => $title,
                                                                'cat'   => $cat,
                                                                'tag'   => $tag,
                                                                'note'  => $note,
                                                                'hr_path'=>$hr_path,
                                                                'lr_path'=>$lr_path
                                                               ) );
        }


        } //end class
    }

    if( class_exists(B99_Portfolio)){
        $b99_PF = new B99_Portfolio();

        register_activation_hook(__FILE__, array(&$b99_PF, 'b99_pf_install'));
        register_activation_hook(__FILE__, array(&$b99_PF, 'b99_pf_install_data'));
    }

    ?>

Any advice would be most excellent. Frankly - and differing or contrary take on my overall plan here would be fine, too.

Thanks in advance.

btw. How the hell do I debug wordpress? I'm using netbeans / xampp...

= update =

Ok - was able to get xdebug up and running (thanks!). I have a couple more questions about this code.

-Just general php - I'm a little puzzled over variable scope

$classvar // implied public modifier?

private function someFunction(){
$classvar // let me get this straight - this is a new local var 
$classvar !=$this->classvar? // right? if so thats an important clarification...
}

-$wpdb

why

global $wpdb
$rows_affected = $wpdb->insert(...

and not just

global $wbdb->insert(...

-register_activation_hook / wordpress db rebuild.

I notice the activation hook gets run each time the admin page gets loaded, and not only when the plugin is 'activated' which seems like the logical behavior. And it seems as tho wp-query rebuilds, or at least runs dbDelta on the entire wordpress database each rebuild of the admin page also? Not that this seems wrong, but I'm not sure I understand why. I should note- I'm just using myPhpAdmin to see if the table is getting added into the db structure.

Clearly I've spent my whole career working client side ;) Although I've made use of a lot of php features in the past, this may be the first time I've attempted anything but 'simple' server side routines or copy/pastes. At any rate - thanks for the help -

1 Answer 1

1

I see two problems-

$this->table_name = $wpdb->prefix . "b99_pf";

$sql = "CREATE TABLE " . $table_name . " (

$table_name will be undefined, should be $this->table_name

and in your query:

cat tinytext default NOT NULL

I don't think it'll like that default in there.

EDIT - to answer your debug question- xdebug, xdebug and netbeans

5
  • +1 for xdebug. kinda a duh on my part - but its working nicely. Very cool to introspect wordpress at runtime. ... thanks for the feedback - but still no table. See my update.
    – Bosworth99
    Jun 4, 2011 at 17:00
  • @Bosworth99 - one more - if( class_exists(B99_Portfolio)){ B99_Portfolio needs quotes.
    – Milo
    Jun 4, 2011 at 17:11
  • sorry - just posted my update. will look into the quotes. The class is clearly getting instantiated, tho.
    – Bosworth99
    Jun 4, 2011 at 17:18
  • @Bosworth99 - there's still an error in the query, sorry I wasn't clear on that one- default shouldn't be there at all, or it needs a value, case doesn't matter. default gives the field a default value, but you haven't passed any value with it. your code otherwise works as-is, the table is created. also check out the php docs on variable scope. I don't have anough experience with plugin activation to speak to that.
    – Milo
    Jun 4, 2011 at 17:27
  • !! and yes, it does get built. Most excellent. Thanks a bunch for your time. That gets me going.
    – Bosworth99
    Jun 4, 2011 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.