Monthly Archives: July 2014

See if Value Exists in PHP Array

in_array is the function to Check if a value exists in an array using PHP. Here is an example of how it is used:

 

$arr = array("abc", "def", "ghi", "jkl");
if (in_array("abc", $arr)) {
echo "<p>abc is in arr!</p>";
}

How to replace wp_mail Function with PHP mail function in WordPress

Be aware that this modification should probably only be used in extreme cases where you can not get WordPress to send emails any other way because this method requires changes to the WordPress core. Making changes to the core files means that you could loose those changes if you update to a different version of WordPress in the future. If you update, you will have to make the changes again. Luckily it is one simple change to one file, so it's not really that bad. I often prefer to use such a hack instead of adding another plugin to my WordPress blog.

To replace the default wp_mail function with one that users PHP mail, find the pluggable.php file located in wp-includes  and replace that wp_mail function with this one:

<?php
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
//WP mail function code replaced by Ian L of jafty.com:
// Use wordwrap() if lines are longer than 70 characters
$txt = wordwrap($message,70);
//Always set content-type in headers when sending HTML email:
$headersnew = "MIME-Version: 1.0" . "\r\n";
$headersnew .= "Content-type:text/html;charset=UTF-8" . "\r\n";
//Use headers to set from address:
$headersnew .= "From: peter@michaelson.com.au\r\n";
// Send email:
    if(mail($to,$subject,$txt,$headersnew)) {
    //if mail sent to user send notice to admin:
    $adminsubject = "Admin Notification from WP";
    mail('admin@example.com',$adminsubject,$txt,$headersnew);
    } else {
    //if mail didn't go through, send notice to admin:
    $txterr = "<h2>ERROR!</h2> email to user did not go through!";
    mail('admin@example.com',$subject,$txterr,$headersnew);
    }
}//end php mail function for wp-mail
?>

Get Referring URL with PHP

<?php
//get pg referer if there is one
    if (isset($_SERVER['HTTP_REFERER'])) {
    $ref_url = $_SERVER['HTTP_REFERER']; //get referrer
    }else{
    $ref_url = 'No referrer set'; // show failure message
    }//end else no referer set
echo "<h3>Refferring URL: $ref_url</h3>";
?>

Exclude WordPress directory from URL rewrite with .htaccess

I ran across a situation today where I had a WordPress site that had a directory for webmaster tools. The problem was that the .htaccess file for the WordPress site was set up to redirect all pages to a WordPress post or page, so if you tried to go to example.com/blog/tools/tool.php, it wouldn't allow that. Here is the fix:

Add a Line to .htaccess to Exclude a Directory from WordPress Redirects:

Basically your WordPress section of the .htaccess file will look similar to this:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(diamondsearch|diamondsearch/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

So what you do if find the first "RewriteRule" line and add this line below it:

RewriteCond %{REQUEST_URI} !^/(dirName|dirName/.*)$

Simply replace both instances of "dirName" with your directory name that you want to be excluded from WordPress redirects. Your final .htaccess file will look something like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(dirName|dirName/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Mysql Get Highest Value for Column in Table

Today I had a project where I had to find what the highest year was in a database table that contained MLB statistics.  The table contained a column for year and I wanted to see if it was updated for the last year, so I figured out the following query to determine the last year inserted into the table.

MYSQL Query:

SELECT MAX(year) AS year FROM TableName

PHP MYSQL Query:

mysql_query("SELECT MAX(year) AS year FROM TableName") or die(mysql_error())

while($r=mysql_fetch_array($result)){

$yr = $r['year'];

echo "Highest Year Found: $yr<br />";

}

Summary

I hope that helps. I have shown how you can get the highest value of a column in basic SQL which could be done from a PHPMyAdmin query window and how to do it with PHP.  All you have to do in the above examples is replace "year" with the column name containing the value you want to find the maximum of and replace "tableName" with the name of the actual table. Of course in the php example, you would also have to add a database connection string before the code for it to work.