Exactly how that is done will vary by your specific situation and needs, so I can't give you a specific script, but it is possible to limit access to portions of your site to only specific IPs, by using the mod_rewrite.c module. How it is done exactly needs to be carefully considered, because it can cause usability issues with other users of your site, and may vary depending on what attack vector you are concerned about.
You might also review Hardening WordPress for more ideas on keeping your site safe. The latest version is actually fairly safe without resorting to fancy .htaccess tricks. Simply using good web hygiene is really the most important security measure.