Skip to content

Instantly share code, notes, and snippets.

@EastSideCode
Last active January 24, 2020 15:42
Show Gist options
  • Save EastSideCode/9fcf07672a81de5ba06ce992aaa141f7 to your computer and use it in GitHub Desktop.
Save EastSideCode/9fcf07672a81de5ba06ce992aaa141f7 to your computer and use it in GitHub Desktop.
General WP Functions
// For images
$siteMailLogoURL = NULL;
$siteMainLogoAlt = NULL;
if (isset($siteMainLogoID)) {
$siteMailLogoURL = wp_get_attachment_url($siteMainLogoID);
$siteMainLogoAlt = get_post_meta($siteMainLogoID, '_wp_attachment_image_alt', true);
}
if (!empty($siteMailLogoURL)) : ?>
<img src="<?php echo $siteMailLogoURL; ?>" <?php echo (!empty($siteMainLogoAlt)) ? "alt='$siteMainLogoAlt'" : ''; ?> />
<?php endif; ?>
/**
* for repeaters
*/
for ($j = 0; $j < $repeaterCount; $j++) {
$lowerRightBulletText = $pageCF['repeater_base_' . $j . '_text'][0];
?>
<?php
} // end for loop
/**
* For search and replace first instance
*/
function str_replace_first($search, $replace, $subject) {
$pos = strpos($subject, $search);
if ($pos !== false) {
return substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}
// base functions file:
// change standard login error messages
function escpde_wrong_login() {
return 'Wrong username or password.';
}
add_filter('login_errors', 'escpde_wrong_login');
add_action('template_redirect', 'escode_custom_disable_author_page');
function escode_custom_disable_author_page() {
if ( isset( $_GET['author'] ) || is_author() ) {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers();
}
}
/**
* Removed authors from sitemap
*/
add_filter( 'wpseo_sitemap_exclude_author', function() {
return [];
}, 10, 1 );
/**
* Limit Post Revisions
*/
if (!defined('WP_POST_REVISIONS')) {
define('WP_POST_REVISIONS', 15);
}
/**
* Add Security Headers
*/
add_action('send_headers', function() {
// Enforce the use of HTTPS, if SSL is turned on
if (is_ssl()) {
header("Strict-Transport-Security: max-age=31536000; includeSubDomains");
}
// Prevent Clickjacking
header("X-Frame-Options: SAMEORIGIN");
// Prevent XSS Attack
// header("Content-Security-Policy: default-src 'self';"); // FF 23+ Chrome 25+ Safari 7+ Opera 19+
// header("X-Content-Security-Policy: default-src 'self';"); // IE 10+
// Block Access If XSS Attack Is Suspected
header("X-XSS-Protection: 1; mode=block");
// Prevent MIME-Type Sniffing
header("X-Content-Type-Options: nosniff");
// Referrer Policy
header("Referrer-Policy: no-referrer-when-downgrade");
}, 1);
/**
* Prevent WordPress from generating extra image sizes
*/
function stop_wp_generating_images( $sizes ) {
/* Default WordPress */
unset( $sizes[ 'thumbnail' ]); // Remove Thumbnail (150 x 150 hard cropped)
unset( $sizes[ 'medium' ]); // Remove Medium resolution (300 x 300 max height 300px)
unset( $sizes[ 'medium_large' ]); // Remove Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
unset( $sizes[ 'large' ]); // Remove Large resolution (1024 x 1024 max height 1024px)
return $sizes;
}
add_filter( 'intermediate_image_sizes_advanced', 'stop_wp_generating_images' );
/**
* Show Database queries in footer
*/
function db_performance( $visible = false ) {
$visible = false;
$stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
get_num_queries(),
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
);
echo $visible ? $stat : "<!-- DB STATS ARE {$stat} -->" ;
}
// add_action('wp_footer', 'db_performance', 20);
// EDIT ADMIN FOOTER TEXT
function modify_footer() {
echo 'Created by Louis Fico';
}
add_filter( 'admin_footer_text', 'modify_footer' );
// END EDIT ADMIN FOOTER TEXT
// START ADD TIMESTAMP TO MAIN STYLE TAG
add_filter( 'style_loader_tag', 'add_stylesheet_timestamp', 10, 4 );
function add_stylesheet_timestamp( $html, $handle, $href, $media ){
// only look for main stylesheet
$mainStyleSheet = get_stylesheet_directory_uri() . '/style.css';
// set up a variable that has the file name with the timeppended
$styleWithTime = 'style.css?ver=' . time();
if( strpos( $mainStyleSheet, $href ) !== false ){
$html = str_replace('style.css', $styleWithTime, $html);
}
return $html;
}
// END ADD TIMESTAMP TO MAIN STYLE TAG
// START REMOVE SIDEBAR WIDGETS
add_filter( 'sidebars_widgets', 'disable_all_widgets' );
function disable_all_widgets( $sidebars_widgets ) {
$sidebars_widgets = array( false );
return $sidebars_widgets;
}
/**
* Remove comments from YOAST SEO
*/
function escode_remove_yoast_seo_comments() {
// if the plugin isnt installed then dont do anything
if ( ! class_exists( 'WPSEO_Frontend' ) ) {
return;
}
$instance = WPSEO_Frontend::get_instance();
// if the above is no longer a method, then dont do anything (makes it not break with future yoast updates)
if ( ! method_exists( $instance, 'debug_mark') ) {
return;
}
// the fun part, removing the debug_mark
remove_action( 'wpseo_head', array( $instance, 'debug_mark' ), 2 );
}
add_action('template_redirect', 'escode_remove_yoast_seo_comments', 9999);
// remove version from head
remove_action('wp_head', 'wp_generator');
// remove version from the feed
add_filter('the_generator', '__return_empty_string');
// remove version numbers from js and css
function escode_remove_version_scripts_styles($src) {
// only remove version numbers that begin with ver=
if (strpos($src,'ver=') !== false) {
$src = remove_query_arg('ver', $src);
}
return $src;
}
add_filter('style_loader_src', 'escode_remove_version_scripts_styles', 9999);
add_filter('script_loader_src', 'escode_remove_version_scripts_styles', 9999);
/**
* Keep jpeg quality at 100
*/
add_filter( 'jpeg_quality', 'escode_jpeg_quality' );
function escode_jpeg_quality() {
return 100;
}
// Disable support for comments and trackbacks in post types
function escode_disable_comments_post_types_support() {
$post_types = get_post_types();
foreach ($post_types as $post_type) {
if(post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
}
add_action('admin_init', 'escode_disable_comments_post_types_support');
// Close comments on the front-end
function escode_disable_comments_status() {
return false;
}
add_filter('comments_open', 'escode_disable_comments_status', 20, 2);
add_filter('pings_open', 'escode_disable_comments_status', 20, 2);
// Hide existing comments
function escode_disable_comments_hide_existing_comments($comments) {
$comments = array();
return $comments;
}
add_filter('comments_array', 'escode_disable_comments_hide_existing_comments', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment