The Simplest WordPress User Access Log Ever

Those of use who develop using pods often find we use it for everything. So here’s a quick tip on using PodsCMS to create a custom user access log.

Step 1: Set up the Pod

I’m assuming you’ve already installed/activate both the PodsCMS and Pods UI plugins. If not, please do so before starting.

Create a new Pod called “logins”.

By default each pod is created with a name and slug field. We’re going to use the name field but you can delete the slug field.

Then you’ll need to create a field for “date”. Of course, Pods stores the date any entry is created in a field called “created” which you can access from within Pods Templates. But it still makes sense to have a date field in the Pod itself, if nothing else for the sake of a clear data model.

So once you have added the date field your Pod will look like this:

Step 2: Add function

Now just add the following code to your functions.php file.

add_action('set_logged_in_cookie','mpv_add_access_log_entry');
function mpv_add_access_log_entry($user) {
$user = explode('|',$user);
$log = new PodAPI(); 
$params = array('datatype'=>'logins');
$params['columns'] = array('name'=>$user[0],'date'=>date('Y-m-d H:i:s'));
$log->save_pod_item(pods_sanitize($params));
}

This function uses the Pod API to insert a row in the logins table. Alternatively you can use the $wpdb class and do something like this:

global $wpdb;
$wpdb->insert($wpdb->prefix.'pod_tbl_logins',array('name'=> $user[0],'date'=> date('Y-m-d H:i:s')));

The only trouble with going this route is that in order to use the pods admin interface to manage the data, you’ll also need to add a row to the wp_pod table. This will change with Pods 2.0 so there’s no need to demonstrate. But I strongly recommend using the PodsAPI class as it will make sure to implement best practices and in 2.0 it will use the $wpdb class anyway.

So that’s it. To create an exportable report of the logins just install the “Exports and Reports” plugin. Or you can use PodsUI to create a custom interface.

Have fun.

Add tag-like functionality to your pod inputs

Yet another update: The code below expects the name of your pick field to correspond to the name of the related pod type ( i.e. a pickfield name “genre” should relate to a pod called “genre” , as opposed to “file_type”).

Warning: My code highlighter is gimping up the code a little. If you plan to copy/paste, please download the text file instead of copying straight from the webpag.

Try the DEMO
(login using guest/guest).

Update: The demo was down yesterday because I was playing with some code. Sorry about that.

Update 2:
Also note that if this input helper isn’t working on your site make sure that you’ve enqueued the “suggest” script which ships automatically with WordPress. Do this by adding the following line of code to your functions.php file:

if (!wp_script_is('suggest', 'queue') && !wp_script_is('suggest', 'to_do') && !wp_script_is('suggest', 'done'))  {
        wp_enqueue_script('suggest');
}

So this isn’t really a helper so I’m not going to add it to the PodsCMS helper gallery. However, I think it’s a useful bit of code for pods developers.

The problem: You prefer using pods for developing content that needs lots of relationships managed seamlessly. Post types and tags just won’t cut it. But man, you miss that slick tag UI that WordPress has where you just start typing, hit enter and POW the tag is added. Pods doesn’t have anything like this natively.

The solution:

First create a new input helper and paste in the following code:


Add
x  

This helper creates an input field and calls up some jQuery magic to make it auto-complete. But in order for this magic to work you need to add a function to your functions.php (or a plugin file if that’s the case) and hook it to the wp_ajax_{$action} hook. For more about this hook check Gary Cao’s article.

add_action('wp_ajax_pods_suggest','ajax_pod_search');
function ajax_pod_search() {
if(isset($_GET['q'])) {
$str = $_GET['q'];
if(strlen($str) < 2) { die; }
$pod = $_GET['datatype'];
global $wpdb;
$query = $wpdb->prepare("SELECT name FROM {$wpdb->prefix}pod_tbl_{$pod} WHERE name LIKE '%s'", '%'.like_escape($str).'%'); 
$results = $wpdb->get_col($query);
echo join( $results, "n" );
} 
die;
}

Now simply deploy the input_helper on your chosen multi-pick field pod.

But wait! It’s not saving your pod items. That’s because PodsCMS expects to receive a list of comma-separated pod ids to register the relationship. But our jQuery is outputting the name instead.

So we now need to add a pre-save helper to manage migrating tag names to ids.

save_pod_item(array('datatype'=>$field, 'columns' => array('name'=>$name)));
} else {
$pid = (is_array($pid)) ? $pid[0] : $pid;
}
$vals[] = $pid; 
endforeach; 
} else {
$pid = pod_check($field, $name, 'name'); 
if(!$pid) 
{ 
  $new_tag = new PodAPI();
  $pid = $new_tag->save_pod_item(array('datatype'=>$field, 'columns' => array('name'=>$name)));
} else {
$pid = (is_array($pid)) ? $pid[0] : $pid;
}
$vals = $pid; 
} 
$val_str = implode(',',$vals);
$columns[$field]['value'] = $val_str;
endif;
?>

Note that we have to tell the helper the field to which we want it to apply. We do this by setting the $field variable. If you are going to use the input helper on multiple fields, you’ll want to create a pre-save helper for each field. This helper will iterate over the submitted pod names, check to see if that pod exists already and getting its id. If the pod name does not already exist, the helper will create it.

Note that this helper makes use of a function called “pod_check”. You will need to add this function to your functions.php file as well.

function pod_check($datatype, $value='', $field='id') {
    global $wpdb;
    $datatype = pods_sanitize($datatype);
    $field = pods_sanitize($field);
    $return = $wpdb->get_col($wpdb->prepare("SELECT `id` FROM `{$wpdb->prefix}pod_tbl_{$datatype}` WHERE `$field` = ".('id'==$field?"%d":"%s"),array($value)));
    if (true == $return && 0 < $return) {
    return $return;
    } else {
    return false;
    }
}

This function actually does the work of looking up the pod id.

And that's it. You can make any pod into a "tag" using this approach. But keep in mind that if you are using this field to relate a pods with required fields, you will need to modify the pre-save helper to add those required fields.

WordPress PodsCMS Widgets

I’m not sure why it took me this long to put this plugin together, but whatever. I’ve been using WordPress PodCMS plugin for a long time now. And quite often I need to add pod stuff to a sidebar. Usually, I’d go find a custom sidebar that I’d made before and adapt it. But then I realized how super easy it would be to have a widget that allows you to select which Pod you want to show and which template you want to show it with !!! so bang!

WordPress PodsCMS Widgets – Version 0.1

WordPress Real Estate Plugin in the Works

Update: If anyone wants an email when this plugin is ready just leave a comment  here. Or shoot me an email at mike(at)www.mikevanwinkle.com. Or subscribe to my feedburner.

Hmmm. So what they hell have I been doing with my time lately. Not blogging, clearly. Well first there’s the day job, I’m trying to keep it :-). Then I’ve been working on a Real Estate plugin built on the PodsCMS framework. (Which incidentally is about to launch version 1.90) Why a plugin based on a plugin? Well Pods creates the power on the database side to build a better real estate listing system than would be possible using custom fields.

Real Estate listings have LOTS of fields, i.e. List Price, Agent, Square Footage, Neighborhood, etc. Sure you could use WP’s native taxonomies for some of these fields, but there are still going to be a lot of Custom Fields like # of beds, # of baths, etc. And try putting together a standard WP Query to filter for 20 custom fields. Damn. I’m just not that much of a pro I guess.

So the plugin will take the power of the pods framework and package it up in a way that you won’t really have to learn anything about pods in order to use it. I’m also adding some custom functions to create listings, display related listings, etc. Hopefully it will prove a valuable plugin to small to mid-size real estate companies looking to showcase their listings.

Full credit, I started the plugin as part of a freelance project for Big Sea Design. Woot! Woot!

WordPress PodsCMS SEO Titles Plugin

I haven’t submit this plugin to the WordPress SVN yet, because I’m still playing with it. The remaining issue is that while it works, I’m not sure it works in the most efficient manner possible. I’m also going to post it over at @podscms and get some feedback.

At any rate, for anyone who’s running a pods installation and having trouble getting SEO friendly titles on your pod pages, this plugin creates a template tag <?php pods_seo_title(); ?> that you can use to override wordpress default behavior. The plugins check to see if the page is a pod page, and if so calls the title of the respective pod. If not, it checks to see if the page is singular and returns the TITLE | SITENAME format if it is. Otherwise it returns the SITENAME | SITE DESCRIPTION form.

Download: Pods SEO Titles