Make WordPress Hooked Function to Run Only Once

by Yang Yang on December 12, 2012

Consider this hook to add something before your WordPress blog’s sidebar:

function before_siderbar() {
?>
<!-- Something goes here, such as an AdSense links unit -->
<?php
}
add_action( 'get_sidebar', 'before_siderbar' );

It’s usual to add an AdSense links unit there but the problem with some themes is that they run get_sidebar more than once, such as on the sidebar AND in the footer. Consequently, you would end up with multiple AdSense links unit here and there, whereas you only wanted it on the sidebar once.

This could create a TOS problem towards AdSense because unconsciously, you would very probably end up adding more than 3 links unit per page.

To prevent this and only make the hooked WordPress function to run once (even it is called multiple times), use the PHP static variable in functions.

function before_siderbar() {
	static $called = false;
	if (isset($called) && $called) {
		return false;
	}
?>
<!-- Something goes here, such as an AdSense links unit -->
<?php
	$called = true;
}
add_action( 'get_sidebar', 'before_siderbar' );

In the above snippet, the variable $called is kept alive across multiple calls to the before_siderbar() function. After the first successful run of the function, $called would have a value of true and it’s kept so inside the function every time it’s called afterwards. In the 2nd and every call afterwards, $called will always be true and the function returns before outputting the AdSense code.

There you go. Now no matter how many times the before_sidebar() is called, only the first run would output the stuff you put in it.

{ Comments on this entry are closed }

PHP mail() is a great function to easily send emails from your website server. If you have ever used it before in action, or are currently using mail() to send out emails from your website or application, chances are you would find Gmail to be very persistent in attaching a ‘via’ field to the from address of your messages to the recipient. If you are on a shared host or have multiple websites on a VPS, the ‘via’ field would be the domain of a whole different website from that of the sending domain, which makes you very uncomfortable.

So how to make the ‘via’ field disappear in Gmail messages sent from your PHP mail() function? How to make the ‘mailed-by‘ field and the ‘signed-by‘ field to be the email-sending domain rather than the server hostname?

How to make Gmail trust your messages sent from the mail() function?

Get rid of Gmail ‘via’ field for PHP mail() messages and make your domain show up in ‘mailed-by’ and ‘signed-by’

Here are what you need to do to make Gmail completely trusts your domain and your PHP mail() messages sent from it.

1. SPF and DKIM

Firstly, you would need to set an SPF record for the domain you are sending emails from and enable DKIM as well. These are primarily for identifying your messages against spam.

2. "From: anything@yourdomain.com"

Secondly, make sure you are setting the “From: ” header to be an email address on the domain you are sending messages from. Don’t pretend to be someone else. Use “From: someone@abc.com” if you are sending the messages from abc.com, rather than anything else, such as blah@def.com, or yours@gmail.com, or whatever. If you want the recipient to reply to your Gmail email instead of your domain email, use the “Reply-To: ” header. “From: ” must always be the domain email that you are sending the email from.

3. "Return-Path: return@yourdomain.com"

Thirdly and most importantly, set the “Return-Path: ” header to be the same domain as that of the “From: ” header. Use the 5th parameter of the mail() function for this:

mail('recipient@example.com', 'Subject', "Message Body", $headers, '-freturn@yourdomain.com')

So the Return-Path of this message would be “return@yourdomain.com” (the email address immediately following the -f switch). The $headers parameter should contain all the necessary message headers. Make sure “From: ” is something@yourdomain.com.

Now Gmail trusts all emails from yourdomain.com

After these steps and measures, Gmail should now completely trust your messages from yourdomain.com. The ‘via‘ field of your messages should be gone and the ‘mailed-by‘ field as well as the ‘signed-by‘ field should be correctly showing up as yourdomain.com.

Uploaded below is the screenshot of a message sent to my Gmail email from one of my websites (*ses.com) using the mail() function:

make Gmail trust your email

Both ‘mailed-by‘ and ‘signed-by‘ fields are correctly populated with the sending domain even though it is not the primary site nor hostname of the server that sends the email. The ‘via‘ field is also gone.

This site doesn’t have any SSL certificates installed.

Gmail is by far the best spam catcher of all email services so if they trust you, your emails sent by PHP mail() from yourdomain.com should look good in all other email inboxes. Our forum has also got a thread to cover this.

Thanks to Michael Gorven and Laura for the help.

{ Comments on this entry are closed }

It became a total mess when I selected the entire root directory of my project and accidentally clicked Source > Format in Zend Eclipse PDT. All the files were instantly re-formatted and saved in a manner I wasn’t comfortable with and there didn’t seem any easy way to undo these changes that had been applied across nearly a hundred files in different folders.

They definitely should have prompted up a dialog box asking the developer’s confirmation. They probably will in future but for now, I need to do something to prevent this from happening again.

Disable Source > Format

First we will disable / hide this menu item or we will probably accidentally click on it again. Without any prompt nor confirmation, PDT simply goes on formatting the selected items silently. When you are in a single file that would be fine cause’ you just use Ctrl + Z to undo the change, however if you are selecting an entire directory or the entire project, this command would go ahead and format ALL the files under it and automatically SAVE THEM all onto the hard drive. The next thing you find yourself doing is opening and reverting each and every one of the files one by one.

Enough talk. To disable this menu item:

Window > Customize Perspective > Menu Visibility > “Source” > “Format” (disable this) > OK

Disable Ctrl + Shift + F

We should also disable this short cut keys combination which can also be inadvertently treaded on. Go to:

Window > Preferences > General > Keys > Select all “Format” entries and click “Unbind Command”

Done. Now the only way to format a page or a selected portion or a resource / item in PHP Explorer is to right click on the target and select Source > Format.

{ Comments on this entry are closed }

When you copy and paste code into Zend Eclipse PDT they will probably be automatically indented and not in the original manner. It may be annoying sometimes. To disable this behavior:

Window > Preferences > PHP > Editor > Typing > When pasting “Adjust indentation”

Just disable the option.

{ Comments on this entry are closed }

The common practice of rounding, namely the round() function in PHP, is to round to the nearest decimal point, that is, 1.0, 2.0, 3.0, etc. or, 10, 20, 30, etc. For example,

echo round(4.25, 0);		// 4
echo round(3.1415926, 2);	// 3.14
echo round(299792, -3);		// 300000

When I’m trying to aggregate all the customer ratings for a specific provider in one of my web hosting reviews community, I want to round the average rating to the nearest 0.5 (half the decimal) so that a half star would be correctly displayed.

This is more of a mathematical problem than a PHP one. After some thinking and testing, I came up with a slightly more sophisticated solution than but the round() function:

echo round(1.7 * 2) / 2;		// 1.5
echo round(2.74 * 2) / 2;		// 2.5
echo round(2.75 * 2) / 2;		// 3.0
echo round(3.1 * 2) / 2;		// 3.0

Just as easy as that.

{ Comments on this entry are closed }

<Files *.php>
    Order Deny,Allow
    Deny from all
    #Allow from 127.0.0.1
</Files>

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

The 2 <Files> directives must be in the exact same order as above.

{ Comments on this entry are closed }

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

Yeah, that’s it. So:

  1. /path/dir/ is redirected to /path/dir
  2. /path/dir/?a=1&b=2 is redirected to /path/dir?a=1&b=2

{ Comments on this entry are closed }

At any time, press:

Ctrl + Shift + L

For a list of available shortcut keys defined in your Zend Eclipse PDT.

Go:

Window > Preferences > General > Keys

To edit the shortcut keys. Or simply press Ctrl + Shift + L twice.

For Zend Eclipse PDT to instantly show a tool tip hint of what the parameters of a function / method are so you don’t have to look up the original definition, just place the cursor in the parameters parenthesis and press one of them:

Alt + ?

Alt + Shift + /

Ctrl + Shift + Space

Which is defined under the title “Context Information” when you press Ctrl + Shift + L.

If a function or method name is elusive to you, type the beginning part of it and press:

Alt + /

Ctrl + Alt + /

And PDT would try and auto-complete it for you. It is defined under the title “Word Completion” when you press Ctrl + Shift + L.

{ Comments on this entry are closed }

The default array() indentation of Eclipse for PHP Developers when you create an array seems to be 2 tabs which I think are too much. To reduce it to just 1 tab:

Window => Preferences => PHP => Editor => Typing => formatter preference page => Edit… => Indentation => Default indentation of array initializers

Or

Window => Preferences => PHP => Code Style => Formatter => Edit… => Indentation => Default indentation of array initializers

To change the default indentation for wrapped lines:

Window => Preferences => PHP => Code Style => Formatter => Edit… => Line Wrapping => Default indentation for wrapped lines

{ Comments on this entry are closed }

Many’s the time after you have uploaded some PHP script to your server and point the web browser to the address it gives 500 Internet Server Error. If you have suPHP installed this is very likely because the uploaded PHP script (files and directories) have wrong permissions set to them.

With regards to Linux permissions, suPHP requires all directories to be at least 755 and all files to be at least 644 for any PHP script to run. If the directory or the PHP script has the wrong permissions set to them, suPHP would give out 500 Internet Server Error until you have corrected them. In addition, the directory and the PHP script must be owned by the current user and group or they wouldn’t run either.

To fix this is very easy, just perform the following command after you have uploaded the PHP script:

chown -R youruser /home/youruser/public_html/*
chgrp -R youruser /home/youruser/public_html/*
find /home/youruser/public_html/* -type f -exec chmod 644 {} \;
find /home/youruser/public_html/* -type d -exec chmod 755 {} \;

The 1st line sets everything (files and directories) under /home/youruser/public_html/ to be owned by user youruser.

The 2nd line sets everything (files and directories) under /home/youruser/public_html/ to be owned by group youruser.

The 3rd line sets all files under /home/youruser/public_html/ to be 644 in permissions.

The 4th line sets all directories under /home/youruser/public_html/ to be 755 in permissions.

{ Comments on this entry are closed }