Vannkorn

Full Stack Web Developer

Close

Creating a Child Theme in WordPress + A bunch of Security Functions

Create a child theme to prevent changes lost. I've also added some security functions for future usage.

Ads: Register now via this link to receive $100 credit from Vultr

Child theme has become one of the best practices in inheriting styles from another WordPress theme, mostly for themes you purchased or installed from the WordPress theme directory. This is to prevent overriding to changes you have made once new updates are available.

To do so, simply create another folder next to your theme folder. Use the name as [your-theme-name]-child. For example, your theme is wimple then your child theme for it is wimple-child.

Inside the wimpo-child folder create 2 files. 1 named style.css and another one named functions.php.

Edit the style.css file and paste in the following code:

/*
Theme Name: Wimple Child
Theme URI: http://themecountry.com/themes/wimple
Author: ThemeCountry Team
Author URI: http://themecountry.com
Template: wimple
Description: Wimple is a really clean and fast loading WordPress theme designed specially for professional blog. With many color scheme design option to choose, Wimple is also very optimized for user reading experience and boosting advertising revenue.
Version: 2.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wimple
Tags: black, white, gray, green, orange, red, pink, theme-options, editor-style, two-columns

This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.

Wimple is based on Underscores http://underscores.me/, (C) 2012-2014 Automattic, Inc.

Resetting and rebuilding styles have been helped along thanks to the fine work of
Eric Meyer http://meyerweb.com/eric/tools/css/reset/index.html
along with Nicolas Gallagher and Jonathan Neal http://necolas.github.com/normalize.css/
and Blueprint http://www.blueprintcss.org/
*/

Next, create a functions.php file and add in the following code:

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

}
?>

Learn more at https://codex.wordpress.org/Child_Themes

If we want to add security over to the main theme, then inside functions.php, paste in the following codes:

/************************
* Security
***********************/
/* Block from reading wp-config.php file */
$transient_name = 'wce_block_' . $_SERVER['REMOTE_ADDR'];

$transient_value = get_transient( $transient_name );

if ( $transient_value !== false ) {

    die( 'BANNED!' );

}

if ( isset( $_GET['wp_config_enumeration'] ) ) {

    set_transient( $transient_name, 1, DAY_IN_SECONDS );

    die( 'BANNED!' );

}

/* Disable User Agent for WP Scan */
if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) && preg_match( '/WPScan/i', $_SERVER['HTTP_USER_AGENT'] ) ) {

    die( 'WP Scan is blocked in this site!' );

}

/* Remove strange XML-RPC server info. */
function add_fake_xmlrpc() {

    // We don’t want to display die(‘XML-RPC server accepts POST requests only.’); on $_GET
    if ( !empty( $_POST ) ) {

        return 'wp_xmlrpc_server';

    } else {

        return 'fake_xmlrpc';

    }

}

class fake_xmlrpc {

    function serve_request() {

        // It's fake ?
        die();
    }

}

add_filter( 'wp_xmlrpc_server_class', 'add_fake_xmlrpc' );

/* Remove Generator information */
add_filter( 'the_generator', 'remove_generator' );

function remove_generator() {

    // Return nothing
    return ' ';

}

remove_action( 'wp_head', 'wp_generator' );

/* Remove version number from stylesheet */
function remove_version_number_from_css() {

    global $wp_version;

    $wp_version = '168.0';

}

add_action( 'init', 'remove_version_number_from_css' );

/* Prevent advanced fingerprinting */
if ( isset( $_GET['advanced_fingerprinting'] ) ) {

    switch ( $_GET['advanced_fingerprinting'] ) {

        case ‘1’:

        // Unpack file
        $file = gzopen( ABSPATH . 'wp-includes/js/tinymce/wp-tinymce.js.gz', 'rb' );

        // Add comment
        $out = '// ' . uniqid( true ) . "\n";

        while ( ! gzeof( $file ) ) {

            $out .= gzread( $file, 4096 );
        }

        // Pack again
        header( 'Content-type: application/x-gzip' );

        echo gzencode( $out );

        break;

        default:

            status_header( 404 );
        }

    die();

}

/* Stop plugin enumeration. */
if ( isset( $_GET['plugin_enumeration'])) {

    // Display something random

    die( '' );

}

/* Prevent username enumeration. */
if ( ! is_admin() && isset( $_REQUEST[ 'author' ] ) ) {

    status_header(404);

    die();

}

Last but not least, copy the screenshot.png file from the main theme and paste into the child theme and you’re ready to go.

Leave a Reply

Your email address will not be published. Required fields are marked *