WordPress.org

Plugin Directory

wiki:Usermeta

Usermeta

This plug-in adds an API for WordPress 1.5, or provides an alternative API for WP 2.0 so that other plug-ins can associate arbitrary meta information to users very easily.

Installation

  1. Upload to your plugins folder, usually wp-content/plugins/
  2. Activate the plugin on the plugin screen
  3. Click "Create/Update usermeta tables" in Options -> Usermeta

Examples

Get the Usermeta object

      $usermeta = get_usermeta_object();

Associate something with a user

      $usermeta->set($user_ID, "myplugin_thingy", "myvalue");

Get the value for the author of a post (in the Loop)

      $val = $usermeta->get(get_the_author_ID(), "myplugin_thingy", true);
      if (is_null($val)) {
        do default action when no meta data
      } else {
        echo $val
      }

Adding more info to a post…

      <p>This post written by <?php the_author(); ?> who lives at
      <?php $usermeta = new Usermeta(); echo $usermeta->get(get_the_author_ID(), "address", true); ?></p>

Add a few bits of information to the current user

      global $user_ID;
      get_currentuserinfo();
      $usermeta->add($user_ID, "myplugin_listofthings", "myval_1");
      $usermeta->add($user_ID, "myplugin_listofthings", "myval_2");
      $usermeta->add($user_ID, "myplugin_listofthings", "myval_3");

Fetching user info for comments

      <?php $usermeta = new Usermeta(); echo $usermeta->get($comment->user_id, "address", true); ?></p>

API

  $bool = $usermeta->database_exists()

    Check to see if our tables exist, returns true if so
  $out = $usermeta->ensure_database()

    Ensure that the usermeta table has been created.  Returns an array
    of messages of actions taken, if any.  (Comes from Wordpress' dbDelta
    function.)
  $bool = $usermeta->add($user_id, $key, $value)
  $bool = $usermeta->add($user_id, $key, $value, $unique)

    Adds in the given value for this user/key when unique is false (or not
    passed in).  If unique is true, a check is made to see if the user/key
    already exists and if it does, no new entry is added.  If anything was
    added true is returned, false otherwise.
  $bool = $usermeta->delete($user_id, $key)
  $bool = $usermeta->delete($user_id, $key, $value)

    If no value is given, deletes all values for the given user/key.  If a
    value is given, only tries to delete that value.  If anything was
    deleted, returns true.
  $bool = $usermeta->get($user_id, $key, $single)

    If single is false, returns an array of all values for this user/key,
    however if single is true, returns the first found entry for this
    user/key or NULL if there are none.
  $bool = $usermeta->set($user_id, $key, $value)

    Sets all occurances of user_id/key to the given value.  If no such
    user_id/key exists, a new entry is added. Always true on success.
  $bool = $usermeta->update($user_id, $key, $value)
  $bool = $usermeta->update($user_id, $key, $value, $prev_value)

    Updates all occurances of this user/key with the new value.  If optional
    fourth param is passed in, then only matches with that value are changed.
    Returns true if any items were changed, false otherwise.

License

Copyright (c) 2005 James Ponder <james@squish.net>

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

WordPress Upgrades

Upgrading from WordPress 1.5 to 2.0

If you used Usermeta 0.1 or 0.2 you MUST run this BEFORE you upgrade to WP 2.0:

ALTER TABLE wp_usermeta CHANGE meta_id umeta_id bigint(20) NOT NULL auto_increment

You do not need to do this with Usermeta 0.3 which is compatible with both versions.

Failed upgrades to WP 2.0

Unfortunately due to some interaction between WP 2.0 and the tables which were used up in Usermeta 0.1 if you upgrade to WP 2.0 having used an old version of Usermeta it will fail to correctly create the necessary data in wp_usermeta. You may see the error:

WordPress database error: [Key column 'umeta_id' doesn't exist in table]

The simplest fix is to restore your database and then follow the instructions above. The problem any other way is that you need to go through the upgrade process again to populate the wp_usermeta table with WP 2.0 parameters (if you really want to try, see upgrade_160() in WordPress API.

Usermeta upgrades

Upgrading to Usermeta 0.3 from previous versions

Run this before you upgrade:

ALTER TABLE wp_usermeta CHANGE meta_id umeta_id bigint(20) NOT NULL auto_increment

If you upgraded to WP 2.0 you will have done this already.

You then need to modify your theme to use get_usermeta_object() instead of get_usermeta().

Support

Old: http://log.squish.net/2005/09/20/usermeta-userextra/ New: http://log.squish.net/2006/02/05/userextra-goes-wp20/

Last modified 8 years ago Last modified on 07/23/06 16:55:09

Attachments (1)

Download all attachments as: .zip