How To: Get the Current Logged in User ID in WordPress

When developing custom themes and plugins for WordPress there are times you will need to get the logged in user’s ID. There are a few different ways to accomplish this, but I’m going to show you the easiest method. To get the user’s ID you’ll use the get_current_user_id() function like so:

$current_user_id = get_current_user_id();
echo 'Your User ID is: ' .$current_user_id;

The get_current_user_id() function will return the currently logged in user’s ID, or 0 if a user is not logged in. Another popular method, which requires a bit more code, is the get_currentuserinfo() function. The method I described above actually uses the wp_get_current_user() function, which is a wrapper for get_currentuserinfo(). So using get_current_user_id() is just a faster method for retrieving the same data. This function was added in WordPress 3.0 as part of the Multisite code merge into WordPress.

To learn more about the get_current_user_id() function check out the Codex article or consult the WordPress core.

How to: 301 Redirect from one domain to another using mod_rewrite

By StrangeWork.com: Recently I created a new E-commerce website for a client of mine. I developed the new site as a subdomain under their existing domain (ex http://newsite.domain.com). Google ended up indexing around 4000 links from the subdomain I had created, so upon launching the new site I wanted to 301 redirect the subdomain links to the primary domain of the new site.

Sounds easy right? Of course! I quickly found many sites that recommended this rewrite rule using mod_rewrite:

RewriteRule ^(.*) http://domain.com/$1 [R=301,L]

However there is a problem with this method that no article seemed to address. This works great at redirecting from the subdomain (http://newsite.domain.com) to the new domain (http://domain.com), BUT if you navigate to http://domain.com you will hit an infinite loop.

So, how do you correct this problem? Easy, you need to create a rewrite condition to only apply the rule to your old domain or the subdomain in this case. Just add the following two lines to your .htaccess file in the root of your website:

RewriteCond %{HTTP_HOST} ^(newsite.)?domain.com$
RewriteRule ^(.*) http://domain.com/$1 [R=301,L]

Now this rewrite rule will ONLY apply to anyone who hits the old subdomain at http://newsite.domain.com and 301 redirect them to http://domain.com with the entire URL structure in tact. This method is easy to apply to a stand domain as well, just replace newsite in the above example with www.

I was pretty baffled at the amount of bad information there is on this method so thought I would share the proper method with everyone. Hopefully the update rewrite rules will help anyone having the same issues I had.

From Microsoft to Linux….almost

By StrangeWork.com: As most of you know I am in the process of starting my own Web Development Company in New Jersey. Starting a company takes a lot of work, but of course I knew this going into it. Business is great and we are starting to grow!

PHP LogoI’ve decided to shift focus on my technologies track a little. I’ve always programmed with Microsoft technologies including classic ASP, ASP.NET, VB, SQL Server, etc. I’ve been developing dynamic websites with classic ASP for almost 8 years now and I still love that language. I use ASP.NET occasionally when the project calls for it, but I love scripting languages. I’ve decided to focus my efforts on PHP rather than ASP.NET.

Scripting has always come natural to me, so why focus on a more object-oriented language? Exactly, so from this point on I will be primarily developing in ASP and PHP. The logic is the same as ASP, just different syntax so the switch won’t be too dramatic.

Expect to see some sweet PHP applications rolling out in the coming months!

The PHP Anthology Book from SitePoint

By StrangeWork.com: SitePoint.com is at it again. Fresh off the press and in my hands is the new SitePoint book The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition.
PHP Anthology Book
As most of you know I primarily use Microsoft technologies for developing, but as this blog shows I also like to dabble in the dark arts known as PHP. I’m no PHP expert, so this book will make for great reference material when I’m hacking up plugins or modifying scripts.

As always SitePoint has done an amazing job at making a technical book easy to read, understand, and actually enjoy! I recommend this book for PHP enthusiasts and experts alike!