Wordpress
List of useful Wordpress Codes
1 . Limit login access to WordPress websites with these codes
You do not want to add additional plugins to your WordPress site, so you do not want to disable login. Then, when this code is printed in functions.php, selected failed attempts will be subject to time limits — The failure timings and wait time can both be customized.
function check_attempted_login( $user, $username, $password ) { if ( get_transient( 'attempted_login' ) ) { $datas = get_transient( 'attempted_login' ); if ( $datas['tried'] >= 3 ) { $until = get_option( '_transient_timeout_' . 'attempted_login' ); $time = time_to_go( $until ); return new WP_Error( 'too_many_tried', sprintf( __( '<strong>ERROR</strong>: You have reached authentication limit, you will be able to try again in %1$s.' ) , $time ) ); } } return $user; } add_filter( 'authenticate', 'check_attempted_login', 30, 3 ); function login_failed( $username ) { if ( get_transient( 'attempted_login' ) ) { $datas = get_transient( 'attempted_login' ); $datas['tried']++; if ( $datas['tried'] <= 3 ) set_transient( 'attempted_login', $datas , 300 ); } else { $datas = array( 'tried' => 1 ); set_transient( 'attempted_login', $datas , 300 ); } } add_action( 'wp_login_failed', 'login_failed', 10, 1 ); function time_to_go($timestamp) { // converting the mysql timestamp to php time $periods = array( "second", "minute", "hour", "day", "week", "month", "year" ); $lengths = array( "60", "60", "24", "7", "4.35", "12" ); $current_timestamp = time(); $difference = abs($current_timestamp - $timestamp); for ($i = 0; $difference >= $lengths[$i] && $i < count($lengths) - 1; $i ++) { $difference /= $lengths[$i]; } $difference = round($difference); if (isset($difference)) { if ($difference != 1) $periods[$i] .= "s"; $output = "$difference $periods[$i]"; return $output; } }
2 . Codes for putting your WordPress site into maintenance mode
/*This code will display the following message */ function wp_maintenance_idc () { if( !is_user_logged_in() ){ echo '<h1>Briefly unavailable for scheduled maintenance. Check back in a minute.</h1>'; die ();} } add_action ('get_header','wp_maintenance_idc'); ------------------------------------------------------------------------ /*This code will redirect to the Location URL */ function wp_maintenance_redirect () { if( !is_user_logged_in() ){ header("Location: http://www.your-website.com/site.html", true, 302); die ();} } add_action ('get_header','wp_maintenance_redirect');
3 . Fixing codes: Your browser is blocking cookies or doesn’t support them
setcookie(TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN); if ( SITECOOKIEPATH != COOKIEPATH ) setcookie(TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN);
4 . WordPress codes to enable WebP uploads
WordPress does not enable you to upload WebP format photos by default, citing security concerns. However, you may quickly upload WebP pictures to your WordPress website using the following code, which is also recommended by Google PageSpeed and GTmetrix as Serve photos in next-gen formats. Simply copy and paste the code into the functions.php file of your theme.
/*This allows to upload webp files */ function webp_file_ext( $types, $file, $filename, $mimes ) { if ( false !== strpos( $filename, '.webp' ) ) { $types['ext'] = 'webp'; $types['type'] = 'image/webp'; } return $types; } add_filter( 'wp_check_filetype_and_ext', 'webp_file_ext', 10, 4 ); function webp_file_upload( $mimes ) { $mimes['webp'] = 'image/webp'; return $mimes; } add_filter( 'upload_mimes', 'webp_file_upload' ); /*Preview Webp files */ function preview_webp_thumnail($result, $path) { if ($result === false) { $displayable_image_types = array( IMAGETYPE_WEBP ); $info = @getimagesize( $path ); if (empty($info)) { $result = false; } elseif (!in_array($info[2], $displayable_image_types)) { $result = false; } else { $result = true; } } return $result; } add_filter('file_is_displayable_image', 'preview_webp_thumnail', 10, 2);