Created
December 28, 2025 19:10
-
-
Save mehrshaddarzi/676ff06b773bd2764660ea3bc8331cf1 to your computer and use it in GitHub Desktop.
Disable Lazy Load For Image In WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Lazy Load Exclude by Filename | |
| Description: حذف loading="lazy" از تصاویر Visual Composer و تصاویر وردپرس بر اساس نام فایل. | |
| Version: 1.1 | |
| Author: Your Name | |
| */ | |
| if (!defined('ABSPATH')) exit; | |
| // --------------------------- | |
| // اضافه کردن فیلد به صفحه تنظیمات عمومی | |
| // --------------------------- | |
| add_action('admin_init', function() { | |
| add_settings_field( | |
| 'lazy_exclude_files', // ID | |
| 'نام فایلهای بدون lazy', // Title | |
| 'lazy_exclude_files_callback', // Callback | |
| 'general' // صفحه | |
| ); | |
| register_setting('general', 'lazy_exclude_files', [ | |
| 'type' => 'string', | |
| 'sanitize_callback' => 'sanitize_textarea_field', | |
| 'default' => '' | |
| ]); | |
| }); | |
| function lazy_exclude_files_callback() { | |
| $value = get_option('lazy_exclude_files', ''); | |
| echo '<textarea name="lazy_exclude_files" rows="10" cols="50" style="width:100%; direction:ltr; text-align:left;">' . esc_textarea($value) . '</textarea>'; | |
| echo '<p>نام فایلهایی که میخواهید lazy حذف شود، هر نام در یک خط</p>'; | |
| } | |
| // --------------------------- | |
| // فیلتر برای تصاویر وردپرس | |
| // --------------------------- | |
| add_filter('wp_get_attachment_image_attributes', function($attr, $attachment, $size) { | |
| $files_option = get_option('lazy_exclude_files', ''); | |
| if (!empty($files_option)) { | |
| $files_to_remove_lazy = array_filter(array_map('trim', explode("\n", $files_option))); | |
| $file_name = basename(get_attached_file($attachment->ID)); | |
| if (in_array($file_name, $files_to_remove_lazy)) { | |
| unset($attr['loading']); | |
| } | |
| } | |
| return $attr; | |
| }, 10, 3); | |
| // --------------------------- | |
| // فیلتر برای Visual Composer | |
| // --------------------------- | |
| add_filter('vc_shortcode_output', function($output, $atts, $content, $tag) { | |
| $files_option = get_option('lazy_exclude_files', ''); | |
| if (!empty($files_option)) { | |
| $files_to_remove_lazy = array_filter(array_map('trim', explode("\n", $files_option))); | |
| foreach ($files_to_remove_lazy as $file) { | |
| $output = preg_replace( | |
| '/(<img[^>]*src=["\'][^"\']*' . preg_quote($file, '/') . '["\'][^>]*)\sloading=["\']lazy["\']/i', | |
| '$1', | |
| $output | |
| ); | |
| } | |
| } | |
| return $output; | |
| }, 10, 4); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment