define ('WP_MEMORY_LIMIT', '768M');
define('WP_MAX_MEMORY_LIMIT', '756M');
set_time_limit(300);
.site-main p a {
font-weight: bold;
}
p:last-of-type{
margin: 0;
}
body {overflow-x:hidden !important;}
Redirect 404 to Home Page PHP
add_action( 'template_redirect', function() {
if (is_404()) {
wp_safe_redirect(home_url());
exit();
}
} );
Smooth Scrolling for Anchor Links
document.addEventListener('click', function(e) {
// Check if the clicked element is an anchor with href starting with '#'
if (e.target.tagName === 'A' && e.target.getAttribute('href') && e.target.getAttribute('href').startsWith('#')) {
e.preventDefault();
const targetId = e.target.getAttribute('href').slice(1); // Remove the '#' from the href
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth'
});
}
}
});
Limit Uploaded Image Size
add_filter( 'wp_handle_upload', function ( $file ) {
$max_width = 1920;
$max_height = 1920;
// Check if the file is an image.
$mime_type = mime_content_type( $file['file'] );
if ( strpos( $mime_type, 'image' ) === false ) {
return $file;
}
// Get the image size.
$image_size = getimagesize( $file['file'] );
if ( ! $image_size ) {
return $file;
}
// Check if the image is smaller than 1920px width or height.
if ( $image_size[0] <= $max_width && $image_size[1] <= $max_height ) {
return $file;
}
// Resize the image.
$image_editor = wp_get_image_editor( $file['file'] );
if ( is_wp_error( $image_editor ) ) {
return $file;
}
$image_editor->resize( $max_width, $max_height );
$image_editor->save( $file['file'] );
return $file;
} );
Disable Site Health
// Remove Tools Submenu Item for Site Health.
add_action( 'admin_menu', function () {
remove_submenu_page( 'tools.php', 'site-health.php' );
} );
// Prevent direct access to the Site Health page.
add_action( 'current_screen', function () {
$screen = get_current_screen();
if ( 'site-health' === $screen->id ) {
wp_safe_redirect( admin_url() );
exit;
}
} );
// Disable the Site Health Dashboard Widget.
add_action( 'wp_dashboard_setup', function () {
global $wp_meta_boxes;
if ( isset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health'] ) ) {
unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health'] );
}
} );
Limit the Number of Post Revisions
add_filter( 'wp_revisions_to_keep', function( $limit ) {
// Limit to the last 10 revisions. Change 10 to whatever limit you want.
return 10;
} );
Change Excerpt Length
add_filter(
'excerpt_length',
function ( $length ) {
// Number of words to display in the excerpt.
return 40;
},
500
);
Dynamic Year Copyright Shortcode
// Copy the Shortcode in the Insertion box below and paste it wherever you want the copyright symbol + year to be displayed.
// This will output © 2023 or the current year automatically.
echo "© " . date( 'Y' );
Disable Thumbnail Image Sizes
add_filter( 'intermediate_image_sizes_advanced', function( $sizes ) {
// Disable specific thumbnail sizes, uncomment the ones you want to disable.
// unset( $sizes['thumbnail'] ); // 150px x 150px
// unset( $sizes['medium'] ); // 300px x 300px
// unset( $sizes['medium_large'] ); // 768px x 0px
// unset( $sizes['large'] ); // 1024px x 1024px
// unset( $sizes['1536x1536'] ); // 1536px x 1536px
// unset( $sizes['2048x2048'] ); // 2048px x 2048px
return $sizes;
} );
Default Featured Image
// Go to Settings > Media after activating this snippet to set the default featured image.
add_action( 'admin_init', function() {
register_setting( 'media', 'default_featured_image', 'absint' );
add_settings_field(
'default_featured_image',
__( 'Default Featured Image', 'wpcode-snippet' ),
function() {
wp_enqueue_media();
$image_id = get_option( 'default_featured_image', 0 );
$image_url = $image_id ? wp_get_attachment_url( $image_id ) : '';
?>
'default_featured_image' )
);
});
add_filter( 'post_thumbnail_html', function( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( ! $html ) {
$default_image_id = get_option( 'default_featured_image' );
if ( $default_image_id ) {
$image_url = wp_get_attachment_image_src( $default_image_id, $size );
if ( $image_url ) {
$html = '
';
}
}
}
return $html;
}, 10, 5 );
Add Media File Size Column
add_filter( 'manage_upload_columns', function ( $columns ) {
$columns ['file_size'] = esc_html__( 'File size' );
return $columns;
} );
add_action( 'manage_media_custom_column', function ( $column_name, $media_item ) {
if ( 'file_size' !== $column_name || ! wp_attachment_is_image( $media_item ) ) {
return;
}
$filesize = size_format( filesize( get_attached_file( $media_item ) ), 2 );
echo esc_html( $filesize );
}, 10, 2 );
Add Media File Size Column
add_filter( 'manage_upload_columns', function ( $columns ) {
$columns ['file_size'] = esc_html__( 'File size' );
return $columns;
} );
add_action( 'manage_media_custom_column', function ( $column_name, $media_item ) {
if ( 'file_size' !== $column_name || ! wp_attachment_is_image( $media_item ) ) {
return;
}
$filesize = size_format( filesize( get_attached_file( $media_item ) ), 2 );
echo esc_html( $filesize );
}, 10, 2 );
Reorder Admin Menu Items
add_filter( 'custom_menu_order', '__return_true' );
// This will move the WPCode menu under the Dashboard menu item.
// Uncomment and add more items as needed.
add_filter( 'menu_order', function () {
return array(
'index.php',
'wpcode',
// 'edit.php', // Posts
// 'upload.php', // Media
// 'edit.php?post_type=page', // Pages
// 'edit-comments.php', // Comments
);
} );
Add Featured Image Column
add_filter( 'manage_posts_columns', function ( $columns ) {
// You can change this to any other position by changing 'title' to the name of the column you want to put it after.
$move_after = 'title';
$move_after_key = array_search( $move_after, array_keys( $columns ), true );
$first_columns = array_slice( $columns, 0, $move_after_key + 1 );
$last_columns = array_slice( $columns, $move_after_key + 1 );
return array_merge(
$first_columns,
array(
'featured_image' => __( 'Featured Image' ),
),
$last_columns
);
} );
add_action( 'manage_posts_custom_column', function ( $column ) {
if ( 'featured_image' === $column ) {
the_post_thumbnail( array( 300, 80 ) );
}
} );
Customize Password Protect Page Login
.post-password-form {
padding: 200px 100px;
height: 75vh;
background: #e7f1f3;
}
#pwbox-1762 {
background-color: #ffffff;
}
/*
Plugin Name: Purge Cache
Description: Adds a button to the WordPress dashboard to clear the object cache
*/
add_action( 'admin_bar_menu', 'add_purge_cache_button', 999 );
function add_purge_cache_button( $wp_admin_bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$args = array(
'id' => 'purge-cache',
'title' => 'Purge Cache',
'href' => '#',
'meta' => array( 'class' => 'purge-cache' )
);
$wp_admin_bar->add_node( $args );
}
add_action( 'admin_footer', 'add_purge_cache_script' );
function add_purge_cache_script() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
//Remove Gutenberg Block Library CSS from loading on the frontend
function smartwp_remove_wp_block_library_css(){
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
}
add_action( 'wp_enqueue_scripts', 'smartwp_remove_wp_block_library_css' );
Explicit Fixed Width and Height
add_filter( 'the_content', 'add_image_dimensions' );
function add_image_dimensions( $content ) {
preg_match_all( '/
]+>/i', $content, $images);
if (count($images) < 1)
return $content;
foreach ($images[0] as $image) {
preg_match_all( '/(alt|title|src|width|class|id|height)=("[^"]*")/i', $image, $img );
if ( !in_array( 'src', $img[1] ) )
continue;
if ( !in_array( 'width', $img[1] ) || !in_array( 'height', $img[1] ) ) {
$src = $img[2][ array_search('src', $img[1]) ];
$alt = in_array( 'alt', $img[1] ) ? ' alt=' . $img[2][ array_search('alt', $img[1]) ] : '';
$title = in_array( 'title', $img[1] ) ? ' title=' . $img[2][ array_search('title', $img[1]) ] : '';
$class = in_array( 'class', $img[1] ) ? ' class=' . $img[2][ array_search('class', $img[1]) ] : '';
$id = in_array( 'id', $img[1] ) ? ' id=' . $img[2][ array_search('id', $img[1]) ] : '';
list( $width, $height, $type, $attr ) = getimagesize( str_replace( "\"", "" , $src ) );
$image_tag = sprintf( '
', $src, $alt, $title, $class, $id, $width, $height );
$content = str_replace($image, $image_tag, $content);
}
}
return $content;
}
Remove Unused JS
/**
* We will Dequeue the jQuery UI script as example.
*
* Hooked to the wp_print_scripts action, with a late priority (99),
* so that it is after the script was enqueued.
*/
function wp_remove_scripts() {
// check if user is admina
if (current_user_can( 'update_core' )) {
return;
}
else {
// Check for the page you want to target
if ( is_page( 'homepage' ) ) {
// Remove Scripts
wp_dequeue_style( 'jquery-ui-core' );
}
}
}
add_action( 'wp_enqueue_scripts', 'wp_remove_scripts', 99 );
Ensure Webfont is loaded
add_filter( 'elementor_pro/custom_fonts/font_display', function( $current_value, $font_family, $data ) {
return 'swap';
}, 10, 3 );
Remove Google Fonts
add_filter( 'elementor/frontend/print_google_fonts', '__return_false' );
Web Squadron Code Snippet Library:
Codes
Password protected Page Styling: https://youtu.be/q8TQU3kMci0?si=lbFIRZ9kpqtBhSBI
Add Preview Sizes, Swapping and Ratios to the Top Bar: remove PHP at top!
Elementor Pro Form Checkboxes
selector .elementor-field-option {
display: inline-block;
vertical-align: middle;
gap:10px;
accent-color: #21282F;
font-size: 17px
}
input {
display: inline-block;
vertical-align: middle;
width: 17px;
height: 17px;
}