add_filter( 'the_content', 'wpse317670_add_img_attribute' );
function wpse317670_add_img_attribute( $content ) {
$from = "https://wordpress.stackexchange.com/".preg_quote('<img', "https://wordpress.stackexchange.com/")."https://wordpress.stackexchange.com/";
$to = '<img example="example"';
return preg_replace($from, $to, $content, 1);
}
This will add the example="example"
to the first image found in every post content.
There is another option, without using regular expression (possibly much faster and will use less memory):
add_filter( 'the_content', 'wpse317670_add_img_attribute' );
function wpse317670_add_img_attribute( $content ) {
$from = '<img';
$to = '<img example="example"';
$pos = strpos( $content, $from );
if ( $pos !== false ) {
return substr_replace( $content, $to, $pos, strlen( $from ) );
}
return $content;
}
Related Posts:
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- How to add a rel attribute to images that contains their categories?
- Actions according to image type and size
- Automatically wrap multiple images in div
- Twentyten Child Theme: header images display messed up in admin panel
- Replace image attributes for lazyload plugin on image elements with specific class
- How to crop image in WordPress with aspect ratio for any size of image
- Missing feature image link function
- When should you, and when should you not, use wp_list_pluck()?
- How to change the markup WordPress inserts for post images
- Woocommerce add to simple product attribute programmatically [closed]
- Programmatically Set First Image as Featured
- WP 4.4. responsive loads normal image after loading the responsive image
- opening links in new tab using – add_filter( ‘the_content’, ‘make_clickable’);
- How to add classes to images based on their categories?
- How to reduce original image quality on upload?
- Set image size on media and text block with a function
- Generating Responsive Background Image Sizes in PHP
- Set default options for inserting media
- Create thumbnail on PDF upload with Gravity Forms
- Remove images from get_the_excerpt
- Remove tag on my RSS Feed
- Use Exact Image Size using add_image_size
- Insert width & height attributes to all images displayed on each page
- Custom image size in Media Dropdown
- Theme-Config tab in admin Pannel?
- How to Add a Random Custom Gravatar in the WordPress Comments?
- Overwrite Parent Theme add_image_size in Child Theme
- Images uploaded using media uploader are appearing upside down
- How to add css class to image attached in all the posts?
- Add custom image size not working
- How to mass-delete inline styles in WordPress custom post type automatically?
- Remove the deleted users avatar from list
- PHP files included in functions.php don’t seem to work
- How Can I Expand the WordPress Customize window without any Plugin?
- how to add an image to the footer?
- Set quantity of woocommerce product on page visit [closed]
- wp_get_attachment_image function size argument not working if I also add a custom class
- Understanding and altering the structure of posted images
- Publish/Update post is changing image links from file url to post url
- Featured Image Thumbnail Creation
- How can I remove the main description text box in a Woocommerce product editor page? I only use the “Product short description” text box [closed]
- Display images that are not in the content
- Best practices regarding the creation of custom widgets?
- show first 3 thumbnails of posts in different sizes [closed]
- Remove after images
- Word Press – Dynamic Content [closed]
- how can I just add to parent theme function(s) instead of redeclaring whole the function
- Getting custom image size URL in functions
- Can’t generate thumbnail images
- Control the srcset much more (not all sizes in it each time)
- What are the meta fields for an attachment?
- How to properly set custom logo size?
- How to Add Shortcode to html img code?
- The_content and Preg_replace in loaded Iframe [closed]
- Image Crop Not Working
- two search forms on the same page
- Use existing image sizes for WooCommerce
- Problem in using Customizer
- wp_get_attachment_image_src returns image on main domain, false on subdomain
- New checkbox in custom widget isn’t saving data
- Linking Javascript in functions.php file
- Retrieve First Image Function
- Featured Image keeps cropping
- Prevent creating multiple image resizes in twentytwelve template
- Conditional function for excluding first image from content, not working
- Function to check if custom page is used by page?
- Different image using srcset function
- Is it possible to limit number of files a non-admin user can upload via the WordPress media library?
- Auto delete content in specific folder inside media library
- Header menu aligned right on all pages except for single-post page [closed]
- How to update an image attachment’s alt text from a custom field when saving a post?
- large image size of 1440×1440 not pulling 2x of 2880×2880 image
- Hide author info in single posts by certain users
- Can You Set A Minimum Image Dimension For Resizing Images?
- Shortcode to insert default text and change one word throughout it?
- (Divi): How to make WordPress load images of specific size for Blurb modules on given page with a funciton?
- I want to customize the_posts_navigation function by replacing prev and next with images
- How would I correctly implement a new if statement in a child functions file?
- True email confirmation for registration (keeping unvalidated users from user table)
- restrict uploaded image size and fixed image display size
- Adding A New Widget to WordPress Disables the Existing Widgets
- How To Show Shortcodes In WordPress Custom Fields?
- Using Switch Statement to Change Image According to Last Digit of Topic ID
- Disable links to images only if link is an image
- Restricted images only for logged user. (hidden url)
- How do I integrate my Child Theme into a Custom Template?
- Shortcode to eliminate and replace with
- Different body image backgrounds on different pages, posts and categories
- custom post with loading script per single post
- Open image size links in a page instead of direct image link
- Get Value of Custom Field
- restrict access to specific urls on a specific period of time
- child_of not working while searching
- Function to get thumbnail img source
- Removing Facebook contact field from user contact not working
- WordPress Custom wp mail template return full template
- Problem with removing characters “<” – WpAllImport
- How to create a post without link in wordpress?
- post_row_actions filter from parent theme not executing in child theme