This function, nk_xmlrpc_block(), is a shell function that blocks access to the XML-RPC file on a WordPress site by adding code to the site’s .htaccess file. The function expects a domain name as its input parameter and uses nk_docroot() to determine the document root for the domain. If .htaccess doesn’t exist for the site, an error message is printed, and the function exits. Otherwise, the function checks whether the .htaccess file already has any lines related to XML-RPC. If it does not, the function creates a backup of the .htaccess file, appends the necessary XML-RPC block code to the file, and prints out that it was added. If the .htaccess file already has XML-RPC block code in place, the function simply prints out that the rules are already present.


Just provide a domain and it’ll take care of the rest.

[root@cloudvpsserver ~]# nk_xmlrpc_block nkern.net
xmlrpc block added to /home/nkern/public_html/.htaccess

It will quit if it find xmlrpc rules already present.

[root@cloudvpsserver ~]# nk_xmlrpc_block nkern.net
xmlrpc rules already present.


nk_xmlrpc_block () {
# First we need to know the domain and docroot.
# domain is provided by the user
# docroot is the result of running nk_docroot on $domain.
docroot="$(nk_docroot "$domain")"

# nk_xmlrpc_block expects a domain to be provided. Print error and exit if not.
if [ "$1" = "" ]; then
    echo "You must provide a domain name"
    return 0

# If .htaccess doesn't exist for the site. Print error and exit.
if [ ! -f "$docroot/.htaccess" ]; then
    echo "No .htaccess present."
    return 0

# Now we can assume that the .htaccess file exists.
# Check whether it currently has any lines abour xmlrpc.
if [ "$(grep -c "xmlrpc" "$docroot"/.htaccess)" = "0" ]; then
    # If it does not have any lines to do with xmlrpc then.
    # Make a backup of the .htaccess file with todays date appended.
    cp -a "$docroot"/.htaccess "$docroot"/.htaccess."$(date +%F)"
    # Append the xml block rules to the .htaccess.
    printf "\n<Files xmlrpc.php>\norder deny,allow\ndeny from all\n</Files>\n" >> "$docroot"/.htaccess
    # Print out that it was added.
    echo "Xmlrpc block added to $docroot/.htaccess"
    # Otherwise if the .htaccess already has an xmlrpc block in place. Print out so.
    echo "Xmlrpc rules already present."

Author: Nichole Kernreicht

Created: 2023-04-09 Sun 21:34