Forums

I don't think "register_activation_hook" is working, how do I debug. (2 posts)

  1. Our-party
    Member
    Posted 3 years ago #

    Hi all,

    I'm trying to learn to write a wordpress plugin by setting myself a goal of witting a user generated glossary plugin after I asked people on twitter what would be useful (well I may as well use my learning experience to be useful for more than just me).

    Anyway, on installation the plugin sets-up a database table, and adds some test data to it, then when the content is displayed a foreach loop changes each phrase and replaces it with a DHTML floaty box.

    the problem is however, I can't work out whats going on with the register_activation_hook, it may be being called and the SQL is failing or it may not be being called (either way I don't have an extra table in the database after I activate the plugin)

    the hook looks like this

    register_activation_hook(__FILE__, "bot_install");

    And the bot_install code like this

    function bot_install()
    {
        global $wpdb;
        $table = $wpdb->prefix."sh_gloss";
    
        $structure = "CREATE TABLE $table (
            id INT(9) NOT NULL AUTO_INCREMENT,
            phrase VARCHAR(80) NOT NULL,
            desc VARCHAR(255) NOT NULL,
    	UNIQUE KEY id (id)
        );";
        $wpdb->query($structure);
    
        // Populate table
        $wpdb->query("INSERT INTO $table(phrase, desc)
            VALUES('Scott Herbert', 'Rockstar Programmer')");
    
    }

    OK so firstly please forgive the ego database entry, it's just for testing...

    Secondly is there something I should have Sean that I've missed? and thirdly (and most importantly) how can I debug "bot_install"? can I just add statements like

    echo "in xxxx";

    or will that mess up the headers (since I guess all this code is ran before the main output)

    Thanks in advance
    Scott

  2. mrthrust
    Member
    Posted 3 years ago #

    Ive been looking into developing a few plugins, and also i find that the register_activation_hook is missing something.
    I've not bothered using the Global variables,

    // What happens when the plugin is activated
    register_activation_hook(__FILE__, 'pluginName_activate');

    below is the function which is run on Activation, it will create a categories table (which it dose after checking phpmyadmin)

    function pluginName_activate() {
    	// Create New Table
      $query = mysql_query("CREATE TABLE pluginName_cat(
    	catID INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    	catName VARCHAR( 20 ) NOT NULL ,
    	catDescription TEXT NOT NULL
           ) ENGINE = MYISAM COMMENT = 'Holds the Category Information';");
    // End Query
    
    $query = mysql_query("INSERT INTO pluginName_cat VALUES ('','catName','Description text is here..'); ");
    }// END pluginName_activate

    After de-activating; I have it set to drop the tables it created, then after re-activating the plugin the tables are created but no values are inserted into the table?

    I'm I missing something here also? would I need to use the Global variables,

Topic Closed

This topic has been closed to new replies.

About this Topic