Skip to content

Instantly share code, notes, and snippets.

@indikatordesign
Last active February 11, 2023 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save indikatordesign/fb029aaa38173f0f9ad11aaa3685dcc3 to your computer and use it in GitHub Desktop.
Save indikatordesign/fb029aaa38173f0f9ad11aaa3685dcc3 to your computer and use it in GitHub Desktop.
WP-Config extended and enabling SMTP via PHPMailer WordPress
You don't need to leave the "wp-config.php" file in the root directory, you can put it one folder above.
Create the following structure under vhosts:
mydomain/public_html/*all-wp-files-and-folders
Put the "wp-config.php" in "mydomain", to the folder "public_html". In the host settings root the domain to "public_html".
If no plugins need write access to the "wp-config.php", set the permissions to 444, otherwise to 644.
Translated with www.DeepL.com/Translator (free version)
<?php
// DB Settings
define( 'DB_NAME', 'db-name' );
define( 'DB_USER', 'db-user-name' );
define( 'DB_PASSWORD', 'dp-password' );
// Host settings
define( 'DB_HOST', 'localhost' );
// define( 'DB_HOST', '127.0.0.1:3306' ); //# netstat -lnp | grep mariadb --> tcp .. 127.0.0.1:3306 .. LISTEN .. /mariadbd
// define( 'DB_HOST', 'localhost:3306' );
// define( 'DB_HOST_SLAVE', '127.0.0.1:3306' );
// Charset to UTF-8
define( 'DB_CHARSET', 'utf8' );
// DB - Collate type
define( 'DB_COLLATE', 'utf8_unicode_ci' );
// DB - Prefix
$table_prefix = 'unique_pref_';
// Security Keys in a Network
// Shared
define( 'AUTH_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
define( 'SECURE_AUTH_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
define( 'LOGGED_IN_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
define( 'AUTH_SALT', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
define( 'SECURE_AUTH_SALT', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
define( 'LOGGED_IN_SALT', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
// Different (optional) ==> if domain xy then this else..
define( 'NONCE_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
define( 'NONCE_SALT', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
// Cookie Domain, shared Cookie ==> One login for the whole Network
// Set Cookie Datas
define( 'COOKIE_DOMAIN', 'yourdomain.com' );
define( 'COOKIEPATH', '/' );
define( 'SITECOOKIEPATH', '/' );
define( 'ADMIN_COOKIE_PATH', '/wp-admin' );
define( 'COOKIEHASH', md5( 'yourdomain.com' ) );
// Cookie names.
define( 'USER_COOKIE', 'wordpressuser_' . COOKIEHASH );
define( 'PASS_COOKIE', 'wordpresspass_' . COOKIEHASH );
define( 'AUTH_COOKIE', 'wordpress_' . COOKIEHASH );
define( 'SECURE_AUTH_COOKIE', 'wordpress_sec_' . COOKIEHASH );
define( 'LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH );
define( 'RECOVERY_MODE_COOKIE', 'wordpress_rec_' . COOKIEHASH );
// Shared Users table
define( 'CUSTOM_USER_TABLE', $table_prefix . 'users' );
define( 'CUSTOM_USER_META_TABLE', $table_prefix . 'usermeta' );
// Enable Cache
define('WP_CACHE', true);
// Enable GZIP
define('ENFORCE_GZIP', true);
// Compress CSS files
define( 'COMPRESS_CSS', true );
// Compress JS files
define( 'COMPRESS_SCRIPTS', true );
// Concatenate JS files
define( 'CONCATENATE_SCRIPTS', true );
// No auto updates
define( 'WP_AUTO_UPDATE_CORE', false );
// Disable all WordPress auto-updates
define( 'AUTOMATIC_UPDATER_DISABLED', true );
// CORS Header
header( 'Access-Control-Allow-Origin: https://cdn.yourdomain.com' );
// Disallow file editing in the backend
define( 'DISALLOW_FILE_EDIT', true );
// Disallow file editing AND updates ==> be careful how you use it
define( 'DISALLOW_FILE_MODS', FALSE );
// Allow editing images to replace the originals
define( 'IMAGE_EDIT_OVERWRITE', true );
// Disable WP-Cron-Jobs to activate them on server
define( 'DISABLE_WP_CRON', true );
// Limit the number of post revisions
define( 'WP_POST_REVISIONS', 3 );
// Set Interval for Autosave
define( 'AUTOSAVE_INTERVAL', 9999999 );
// Turn on trash for media files
define( 'MEDIA_TRASH', true );
// Set default theme
define( 'WP_DEFAULT_THEME', 'twentytwentyone' );
// Empty recycle bin automatically
define( 'EMPTY_TRASH_DAYS', 10 );
// Memory Frontpage
define( 'WP_MEMORY_LIMIT', '256M' );
// Memory Backend
define( 'WP_MAX_MEMORY_LIMIT', '512M' );
// Blog- and Siteaddress
define( 'WP_HOME', 'https://yourdomain.com' ); // Main Blog in a Network
define( 'WP_SITEURL', 'https://yourdomain.com' ); // Main Blog in a Network
// Change languages
define( 'WPLANG', 'de_DE' );
define( 'WP_LANG_DIR', dirname(__FILE__) . 'wordpress/languages' );
// Force SSL in admin area
define('FORCE_SSL_ADMIN', true);
// Force SSL on Login Pages
define( 'FORCE_SSL_LOGIN', true );
// Multisite
// define( 'WP_ALLOW_MULTISITE', true );
define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', false );
define( 'DOMAIN_CURRENT_SITE', 'yourdomain.com' ); // Main Blog in a Network
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );
define( 'WP_DEFAULT_THEME', 'divi' );
// Define SMTP Credentials
define( 'PREFIX_Mail_HOST', 'smtp-out.hostername.com' );
define( 'PREFIX_Mail_USER', 'mail-user-name' );
define( 'PREFIX_Mail_PSWD', 'password' );
// Control Outgoing Requests
define( 'WP_HTTP_BLOCK_EXTERNAL', true );
define( 'WP_ACCESSIBLE_HOSTS', '*.wordpress.org, *.github.com, *.yourdomain.com, *.elegantthemes.com, vimeo.com, *.youtube.com, *.google-analytics.com, *.google.com, *.googleapis.com, *mailchimp.com' ); // Add domains you need to connect for updates etc.
// Allow WordPress to update files permission
define( 'FS_METHOD', 'direct' );
define( 'FS_CHMOD_DIR', ( 0755 & ~ umask() ) );
define( 'FS_CHMOD_FILE', ( 0644 & ~ umask() ) );
// Paths to your content, plugins and templates
define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
define( 'WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins' );
define( 'PLUGINDIR', 'wp-content/plugins' );
define( 'WPMU_PLUGIN_DIR', WP_CONTENT_DIR . '/mu-plugins' );
define( 'WPMU_PLUGIN_URL', WP_CONTENT_URL . '/mu-plugins' );
define( 'MUPLUGINDIR', 'wp-content/mu-plugins' );
define( 'TEMPLATEPATH', get_template_directory() );
define( 'STYLESHEETPATH', get_stylesheet_directory() );
define( 'UPLOADS', WP_CONTENT_DIR . '/uploads' );
// With this you can also change the wp-content folder name
// define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/public_html/data-main' );
// define( 'WP_CONTENT_URL', 'https://manage.indikator-design.com/data-main' );
// define( 'UPLOADS', 'data-main/uploads' );
// Define unfiltered uploads
define( 'ALLOW_UNFILTERED_UPLOADS', true );
// Disallow unfiltered_html for all
define( 'DISALLOW_UNFILTERED_HTML', true );
// Enable WP_DEBUG mode
define( 'WP_DEBUG', 1 );
if ( WP_DEBUG )
{
// Save Queries inside database and that array can be displayed to help analyze
define( 'SAVEQUERIES', true );
// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', 1 );
// debug.log to wp-content
define( 'WP_DEBUG_LOG', true );
// Don't show errors on frontend
define( 'WP_DEBUG_DISPLAY', false );
// Magic switch for local dev
// define( 'WP_LOCAL_DEV', true );
} // end if
// Disable most of WordPress ==> faster for testing
define( 'SHORTINIT', false );
// Use WordPress without Themes
define( 'WP_USE_THEMES', true );
// Turn off WSOD Protection (and don't send email notification)
define( 'WP_SANDBOX_SCRAPING', true );
// var_dump to file WP_CONTENT_DIR . '/dump.log'
if ( ! function_exists( 'writeDump' ) )
{
function writeDump( $log, $name = false )
{
$separator1 = '';
for ( $i = 0; $i <= 53; $i++ ){ $separator1 .= '#'; }
ob_start();
echo '---***--- Custom var_dump: ---***---' . "\r\n\r\n";
echo '### Start' . $separator1 . "\r\n\r\n";
if ( function_exists( 'current_filter' ) )
{
echo 'Current Filter: ' . current_filter();
echo "\r\n\r\n";
} // end if
echo 'Output: ' . ( $name ? $name : '' );
echo "\r\n\r\n";
var_dump( $log );
echo "\r\n";
echo '### End '. $separator1 . "\r\n\r\n";
file_put_contents( WP_CONTENT_DIR . '/dump.log', ob_get_clean(), FILE_APPEND | LOCK_EX );
} // end writeDump
} // end if
// Example to use
// writeDump(
// [
// '$variable1' => $variable1,
// '$variable2' => $variable2,
// 'someFunction' => someFunction(),
// ],
// 'functionToTest' );
// var_dump to an overlay on bottom in your browser
if ( ! function_exists( 'writeFooter' ) )
{
function writeFooter( $var, $noAdmin = false )
{
if ( ! $noAdmin && ! current_user_can( 'administrator' ) )
return;
$hook = is_admin() ? 'admin_footer' : 'wp_footer';
add_action( $hook, function() use ( $var )
{
echo '<div style="display:block;width:100%;position:fixed;bottom:0;z-index:2000000000;background:#fff;padding:0 20px;max-width:100%;max-height:100%;overflow-y:scroll"><pre>';
var_dump( $var );
echo '</pre></div>';
},999);
} // end writeFooter
} // end if
// Relocate temporary your login and admin to another domain
// define( 'RELOCATE', true );
// Repair Database ==> uncomment if you have some errors
// define('WP_ALLOW_REPAIR', true); // use it as needed with https://domain.com/wp-admin/maint/repair.php
// Absolute path to the WordPress directory
if ( ! defined( 'ABSPATH' ) ) { define( 'ABSPATH', __DIR__ . '/' ); }
// Sets up WordPress vars and included files
require_once ABSPATH . 'wp-settings.php';
<?php
if ( ! defined( 'ABSPATH' ) ) die( 'Nothing to find Ma\'am..' );
add_action( 'phpmailer_init', function( $phpmailer )
{
// If you want to use custom HTML wrappers for your emails, include them
// include( 'templates/email-wrapper-html.php' );
// include( 'templates/email-wrapper-plain.php' );
$phpmailer->Priority = 3;
$phpmailer->Mailer = 'smtp';
$phpmailer->From = 'online@yourdomain.com';
$phpmailer->FromName = 'Company Name';
$phpmailer->Sender = $phpmailer->From;
$phpmailer->Host = PREFIX_Mail_HOST;
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->Port = 465;
$phpmailer->SMTPAuth = true;
$phpmailer->Username = PREFIX_Mail_USER;
$phpmailer->Password = PREFIX_Mail_PSWD;
$phpmailer->AddReplyTo( 'contact@yourdomain.com', $phpmailer->FromName );
// Just if you want to use own templates
// $reason = 'your activity on yourdomain.com';
// $date = 'From ' . date_i18n( get_option( 'date_format' ) ) . ' at ' . date_i18n( get_option( 'time_format' ) );
// $search = [ '%content%', '%reason%', '%date%' ];
// $replace = [ $phpmailer->Body, $reason, $date ];
// $message = str_replace( $search, $replace, $emailWrapperHTML );
// $messageAlt = str_replace( $search, $replace, $emailWrapperPlain );
$phpmailer->Body = $message;
$phpmailer->AltBody = $messageAlt;
});
If don't want to set your email credentials to "wp-config.php", you can write the credentials in an object and save it encrypted to your DB.
<?php
if ( ! defined( 'ABSPATH' ) ) die( 'Nothing to find Ma\'am..' );
/**
* Class Description
*
* @since 1.0.0
*/
if ( ! class_exists( 'helperMethods' ) )
{
class helperMethods
{
/**
* Encrypt/Decrypt sensitive data
*
* @since 1.0.0
*/
public function processKey( $key, $vector, $data, $decrypt = false )
{
$met = 'AES-256-CBC';
$key = hash( 'sha256', $key );
$vec = substr( hash( 'sha256', $vector ), 0, 16 );
if ( ! $decrypt )
return base64_encode( openssl_encrypt( $data, $met, $key, 0, $vector ) );
else
return openssl_decrypt( base64_decode( $data ), $met, $key, 0, $vector );
} // end processKey
} // end class
} // end if
// Write the creds with a small script once to db and remove it
$creds =
[
'host', 'smtp-out.hostername.com',
'user', 'mail-user-name',
'pass', 'password'
];
$crypt = ( new helperMethods )->processKey( NONCE_KEY, NONCE_SALT, json_encode( $creds ) );
update_option( 'my_mail_creds', $crypt, true );
// Get your creds
$get = get_option( 'my_mail_creds' );
$creds = json_decode( ( new helperMethods )->processKey( NONCE_KEY, NONCE_SALT, $get, true ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment