Skip to content

Instantly share code, notes, and snippets.

View carl-alberto's full-sized avatar

Carl Alberto carl-alberto

View GitHub Profile
<?php
/**
* Disable RESTAPI
* Using a plugin like: https://wordpress.org/plugins/disable-json-api/
* Feature from Itheme security
* @package wpplugin
*/
add_filter( 'rest_authentication_errors', 'cafunction1_disable_rest_api' );
@carl-alberto
carl-alberto / gist:d069f0e183db6ef01ecc6c8e2d482c44
Created April 22, 2020 09:27
Prevent user enumeration. Add in a custom WP plugin or theme's functions.php
<?php
/**
* Stop user enumeration directly from code.
* Using a plugin like: https://wordpress.org/support/topic/wp-json-user-enumeration-disable/
* If using Wordfence: https://www.wordfence.com/blog/2016/12/wordfence-blocks-username-harvesting-via-new-rest-api-wp-4-7/
* If using Cerber: https://wordpress.org/support/topic/wp-json-user-enumeration-disable/
* Also a feature in Itheme Security https://ithemes.com/ithemes-security-settings-checklist/
*
* @package wpplugin
*/
<?php
/**
* Stop user enumeration.
*
* @package wpplugin
*/
if ( ! is_admin() ) {
// phpcs:disable
if ( preg_match( '/author=([0-9]*)/i', $_SERVER['QUERY_STRING'] ) ) {
if (!is_admin()) {
// default URL format
if (preg_match('/author=([0-9]*)/i', $_SERVER['QUERY_STRING'])) die(); add_filter('redirect_canonical', 'shapeSpace_check_enum', 10, 2);
}
function shapeSpace_check_enum($redirect, $request) {
// permalink URL format
if (preg_match('/\?author=([0-9]*)(\/*)/i', $request)) die(); else return $redirect;
}
@carl-alberto
carl-alberto / gist:e896b8c281f1085a2cd5170cb2be1a2e
Created March 31, 2020 14:02
Additional security headers
/**
* Add security headers for Nginx based sites
*
* @param [type] $headers add security headers as array.
*
* @return array
*/
function additional_securityheaders( $headers ) {
if ( ! is_admin() ) {
$headers['Referrer-Policy'] = 'no-referrer-when-downgrade';
@carl-alberto
carl-alberto / gist:32dfb84d2aa485430ae7a1b848767309
Last active March 21, 2020 23:33
Fully disable WP_CACHE persistent true value for WP-Rocket
// You can add this to a custom plugin or your theme's functions.php
function set_rocket_wp_cache_define_false( $turn_it_on ) {
return 'false';
}
add_filter('set_rocket_wp_cache_define', 'set_rocket_wp_cache_define_false');
<h1>Pantheon MyISAM to InnoDB engine converter</h1>
<?php
/*
* Use this script ONLY if you are a Pantheon customer.
* ONLY RUN THIS SCRIPT IN DEV!
*/
$mysqli = @new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME'], $_ENV['DB_PORT']);
if ($mysqli->connect_errno) {
<?php
# This is a Windows-friendly symlink
require_once WP_CONTENT_DIR . '/plugins/wp-redis/object-cache.php';
@carl-alberto
carl-alberto / wp-config.php
Last active July 31, 2019 19:21
Catch all redirect
if (($_SERVER['REQUEST_URI'] == true) && (php_sapi_name() != "cli")) {
header('HTTP/1.0 301 Moved Permanently');
header('Location: https://'. $_SERVER['HTTP_HOST' );
// Name transaction "redirect" in New Relic for improved reporting (optional).
if (extension_loaded('newrelic')) {
newrelic_name_transaction("redirect");
}
exit();
@carl-alberto
carl-alberto / functions.php
Last active April 22, 2020 22:47
Programmatically change pass
<?php
// You can add this code in the theme's functions.php
$user = get_user_by( 'email', 'user@example.com' ); // change the email that you need to update the password
wp_set_password( '$stronGp$sS', $user->ID ); // chage this to another password if you want
// dont forge to remove this code once you have access to your site
?>