WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] register_activation_hook - doesn't work for new sites (6 posts)

  1. johnstonphilip
    Member
    Posted 3 years ago #

    I am making a plugin that needs a custom database. I followed the directions on this page:

    http://codex.wordpress.org/Creating_Tables_with_Plugins

    But when a new user signs up for a site, the database is not created for that site. I checked in phpMyAdmin and it was not created.

    Is there another hook I should be using?

  2. johnstonphilip
    Member
    Posted 3 years ago #

    P.S. if I try to call the function straight out in the plugin (so it gets called everytime wordpress is loaded) I get this error:

    "Call to undefined function wp_get_current_user() in /home/public_html/wp-includes/capabilities.php on line 1059"

    However, I don't get that error if I use the register_activation_hook - but again it doesn't call the function for new accounts. That only calls the function for existing accounts and only if I deactivate and reactivate the plugin everytime.

  3. johnstonphilip
    Member
    Posted 3 years ago #

    I have a little more information after messing around (still no solution yet)

    Again, I am running a Multi-Site version of WordPress 3.0.1

    I activated the plugin that creates the table logged in as the Super Admin - however I activated it while being in the "backend" of "sub-site" #3.

    The table is created for that account ONLY.
    Screenshot from PHPMyAdmin: http://grab.by/5IRt

    All other accounts show this error in their backend:

    Fatal error: Call to undefined function wp_get_current_user() in /home/muswap/public_html/wp-includes/capabilities.php on line 1059

    So the plugin appears to work for only the account that is activating the plugin. The rest of the accounts show an error.

    Any ideas why?

  4. I activated the plugin that creates the table logged in as the Super Admin - however I activated it while being in the "backend" of "sub-site" #3.

    So the plugin appears to work for only the account that is activating the plugin. The rest of the accounts show an error.

    Any ideas why?

    Yep. You;re trying to do network-wide functions is why. either network-activate the plugin or drop it in mu-plugins so it runs on all sites all the time.

  5. johnstonphilip
    Member
    Posted 3 years ago #

    Hey Andrea - I had already tried both of those and that did not work. But I did find a solution:

    The db delta function just needs to be in a different file than upgrade.php

    Copy the function called "dbDelta" in the "wp-admin/includes/upgrade.php" directory

    Paste that entire function (it's quite a few lines of code) into your plugin and rename it something like dbDelta2

    Then in your function to create a new table change these lines:

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql0);

    To:

    dbDelta2($sql0);

    That seems to have fixed the problem.

  6. johnstonphilip
    Member
    Posted 3 years ago #

    Oh also, register_activation_hook doesn't work but I did get a function working (thanks to http://wptoy.com/tips-tricks/create-a-plugin-with-its-own-custom-database-table/)

    Here's the complete function:

    #To allow this to be as extensible as possible, make sure $table_prefix is globalised, we also need the $wpdb class functions too
    global $table_prefix, $wpdb;
    
    #Create the 'name' of our table which is prefixed by the standard WP table prefix (which you specified when you installed WP)
    $wp_track_members_table = $table_prefix . "wp_track_members";
    
    #Check to see if the table exists already, if not, then create it
    if($wpdb->get_var("show tables like '$wp_track_members_table'") != $wp_track_members_table) {
    
    $sql0 = "CREATE TABLE <code>&quot;. $wp_track_members_table . &quot;</code> ( ";
    $sql0 .= " <code>page_load_id</code> int(11) NOT NULL auto_increment, ";
    $sql0 .= " <code>todays_date</code> date NOT NULL default '0000-00-00', ";
    $sql0 .= " <code>todays_time</code> time NOT NULL default '00:00:00', ";
    $sql0 .= " UNIQUE KEY <code>page_load_id</code> (<code>page_load_id</code>) ";
    $sql0 .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; ";
    
    #We need to include this file so we have access to the dbDelta2 function below (which is used to create the table)
    //require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta2($sql0);
    }
    
    # The Hook - you can choose which hook you want to use, for this instance, we'll run this script every time the footer is loaded
    add_action('wp_footer', 'createtable_wp_track_members');
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.