{"id":1272021,"date":"2024-01-11T13:34:50","date_gmt":"2024-01-11T13:34:50","guid":{"rendered":"https:\/\/rankmath.com\/?p=1272021"},"modified":"2025-02-06T10:58:44","modified_gmt":"2025-02-06T10:58:44","slug":"block-ip-address","status":"publish","type":"post","link":"https:\/\/rankmath.com\/ja\/blog\/block-ip-address\/","title":{"rendered":"How to Easily Block IP Address in WordPress"},"content":{"rendered":"<p>Are you tired of dealing with unwanted visitors on your WordPress site? <\/p>\n\n\n\n<p>From spam comments and annoying bots to hackers trying to break in, these disruptions can slow down your site and cause you a lot of stress. <\/p>\n\n\n\n<p>The good news is, you can take control. By blocking IP addresses in WordPress, you can protect your site, keep your community safe, and enjoy smoother performance.<\/p>\n\n\n\n<p>In this post, I\u2019ll walk you through how to spot suspicious IPs and the different ways you can block them. So, without any further ado, let&#8217;s get started.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div id=\"toc\">\n    <h2>\u76ee\u6b21<\/h2>\n    <ol>\n        <li><a href=\"#what-is-an-ip-address\">What is an IP Address?<\/a><\/li>\n        <li><a href=\"#reasons-to-block-ip-address\">Reasons to Block IP Addresses in Your WordPress Website<\/a>\n        <\/li><li><a href=\"#identifying-ip-address\">Identifying the IP Addresses to Block in WordPress<\/a><\/li>\n        <\/li><li><a href=\"#methods-to-block-ip-address\">Methods to Block IP Addresses in WordPress<\/a>\n            <ul>\n                <li><a href=\"#block-ip-address-of-commenters\">Block IP Addresses of Commenters<\/a><\/li>\n                <li><a href=\"#cPanel\">Block IP Addresses Using cPanel<\/a><\/li>\n                <li><a href=\"#security-plugin\">Block IP Addresses Using a Security Plugin<\/a><\/li>\n                <li><a href=\"#htaccess-file\">Block IP Addresses by Editing the .htaccess File<\/a><\/li>\n                <li><a href=\"#functions-file\">Block IP Addresses Using the WordPress functions.php File<\/a><\/li>\n           \n            <\/ul>\n        <\/li>\n        <li><a href=\"#conclusion\">\u7d50\u8ad6<\/a><\/li>\n    <\/ol>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-an-ip-address\"><span class=\"number\">1<\/span>  What is an IP Address?  <\/h2>\n\n\n\n<p>Every device that connects to the internet, whether it\u2019s your computer, phone, or tablet, has a unique number called an IP address (short for Internet Protocol address). Think of it like a phone number: just as phone numbers let you call each other, IP addresses let devices find and talk to one another online.<\/p>\n\n\n\n<p>An IP address usually looks like a series of numbers (for example, 192.168.1.1), and it serves two main purposes: identifying a device on a network and showing its location. Whenever someone visits your WordPress site, their IP address gets recorded in your site\u2019s access logs. The same happens when you visit other websites too.<\/p>\n\n\n\n<p>So, if IP addresses are so essential, why would you ever want to block them? That\u2019s exactly what I&#8217;ll dive into next.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"reasons-to-block-ip-address\"><span class=\"number\">2<\/span>  Reasons to Block IP Addresses in Your WordPress Website<\/h2>\n\n\n\n<p>Blocking IP addresses isn\u2019t just for advanced users, it\u2019s a practical step you can take to protect and manage your WordPress site. Let\u2019s look at some common reasons why you might want to block certain IPs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"number\">2.1<\/span>  Preventing Malicious Activity<\/h3>\n\n\n\n<p>If you notice repeated hacking attempts, brute force login attacks, or suspicious behavior from the same IP address, blocking it can stop these threats in their tracks. <\/p>\n\n\n\n<p>This simple action helps protect your site from security breaches and unauthorized access.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"number\">2.2<\/span>  Preventing Spam and Unwanted Emails and Comments<\/h3>\n\n\n\n<p>Spam comments with shady links or irrelevant form submissions can clutter your site and inbox. <\/p>\n\n\n\n<p>By blocking the IPs behind this spam, you can keep your comment sections clean, your inbox manageable, and your visitors focused on meaningful content.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"number\">2.3<\/span>  Managing Unwanted Traffic<\/h3>\n\n\n\n<p>Sometimes, certain IPs, or even whole ranges, can overwhelm your server with excessive requests. These can be <a href=\"https:\/\/rankmath.com\/kb\/bot-traffic\/\">bots<\/a>, crawlers, or even DDoS (Distributed Denial of Service) attacks.<\/p>\n\n\n\n<p>Blocking them reduces server load, improves site speed, and ensures your real visitors have a smooth experience.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"number\">2.4<\/span>  Restricting Access to Content or Company Policies<\/h3>\n\n\n\n<p>You may want to block IPs so only the right audience sees your content.<\/p>\n\n\n\n<p>For example, subscription sites can limit access to paying members. Companies can also block certain IPs so employees don\u2019t visit websites that are against workplace rules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"number\">2.5<\/span>  Geographical Restrictions<\/h3>\n\n\n\n<p>If you want to stop traffic from certain countries or regions, blocking IPs from those areas is one way to do it. This is useful if your service is only available in certain locations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"identifying-ip-address\"><span class=\"number\">3<\/span>  Identifying the IP Addresses to Block in WordPress<\/h2>\n\n\n\n<p>If you&#8217;re dealing with spam comments on your posts or pages, it&#8217;s essential to note that WordPress stores the IP addresses of your website commenters.<\/p>\n\n\n\n<p>To locate this information, simply navigate to the <strong>\u30b3\u30e1\u30f3\u30c8\u30b3\u30e1\u30f3\u30c8<\/strong> page from your WordPress admin area.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"2032\" height=\"1140\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/ip-address.jpeg\" alt=\"locate the IP address in WordPress\" class=\"wp-image-1272049\" \/><\/figure>\n\n\n\n<p>However, in the case of a severe attack impeding access to your admin area, it&#8217;s important to examine your server\u2019s access log. Access this log through your hosting provider&#8217;s cPanel tools. <\/p>\n\n\n\n<p>\u306b\u79fb\u52d5\u3057\u307e\u3059 <strong>Metrics<\/strong> section and select the <strong>Raw Access<\/strong> option, as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1606\" height=\"442\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/raw-access.jpeg\" alt=\"Raw Access\" class=\"wp-image-1272056\" \/><\/figure>\n\n\n\n<p>This action will lead you to the Raw Access Log page, where you&#8217;ll find the raw access log for your domain. Click on the domain to download the access logs file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1608\" height=\"414\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/download-raw-access.jpeg\" alt=\"Download Current Raw Access Logs\" class=\"wp-image-1272057\" \/><\/figure>\n\n\n\n<p>By clicking on your domain, you&#8217;ll download your server\u2019s log file in .gz archive format. Proceed to extract the file.<\/p>\n\n\n\n<p>If you&#8217;re having trouble opening this file format on your computer, try converting it to DOC or PDF using online tools. You can then download the converted file to view your raw access logs.<\/p>\n\n\n\n<p>The access log file comprises raw data of all requests made to your website, with each line starting with the IP address responsible for that particular request.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1526\" height=\"694\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/log-file.jpeg\" alt=\"Log file\" class=\"wp-image-1272058\" \/><\/figure>\n\n\n\n<p>It&#8217;s a good idea to keep an eye out for any suspicious IP addresses in the access log, particularly if you notice a certain IP address showing up frequently.<\/p>\n\n\n\n<p>Be careful not to block yourself accidentally, the genuine audience, or search engines from accessing your website.<\/p>\n\n\n\n<p>If needed, use an online <a href=\"https:\/\/nordvpn.com\/ip-lookup\/\" target=\"_blank\" rel=\"noopener\">IP lookup tool<\/a> to get more information about a suspicious IP address before making any decisions.<\/p>\n\n\n\n<p>Now that you know how to identify suspicious IPs on your website, let&#8217;s discuss how to block these IPs from accessing your site.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods-to-block-ip-address\"><span class=\"number\">4<\/span>  Methods to Block IP Addresses in WordPress<\/h2>\n\n\n\n<p>Let&#8217;s discuss five methods to block IP addresses from accessing your WordPress website. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"block-ip-address-of-commenters\"><span class=\"number\">4.1<\/span>  Block IP Addresses of Commenters&nbsp;<\/h3>\n\n\n\n<p>This method allows you to block the IPs from commenting on your WordPress posts or pages.<\/p>\n\n\n\n<p>To implement this, navigate to <strong>Settings \u2192 Discussion<\/strong> from your WordPress dashboard and scroll down to the <strong>Disallowed Comment Key<\/strong>s section.<\/p>\n\n\n\n<p>Ensure that you enter the IP addresses one per line, as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"2560\" height=\"551\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/disallowed-comment-keys-scaled.jpeg\" alt=\"Block IP address of commenters\" class=\"wp-image-1272065\" \/><\/figure>\n\n\n\n<p>Once you&#8217;ve entered the IP addresses, scroll down to the bottom of the page and click on the <strong>\u4fdd\u5b58<\/strong> <strong>Changes <\/strong>\u30dc\u30bf\u30f3\u3002<\/p>\n\n\n\n<p>WordPress will now automatically prevent users with these IP addresses from posting comments on your website. While these users can still visit your site, they&#8217;ll encounter an error message if they attempt to submit a comment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"cPanel\"><span class=\"number\">4.2<\/span>  Block IP Addresses Using cPanel<\/h3>\n\n\n\n<p>This method enables you to block an IP address entirely, providing a robust measure to protect your WordPress site from potential hacking attempts and DDoS attacks. <\/p>\n\n\n\n<p>Log in to your hosting account&#8217;s cPanel dashboard and navigate to the <strong>Security <\/strong>section. Choose the <strong>IP Blocker<\/strong> \u4e0b\u56f3\u306e\u3088\u3046\u306a\u30a2\u30a4\u30b3\u30f3\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1260\" height=\"394\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/ip-blocker.jpeg\" alt=\"IP Blocker\" class=\"wp-image-1272066\" \/><\/figure>\n\n\n\n<p>This will direct you to the IP Blocker page, where you can precisely specify the IP address you wish to block from accessing your website.&nbsp;<\/p>\n\n\n\n<p>You can add a single IP address or an IP range and click the <strong>Add <\/strong>\u30dc\u30bf\u30f3\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1274\" height=\"540\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/add-ip-address.jpeg\" alt=\"Add IP address\" class=\"wp-image-1272067\" \/><\/figure>\n\n\n\n<p>After that, scroll down to view the list of IP addresses you have restricted from visiting your website. You can come back anytime you want to delete or unblock any IPs by clicking the <strong>Delete<\/strong> \u30aa\u30d7\u30b7\u30e7\u30f3\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1270\" height=\"212\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/list-of-ip-address.jpeg\" alt=\"List of IP address\" class=\"wp-image-1272069\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"security-plugin\"><span class=\"number\">4.3<\/span>  Block IP Addresses Using a Security Plugin<\/h3>\n\n\n\n<p>Blocking IP addresses using a security plugin is the easiest method to enhance the protection of your website against potential threats and unauthorized access. <\/p>\n\n\n\n<p>Security plugins like <a href=\"https:\/\/wordpress.org\/plugins\/sucuri-scanner\/\" target=\"_blank\" rel=\"noopener\">Sucuri Security<\/a> help protect your website from malicious attacks. Refer to our tutorial on <a href=\"https:\/\/rankmath.com\/blog\/how-to-install-wordpress-plugin\/\">installing a WordPress plugin<\/a> on your website.<\/p>\n\n\n\n<p>The process typically involves the use of a Web Application Firewall (WAF) integrated into the security plugin. This firewall acts as a barrier between your website and incoming traffic, analyzing each request for potential threats. Suspicious IP addresses, known for malicious activities like hacking attempts, are automatically detected and blocked by the security plugin.<\/p>\n\n\n\n<p>Moreover, you can still block the IP addresses manually using the Sucuri plugin.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1264\" height=\"490\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/sucuri-1.jpeg\" alt=\"Sucuri WP plugin\" class=\"wp-image-1272082\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"htaccess-file\"><span class=\"number\">4.4<\/span>  Block IP Addresses by Editing the .htaccess File<\/h3>\n\n\n\n<p>You can block suspicious IP addresses from accessing your website by adding a few lines of code to your <a href=\"https:\/\/rankmath.com\/kb\/edit-htaccess\/\">website&#8217;s .htaccess file<\/a>.&nbsp;<\/p>\n\n\n\n<p>However, if you lack coding knowledge, we recommend using the previously mentioned methods. <\/p>\n\n\n\n<p class=\"takeaway yellow\"><strong>\u30ce\u30fc\u30c8\uff1a <\/strong>It&#8217;s important to <a href=\"https:\/\/rankmath.com\/blog\/backup-wordpress-site\/\">back up your website<\/a> before adding any code so that you can restore your site if anything goes wrong.<\/p>\n\n\n\n<p>Log in to your FTP client or cPanel and locate the <strong>\u30d5\u30a1\u30a4\u30eb\u30de\u30cd\u30fc\u30b8\u30e3\u30fc<\/strong> within your <code>public_html<\/code> or your site&#8217;s directory. Choose to edit the .htaccess file.<\/p>\n\n\n\n<p>In the .htaccess file, paste the following code at the bottom of the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>deny from &#091;IP Address You Want To Block]<\/code><\/pre>\n\n\n\n<p>Replace the <strong>IP Address You Want To Block<\/strong> with the actual IP address you wish to block, as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"960\" height=\"320\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/block-ip-1.jpeg\" alt=\"replace IP address\" class=\"wp-image-1272101\" \/><\/figure>\n\n\n\n<p>If you have multiple IPs to block, continue adding the code with each IP address.<\/p>\n\n\n\n<p>Rank Math makes it very simple to <a href=\"https:\/\/rankmath.com\/kb\/edit-htaccess\/\">edit your .htaccess file<\/a>. To edit your .htaccess file, go to<strong>\u00a0Rank Math SEO \u2192 General Settings \u2192 Edit .htaccess<\/strong>. If this section is not available on your website, then ensure that you\u2019ve\u00a0<a href=\"https:\/\/rankmath.com\/kb\/switch-from-easy-to-advanced-mode\/\">switched to the Advanced Mode<\/a>\u00a0Rank Math\u3067\u3002<\/p>\n\n\n\n<p>If you want to edit the .htaccess file, click the checkbox below the text. The text area below will become active, and you can edit your .htaccess file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"2310\" height=\"1616\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2025\/08\/edit-htaccess-file-1.jpg\" alt=\"Edit .htaccess file in Rank Math\" class=\"wp-image-1588524\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"functions-file\"><span class=\"number\">4.5<\/span>  &nbsp;Block IP Addresses Using the WordPress functions.php File<\/h3>\n\n\n\n<p>This method requires adding custom code into the functions.php file of your active WordPress theme.<\/p>\n\n\n\n<p>Below is a code snippet that demonstrates how to block an IP address using functions.php. The code utilizes an array to keep track of blocked IP addresses. It checks whether the visitor&#8217;s IP is in the array before granting access.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function block_ip_addresses() {\n    $blocked_ips = array(\n        '192.168.0.1',\n        '10.0.0.1',\n        '123.456.789.0'\n    );\n\n    if (in_array($_SERVER&#091;'REMOTE_ADDR'], $blocked_ips)) {\n        wp_die('Access Denied');\n    }\n}\nadd_action('init', 'block_ip_addresses');<\/code><\/pre>\n\n\n\n<p>Add as many IP addresses as needed using the style from the <code>$blocked_ips<\/code> array. If you have only a few, delete the other IPs and input the IPs you want to block.<\/p>\n\n\n\n<p>Access your active theme\u2019s functions.php at <strong>Appearance \u2192 Theme File Editor<\/strong>. Choose functions.php, scroll to the bottom, and paste the code as shown.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"2560\" height=\"918\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/add-code-scaled.jpeg\" alt=\"Add code in functions.php file\" class=\"wp-image-1272118\" \/><\/figure>\n\n\n\n<p>Once you&#8217;ve added all the IPs you want to block, simply click the <strong>\u30d5\u30a1\u30a4\u30eb\u306e\u66f4\u65b0<\/strong> \u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u5909\u66f4\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>After successfully applying this code, the specified IP addresses will be denied access. The code utilizes the <code>wp_die<\/code> function displaying the message &#8220;Access Denied&#8221; to the blocked user, as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1252\" height=\"278\" src=\"https:\/\/rankmath.com\/wp-content\/uploads\/2024\/01\/access-denied.jpeg\" alt=\"Access Denied message\" class=\"wp-image-1272137\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"number\">5<\/span>  \u7d50\u8ad6<\/h2>\n\n\n\n<p>Keeping your WordPress site safe from hackers, spam, and unwanted traffic is important if you want it to run smoothly. The good news is, you have different ways to block IP addresses, whether that\u2019s using a plugin, editing your <code>.htaccess<\/code> file, or setting up a firewall.<\/p>\n\n\n\n<p>Just remember, blocking the wrong IP can lock out real visitors or even potential customers. So, always double-check before you block an address.<\/p>\n\n\n\n<p>When you use this feature wisely, blocking IPs can give your site an extra layer of protection, improve performance, and create a safer experience for your visitors.<\/p>\n\n\n\n<p>If you like this post, let us know by <a href=\"https:\/\/twitter.com\/rankmathseo\" target=\"_blank\" rel=\"noopener\"><strong>@rankmathseo \u3092\u30c4\u30a4\u30fc\u30c8\u3057\u307e\u3059\u3002<\/strong><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Are you tired of dealing with unwanted visitors on your WordPress site? From spam comments and annoying bots to hackers trying to break in, these disruptions can slow down your site and cause you a lot of stress. The good news is, you can take control. By blocking IP addresses in WordPress, you can protect your site, keep your community safe, and enjoy smoother performance. In this post, I\u2019ll walk you through how to spot suspicious IPs and the different ways you can block them. So, without any further ado, let&#8217;s get started.<\/p>","protected":false},"author":37,"featured_media":1290432,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[136],"tags":[],"class_list":{"0":"post-1272021","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-wp-tutorials"},"_links":{"self":[{"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/posts\/1272021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/comments?post=1272021"}],"version-history":[{"count":4,"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/posts\/1272021\/revisions"}],"predecessor-version":[{"id":1588515,"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/posts\/1272021\/revisions\/1588515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/media\/1290432"}],"wp:attachment":[{"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/media?parent=1272021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/categories?post=1272021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rankmath.com\/ja\/wp-json\/wp\/v2\/tags?post=1272021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}