Getting rid of .git untracked file errors with git clean

The following untracked working tree files would be overwritten by checkout:

Nothing drives me crazier than trying checkout a git branch and getting this error.

$ git checkout develop
error: The following untracked working tree files would be overwritten by checkout:
	tests/fixtures/5e1c60f7048c2630c76c7e268ed7ad55
Please move or remove them before you can switch branches.
Aborting

This usually happens to me when something in my application creates cache files and the git ignore wasn’t configured. So there are several different ways to solve this depending on what you want to do.

If you want to keep the files just commit them like the warning prompts you to. In this case:

git add tests/fixtures/
git commit -m "Adding fixture files" tests/fixtures/

But sometimes you don’t want to keep them so you delete the file with:

rm r-f tests/fixtures/5e1c60f7048c2630c76c7e268ed7ad55

But then you just get the same error for another file in the same directory. You can’t delete the whole directory because there are some files in it that ARE committed and need to be kept.

What I learned today is that you can use git’s ‘clean’ function to remove these.

git clean -f tests/fixtures/

The -f flag here will force delete all of the untracked files in that directory. Alternatively if you want to be more careful you can use -i for an interactive prompt:

$ git clean -i tests/
Would remove the following items:
  tests/test-input-helper.php   tests/test-site-workflow.php
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers    4: ask each             5: quit
    6: help
What now> 5
Bye.

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.

Meta Tag Titles and Descriptions for Your WordPress site.

There are many good plugins out there the help you easily add meta tags to your site. All-in-one-SEO is probably the most popular of all the options. But there several reason why you may want to manually add this code to your template.

For one, you may be a template designer and want your templates to come pre-installed with this SEO friend feature.

Second, you may be having issues with Facebook link sharing not working. WordPress plugins use the wp_head() hook to access your template. But you have no control over the order. If you are using more than a dozen plugins your meta tags can get buried under tons of other code. This can prevent some bots from finding them. Indeed on several of my site Facebook’s bot was not finding them.

Third, it is just always good to do things without plugins if possible. Call it Occum’s Razor of web site building, the less code you use, the better.

Luck for us, it is extremely simple to add code for meta tags and descriptions without knowing much about wordpress or PHP.

Open the header.php file in your theme and paste in the following code:

<?php if(is_singular()) { ?>
<?php
global $post;
?>
<?php $recent = new WP_query('p='.$post->ID);
while($recent->have_posts()) : $recent->the_post(); ?>
<meta name="title" content="<?php the_title(); ?>">
<meta name="description" content="<?php the_content_rss('', TRUE, '', 50); ?>">
<?php endwhile; ?>
<?php } else { ?>
<meta name="title" content="<?php bloginfo('title'); ?>">
<meta name="description" content="<?php bloginfo('description'); ?>">
<?php } ?>

So what’s going on here? First, we’re checking to see if the post is a single post using the WordPress Conditional tag <?php if(is_singular()); ?> because we’ll to pull the title and the description of the individual post for our meta tags. But to do this, we need to get some information about the post, which is why we use the call global $post. This will give us information about the current post. Particularly it allows us access to $post->ID to query information about the post using WP_query.

The query we us looks like this:

$recent = new WP_query('p='.$post->ID); 

Once we have the query we put it into the standard wordpress loop:

while($recent->have_posts()) : $recent->the_post();

Then we use the standard WordPress tag <?php the_title(); ?> to pull in the META TITLE and <? the_content_rss() ?> to get the description. Notice, we are not using <?php the_excerpt(); ?>. This is because this WordPress tag prints the excerpt with a “read more” link in it. This will seriously screw up your theme. Using <?php the_content_rss(); ?> allows us to specify how many words of the content to pull in. But the second parameter has to be set to TRUE to avoid pulling in a “read more” link.

Pods Tutorial Series Under Way

As promised, today I’ve uploaded some tutorials showing how to use pods to create an events calendar. Pods can be a little tricky and are not recommened for the casual WordPress user. However, if you are familiar with the basics of WordPress theming and are not frightened by a little PHP, I think you will find these How Tos valuable.