With increase in usability of WordPress websites, the risk of getting hacked also increases. So, we need to take certain measures to make our WordPress website secured and reliable.
There are several ways where an attacker can attack your website,
- inject java scripts into all of your posts which will redirect to their websites.
- Modify your WordPress core files
- get access to your WordPress folder and upload shell files and execute them.
- send an infinite amount of requests to your webserver to make it down.
How to recover the hacked WordPress website with out any 3rd party plugins?
- Delete all script tags from post: (PHP script: save it as run.php in wordpress directory and hit it from direct url example.com/run.php)
<?php
ignore_user_abort(true);
$path = $_SERVER['DOCUMENT_ROOT'];
require( $path.'/wp-load.php' );
$args = array(
'post_type' => array(
'post',
'page',
'revision',
'item',
'wpsl_stores',
'nav_menu_item',
'accordion_menu',
'et_pb_layout',
'scheduled-action',
'wpsl_stores',
'popup_theme',
),
'post_status' => array('publish', 'draft', 'inherit'),
'posts_per_page' => -1,
);
$posts = get_posts($args);
echo 'Post count is '.count($posts).'<br>';
foreach($posts as $post){
$content = $post->post_content;
$new_content = strip_tags_content($content, '<script>', true);
$args = array(
'ID' => $post->ID,
'post_content' => $new_content,
);
wp_update_post( $args );
}
echo '<br>Done.';
function strip_tags_content($text, $tags = '', $invert = FALSE) {
return preg_replace('#<script(.*?)>(.*?)</script>#is', '', $text);
}
2. Replace your wp-includes folder with the WordPress core wp-includes folder (you can download it from wordpress.org) [NOTE: Please make sure you download the same version of WordPress which you have installed in your server. And you can replace other files as well if you know those files.]
How to prevent WordPress hacking in future?
1. Set proper file permissions as below:
chmod 644 *
chmod 755 wp-content
chmod 755 wp-admin
chmod 755 wp-includes
cd wp-includes
chmod 644 *
chmod 755 sitemaps
chmod 755 php-compat
chmod 755 ID3
chmod 755 customize
chmod 755 blocks
chmod 755 block-supports
chmod 755 block-patterns
chmod 755 rest-api
chmod 755 SimplePie
chmod 755 Requests
chmod 755 Text
chmod 755 PHPMailer
chmod 755 random_compat
chmod 755 pomo
chmod 755 IXR
chmod 755 assets
chmod 755 theme-compat
chmod 755 js
chmod 755 fonts
chmod 755 certificates
chmod 755 images
chmod 755 css
chmod 755 widgets
chmod 755 block-patterns
cd ..
cd wp-contents
chmod 755 uploads
chmod 644 index.php
cd ..
cd ..
chmod 755 <your main directory> (very important)
2. Install All in WP security plugin. And make your your rating score is 20/20 in each section.

If you need any help recovering your website, feel free to reach out to my WhatsApp +918249675129.