On Ustream: WordPress Plugin to Capture Email Addresses in Exchange For a File Download

Update: No longer streaming. The plugin works great. Kim is going to make it pretty, then I’ll post to WordPress and do a right up.

I’m streaming live right now while I work on a WordPress plugin. Follow me at ustream.tv/channel/stranger-studios.

I’ll be working on a plugin for a feature that we’ve done a million different ways: asking for a user’s email address in exchange for a link to a file download. A solid plugin for this will save us a lot of time. If it turns out well, I’ll put it in the WP plugin directory and link to it from here.

Here are the requirements/specs I have for this plugin:

  • Should use a short code of the form [filedownload: /path/to/file.txt] to define where the form should go and what file.
  • The form will submit to the current page, log the email entered, add a session flag.
  • If an email is in $_POST, the post/page will show the download link instead of the email form.
  • Files will be hidden behind a script to obscure the path to the file. The script will check for the session flag before returning the file.
  • Should use a template to make it easy to change the HTML/CSS for the form.
  • Should be easy to adjust the addEmail code to work with Constant Contact/etc.

That’s it. Please join me. Post questions to the chat. Again, if this code comes out clean, I hope to share it.

Enhanced by Zemanta

Unit of Measure PHP Class

For a recent project, we developed a php class that made it easier to work with units of measure. I was surprised that there wasn’t anything like this available out there, so we decided to open source the code once it was in a good state. That day has come.

We will be updating this code (and this blog post) over the next few days and likely down the road as well. But in the meantime, here is a link to the GitHub repository: phpUOM on GitHub.

(If this goes well, we’ll hopefully be open sourcing a bit more of our code in the future.)

Hidden 404 Errors with WordPress Plugin Pages

After a couple hours, I’ve tracked down and fixed a bug I was having with some of our WordPress plugins. I believe that there are a few people out there having the same problem. I think there may be another solution online, but it is one of those issues that is difficult to pare down to a good search query.

Anyway here is a solution for “404 issues with plugin pages” or “lynx shows a 404, but the page still loads”, or “Google Webtools says there is a 404, but I can get to the page”, or “setting status to 200:OK still results in 404″, or “I get a 404 in IE, but refreshing the page brings it up”, or “I get random 404 errors in IE”, or “I’m getting an HTTP/1.1 404 Not Found error but the page still loads”.

You may skip ahead to the Final Solution code, but it is probably a good idea to read everything below to make sure that you are indeed having the same issue I had… and that this will actually fix your problem.

The Context

I have some WordPress plguins (Stranger Products, Stranger Events) that generate pages outside of the core WordPress system (i.e. they are not “wordpress pages” in the WP DB, they are web pages generated by our plugin script). To serve these pages, I add a bunch of rules to the .htaccess file to redirect stuff like /products/1/ to a product info page.

Some gallery plugins or other plugins that generate new pages may have a similar setup/issue.

The Problem

While the mod rewrite works fine, and the page loads fine, WordPress doesn’t find a WP page or post for the query string and so sends a “HTTP/1.1 404 File Not Found” status in the header. Most web browsers will ignore this and show the content that comes after the header. It seems that IE will sometimes choke on this status, and other times show the page. Funny IE!

Google’s crawler however will not crawl that page and will let you know in a web toolkit report. Also, I noticed that the lynx command line browser for Linux would show the 404 error and then load the page.

The big issue here is that Google is not going to crawl our page.

The Fix

I spent a lot of time tracking down where in the WordPress code the 404 status is set. Ideally, there would be a plugin “hook” near this that we could use to prevent the 404 status from reaching the browser.

The function that makes the 404 decision is handle_404(), which can be found in the /wp-includes/classes.php file. Here is the code (for WordPress 2.8.4, similar for previous versions I looked at too):

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
function handle_404() {
	global $wp_query;
	if ( (0 == count($wp_query->posts)) && !is_404() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) ) {
		// Don't 404 for these queries if they matched an object.
		if ( ( is_tag() || is_category() || is_author() ) && $wp_query->get_queried_object() ) {
			if ( !is_404() )
				status_header( 200 );
			return;
		}
		$wp_query->set_404();
		status_header( 404 );
		nocache_headers();
	} elseif ( !is_404() ) {
		status_header( 200 );
	}
}

This would be the ideal place to say, “Hey, don’t 404 this page”, but there is no hook in here. I tried setting the $wp->did_permalink flag to FALSE, which worked sometimes, but sometimes WordPress would write that back to TRUE after I reset it. And I’m not even sure what that flag is doing; so playing with it might cause some bugs elsewhere.

The next place to check is the status_header() function called by handle_404. This function is found in the /wp-includes/functions.php file. Here is the code:

1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
function status_header( $header ) {
	$text = get_status_header_desc( $header );
 
	if ( empty( $text ) )
		return false;
 
	$protocol = $_SERVER["SERVER_PROTOCOL"];
	if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
		$protocol = 'HTTP/1.0';
	$status_header = "$protocol $header $text";
	if ( function_exists( 'apply_filters' ) )
		$status_header = apply_filters( 'status_header', $status_header, $header, $text, $protocol );
 
	return @header( $status_header, true, $header );
}

Tada! This function uses the “status_header” hook/filter before updating the header. So we can create a function in our plugin to check the status for a 404 and then return false/NULL if we know that there really is a page to load. Here’s how I did it.

The Final Solution

In PHP code for my pages, I created a global variable called $isapage and set it to true. So at the very top of any page that is giving the 404 errors, add this code:

global $isapage;
$isapage = true;

Now I add the following function and filter to my plugin code:

//this function checks if we have set the $isapage variable, and if so prevents WP from sending a 404
function ssp_status_filter($s)
{
	global $isapage;
	if($isapage && strpos($s, "404"))
		return false;	//don't send the 404
	else
		return $s;
}
add_filter('status_header', 'ssp_status_filter');

I hope this helps some people out there.

Feel free to critique this solution. Let me know if I missed something or if there are better ways to do this.

Feel free to post related issues. I may have found solutions to those along the way… or maybe a commenter can help you out.

Editing the StudioPress Lifestyle Theme

A client contracted us to make some updates to the StudioPress Lifestyle WordPress theme. I’m documenting the changes here so our client can possibly do this work themselves in the future, and it may also help others out there looking to tweak the StudioPress (or really any) premium theme.

Changing the Menu Colors

Changing the color scheme involves updating the style.css stylsheet and creating new versions of various images. I did a rough mockup of the color changes in Photoshop, and then went to work on the images.

To change the header and headline elements from blue to orange, I did the following steps to these files:  header.png, header_blank.png, logo_blank.png, headline.png, and topnav.png.

  1. Open the image in Photoshop.
  2. Goto Image –> Mode –> RGB Color to change the mode.
  3. Create a new layer on top of the background.
  4. Use the paint can tool to fill the new layer with your new color.
  5. Change the layer type from “Normal” to “Color”.
  6. (optional) If the brightness of the image is off now, create a new layer filled with the new color and change the layer type to “Luminosity”.
  7. Save the image for web as a PNG 24.

To change the background color of the higlighted/active submenu item, I changed the background color of line 381 (the “#subnav li a:hover, #subnav li:active” declaration).

You can also edit the navbar.png and navhov.png files to change the main menu.

Changing the Header

To change the logo from an image to plaintext, in the WP admin goto Appearance –> Lifestyle Theme Options and change the Header Blog Title setting from “Image” to “Text”.

You can also edit the file in the /images/psds folder to use an Image logo in the header.

Changing the Body/Page Background Colors

I used the Firebug plugin for Firefox to inspect the original website to find out where certain colors and styles were set in the style.css file.

Using Firebug, I learned that the body’s background color is set on line 19. I changed this color from #E2DDCB to #1462A6.

You could also do a search and replace, but this color was only used in one spot in the style.css file.

Moving inward from the background, the next band of color is actually the border of the “wrap” element. I changed the color of the border declaration on line 35 to #6595BF.

Changing Link and Text Colors

The hyperlink colors are declared between lines 40 and 60.  The lifestyle theme uses the same color for links and visited links. I wanted a different visited color, so I changed that section of code to look like this:

a {
color: #1462A6;
text-decoration: none;
}

a:visited {
color: #6595BF;
}

a:hover {
color: #6595BF;
text-decoration: underline;
}

a img {
border: none;
}

a:focus, a:hover, a:active {
outline: none;
}

To change the article heading link colors, search for “#content h1 a, #content h1 a:visited” and “#content h1 a:hover”. (I changed the colors around lines 586 and 595.)

Hiding the Post Meta (“by”, “posted on”, “filed under”, “tags”, etc)

I hid these using CSS. I added a “display: none;” line to the .date  declarations in style.css on line 779. To hide the tags (postmeta2) section, I add these lines under the .postmeta2 declaration around line 841:

.postmeta p,  .postmeta2 p {
display: none;
}

This will hide everything inside the postmeta2 section, but will still show the bottom border and bottom margin.

Changing the Sidebar

I removed the Blog Roll and Admin sections by commenting out lines 9 through 25 of the sidebar_right.php file in the theme directory.

I then added the following widgets:

  • I added a “text” widget to “Sidebar Top” with some Google Adsense code for a 300×250 ad.
  • I added a “text” widget to “Sidebar Top” with the title “Featured Video” and some embed code from YouTube. (Note: it didn’t seem to matter that the YouTube embed width was greater than 300 pixels. YouTube must size it to fit the page.)
  • I added a “recent posts” widget to “Sidebar Top” with the default settings.
  • I added a “text” widget to “Sidebar Bottom Right” with the title “Advertisement” and some code for a custom 120px x 600px ad.

Updating the Homepage

The first thing we need to do is enable the home.php file as a template for our pages. We need to change this line in home.php:

<?php get_header();  ?>

to:

<?php
/*
Template Name: Homepage
*/
get_header();
?>

Save and upload the file. Now edit the “Home” page in WP and choose “Homepage” as the page template. Save the page.

To choose which blog categories are used for the featured content areas, in WP Admin goto Appearance –> Lifestyle Theme Options. Change the appropriate settings.

To get thumbnails to show up in the featured sections, add a custom field called “thumbnail” pointing to an image (70×70 pixels is good) to use. You can upload these images to the post first and then copy the src url.

To set the thumbnails for the last (bottom) featured section, use “hpbottom” as the name of the custom field.

Setting up the Featured Gallery (Fading Script)

The first step here is to install the “featured content gallery” plugin. (It wasn’t included with my install, but it’s a freely available plugin.) In the WP Admin goto Plugins –> Add New. Search for “featured content gallery”. Install the plugin. Activate the plugin.

Now goto Settings –> Featured Content Gallery and choose either the category or page/post IDs to use for the gallery. You must also set a height and width and text area height in the next section (I used 588 width, 400 height, 100 height for the text). I left the colors as default. Update the settings.

The last thing you need to do is make sure you upload a picture (mine were 588×400) and set a custom field called “articleimg” that points to the URL of the image you want to show up. The gallery will not show up unless you set this custom field.

Click here to get a copy of the StudioPress Lifestyle WordPress theme.

Yes You Should Refinance. But How?

With mortgage rates dropping like a brick, it’s becoming a no-brainer for us to refinance our home loan. Even though we just got a 30-year loan 2 years ago at 5.875%, we can get 30-year loans now for around 4.5% or lower. You might be in a similar situation.
Rule of Thumb
The rule of thumb I hear thrown around a lot is that if you can drop 1% off your mortgage rate, you should refinance. To get a more precise idea if refinancing is good for you, you should really take …

“Payback Time” Analysis for GOOG

I still own a few shares of GOOG. It’s felt overpriced recently, but I’m holding onto a minimal amount at all times and trying to add more over time. So I’m hoping the price drops a bunch so I can pick up more cheaply.
Do a search here for GOOG for my previous thoughts (years old), but I basically think that the world will continue to be drowned in data. Google’s goal to organize the world’s information and their expertise at scaling Internet apps puts them in a great position to …

Gold as a Commodity (via Crossing Wallstreet)

Great overview and background on Gold as a Commodity over at Crossing Wallstreet.
The summary for current investors is…
My view is that the Federal Reserve will raise interest rates earlier than expected. I don’t know exactly when that will be but it will put gold on a dangerous path. For now, my advice is to stay away from gold, either long or short.
… but you should read the whole thing for a lot of interesting tidbits on the history of gold and how to track and trade it. Here’s another quote …

Goldman Sachs Traded Profitably EVERY DAY Last Quarter

From DealBreaker.com (via CrossingWallStreet):
Goldman Sachs just revealed in an SEC filing that its traders made money on every single trading day last quarter, a record for the firm. Net revenue for trading was $25 million or higher in all of the first quarter’s 63 trading days with 35 of those days bringing in more $100 million, according to the filing.
That’s pretty amazing. They didn’t have ANY down days? How is this possible? Is the new Goldman Sachs playing it safe? I thought they were doing high risk trades? Were they …

NASDAQ Cancelling Trades After Crazy Day in The Market

Trying to figure out what to think about this: (from BusinessWeek)
The Nasdaq said after markets closed that it will cancel all trades of stocks that moved more than 60 percent from their price at, or immediately prior to, 2:40 p.m., when the slide started. The cancellation applies to trades executed between 2:40 p.m. and 3 p.m.
Were there true “errors” leading to these trades (e.g. running trades that weren’t placed, or trades triggered because a stocks price was listed incorrectly)? Or were there just a bunch of people with stop losses …