Turns out that bash has cache…

All developers, whether they like to admit it or not, will at times waste a huge amount of time trying to figure something out that has seemingly no explanation – and it turns out to just be a caching issue. This terribly common with front end web development.

Today I was installing Node.JS on a server directly from source but made a mistake. I figured, no problem, I’ll just uninstall and reinstall correctly. But the command node --version was returning an error – /usr/local/bin/node: No such file or directory.

After trying to install a million different ways, creating symlinks, and searching high and low for orphaned files… it was a simple caching issue.

Linux bash caches paths of executables. The fix is simple.

hash -d package removes the cache for a specific entry, while hash -r dumps the entire thing.

Saving Authorize.net data to Gravity Forms entry object

I won’t get into all of the details of why I needed this functionality, but I was working on developing an online payment process using Gravity Forms and Authorize.net and the accounting office requested a very, very specific reporting format.

After developing a convoluted system using Google App Scripts to combine, archive, package, and delivery this data on a daily basis from multiple forms on multiple sites – the accounting office requested the CSV data also be merged with transaction data from Authorize.net. It would save them significant amounts of time. (Also, Google App Scripts are REALLY amazing for managing and automating documents and spreadsheets.)

Unfortunately Gravity Forms payment add-ons store payment data in a separate table and then simply call it when you view and entry in the dashboard, but it’s not accessible from the regular GFAPI entry object. Sigh.

So here’s what I came up with. Hopefully another poor soul may find it useful…

Adding custom fields with multiple values to a WordPress post

The problem

I was working to create a system that allowed users to submit a Gravity Form entry with multiple file attachments that would create a post on the front end, printing those attachments. GF and the wonderful Gravity Forms + Custom Post Types plugin makes it easy to map a form field to a custom meta field (new or existing) – however I ran into a snag when trying to display multiple file uploads.  Simply echoing get_post_meta was just returning one of the meta field values, not each value.

Multiple custom field values with same key

The answer

Create a simple loop that stores each of the meta values to a variable, then loop through each echoing out the file links. This loop will need to be added to the post content either via theme template, filtering the_content(), and adding it onto a hook.  With this particular project using Genesis, I added it to the genesis_entry_content hook.

<?php $bulletin_files = get_post_meta( get_the_ID(), 'bulletin_file_upload' );
 if ( $bulletin_files ) : ?>
 <div><ul>
 <?php foreach( $bulletin_files as $url ) { ?>
 <li><a href="<?php echo $url; ?>">
 <?php echo basename($url ); ?>
 </a></li>
 <?php } ?>
 </ul></div>
 <?php endif; ?>

Custom fields looped

A really cool function used in the snippet above is basename(). It’s a simple php function that takes a string for a file path (in this case a url) and just grabs that filename at the end of the string.  Pretty cool and way simpler than some regex solution.

Custom document titles in WordPress 4.4, Genesis, and WP SEO

Adding the global site title to wp_title

As part of a massive WordPress multisite installation, I wanted to force rewrite the document titles of all pages globally on a custom Genesis theme. Genesis includes its own document title filter, all managed through the dashboard of each site. On my installation child site document titles were missing the name of the global parent site.  However, manually entering custom titles via WordPress SEO or Genesis SEO settings would have taken forever on . So my solution was just to add a filter to wp_title() and add the global title where needed. Here’s what I came up with:

/**
* Remove site specific Genesis SEO title settings and creates template for titles globally
*/

 //Stop Genesis from using the SEO settings from dashboard
remove_filter( 'wp_title', 'genesis_default_title', 10, 3 ); 

//Filter default wordpress title to append SAU where needed
function sau_wp_title( $title, $sep ) {
global $blog_id;
$global_details = get_blog_details( 1 );
$global_title = $global_details-&gt;blogname;
$sep = ' | '; 
	
if ( ($blog_id != 1) &amp;&amp; ( !is_home()) &amp;&amp; (!is_front_page() ) ) {
$title = "$title $sep $global_title";
} // end if
return $title;
} 

add_filter( 'wp_title', 'sau_wp_title', 10, 2 );

It worked great… until a week later when I upgraded WordPress to 4.4. The new release replaced wp_title with an entirely new method for theme and plugin developers to customize document titles.

Plugin authors can now check for theme support and have a few new filters available that will allow them to change or replace the title in a reliable way:

  • 'pre_get_document_title' short-circuits wp_get_document_title() if it returns anything other than an empty value.
  • 'document_title_separator' filters the separator between title parts.
  • 'document_title_parts' filters the parts that make up the document title, passed in an associative array.

WordPress 4.4 adds document title filters

Googling around, I found a post that explain these new filters at length and rewrote my snippet to rewrite ALL titles globally just to my liking. Note: I did not change the snippet below to work for any site, as the $tagline variable will need to be rewritten to pull from get_bloginfo.

add_filter('pre_get_document_title', 'sau_pre_get_document_title', 10);
function sau_pre_get_document_title(){
 global $blog_id;
 global $post;
 $blog_title = get_bloginfo();
 $page_title = get_the_title($post-&gt;ID);
 $sep = ' | '; 
 $global_details = get_blog_details( 1 );
 $global_title = $global_details-&gt;blogname;
 $tagline = 'Modern, Affordable, Competitive';

 if ( ($blog_id == 1) ) {
    if (is_home()) {
    $title = "$global_title $sep $tagline";
    return $title;
    }
 
else {
    $title = "$page_title $sep $global_title";
    return $title;
    }
}

if ( ($blog_id != 1) &amp;&amp; ( is_home()) || (is_front_page() ) ) {
    $title = "$blog_title $sep $global_title";
    return $title;
}

if ( ($blog_id != 1) &amp;&amp; ( !is_home()) &amp;&amp; (!is_front_page() ) ) {
   $title = "$page_title $sep $blog_title $sep $global_title";
   return $title;
}
 
else {
   return $title;
}
}

Removing Yoast WP SEO titles

For those that are using Yoast’s WP SEO and want to stop it from forcing itself upon your custom title set up, all you have to do is add the following to your code (renaming the function added to the filter as needed):

add_filter('wpseo_title', 'sau_pre_get_document_title', 15);