6 Apr, 2010
In a recent comment, we were asked if there is a way to do the custom single page based on the custom author page. Depending on your Wordpress version, this may or may not be implemented already, but either way, the solution can be found below:
global $wp_query;
$curpost = $wp_query->get_queried_object();
if(file_exists('single-'.$curpost->ID.'.php'))
{
include('single-'.$curpost->ID.'.php');
}
else
{
//Default single page
}
The basic theory is the same, get the page ID. Check if single-ID.php exists. If it does, include it. If not, do default author code.
6 Apr, 2010
Recently, I was trying to track down large files on a Linux server. Here’s an easy way to find files larger than a given size in the current directory.
It doesn’t get much easier than that.
30 Mar, 2010
After years of using passive FTP I figured I should find out what it actually does compared to Active FTP.
The difference is in Active, your client tells the server what port you will be doing your business on. Active has worked fine for years, usually on port 21. However with more and more firewalls there had to be a different way. This is where passive mode comes in. In Passive FTP, the client asks the server what is the best port for the server to use. The server responds with a non-blocked port and you can go ahead and transfer.
As a note, in my experience with ftp, if you are getting errors trying to connect. First check all the host/login information. If that is all inputted correctly its a pretty safe bet you need to use passive ftp.
29 Mar, 2010
For the most part, business start-up costs and organizational costs must be deducted over a 180-month (15 year) period. For example, if your start-up costs were $18,000, and you started your business on August 1, 2009, you would be able to deduct $500 in 2009 for your start-up costs. ($18,000 divided by 180 months = $100 per month. If started in August, the business will be operating for five months in 2009.)
24 Mar, 2010
I have recently been listing items for sale onto Google Base through XML Feed created by a custom PHP script. There was an issue that I was having trouble fixing however. All my items were at the bottom of search results. Many items that were not even the same thing being searched for were being listed before mine. Meanwhile the number 1 item listed was usually a grand comparison of major sellers displaying their price for it. From there you can filter the results. My goal was to list my electronic goods right next to Best Buy or Newegg and have a better price.
After searching the internet for the answer I got no where. From there I had hunch it had to be based on some sort of identifier that groups all of these together. With this idea in mind I found some Google documentation on adding what’s called a GTIN, basically a unique identifier for a certain product. This can be UPC, ISBN, SKU among others. I eventually added a <g:upc></g:upc> tag to my feed and the next day I was right with the big boys. The difference in traffic is absolutely tremendous and has transferred into a big sales jump.
The GTIN is not required. But you should add this for your benefit.
23 Mar, 2010
I came across a few queries in our newest project and noticed a bunch of them injected their variable data inside curly braces.
I come to find out you can use this in any string in php to encapsulate whatever data you need. Whether it is a simple variable like $x = 0 or a part of an array or object. I happen to never be aware of this since I usually achieve the same results by popping out of the string to add my data, ex. $x = “My favorite baseball team is “.$fav_baseball_team.”!”. It really comes down to aesthetics but still interesting to know.
15 Mar, 2010
I’m considering using FPDF as a solution to creating PDF’s on Debian since FDFlib isn’t installed. It isn’t as efficient as the native PDFlib, but apparently is sufficient for most things. I’m impressed so far.
If interested, this is actually installable through apt, though I’ll probably just include it on a per application basis. It makes applications more portable.
Here’s some sample code from the FPDF website.
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
23 Feb, 2010
Reading a CSV in PHP is much easier than I anticipated with the advent of the fgetcsv() function for PHP 4. The function basically reads a file line by line and performs and explode on the delimiter, returning the array. Here’s some sample code:
$length = 0;
if ( ($handle = @fopen($file, "r")) !== FALSE )
{
while (($row = fgetcsv($handle, $length, "\t")) !== FALSE)
{
var_dump($row); //dumps an array corresponding
}
}
The length parameter is option as of PHP 5.0.4. Using a limited line length makes execution slightly faster, so if it is realistic to limit line length (even if limiting it at a great value) it could improve script execution time significantly.
The function also allows for setting the enclosure and escape characters.
14 Jan, 2010
I needed to offer a client the ability to send Email using PHPmailer and Gmail. The below is what I came up with, which worked flawlessly:
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$body = "Message Body";
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SetFrom('from@address.com', 'First Last');
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('email@address.com', 'First Last');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
The part that threw me for a loop was right here:
$mail->Host = "ssl://smtp.gmail.com";
I was using simply smtp.gmail.com. I knew from setting up mail clients that you needed to use SSL, but it didn’t occur to me to tell PHPmailer to do the same. When it came to me, simply looking up how to instruct PHPmailer to use SSL did the trick.
11 Jan, 2010
Just created a wordpress funciton in_tree($a, $b) which checks to see if page ID $a is in the tree of page ID $b. That is, it checks to see if $a is a child of $b or $a is $b.
/**
* Checks to see if a $in_tree_id is in the tree of
* $in_tree_root post (is a child of or is the post
* ID provided)
*/
function in_tree($in_tree_id, $in_tree_root)
{
if($in_tree_id == $in_tree_root)
return true;
if(!get_post($in_tree_id)->post_parent)
return false;
return in_tree(get_post($in_tree_id)->post_parent, $in_tree_root);
}