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
- Add multiple custom fields to the general settings page
- Add image size if page template
- When should you, and when should you not, use wp_list_pluck()?
- Set JPEG compression for specific custom image sizes
- Display random categories on the front page (Finding and Editing Theme Functions)
- remove links from images using functions.php
- Issue with wp_get_attachment_image() and SVG file type
- How to change the markup WordPress inserts for post images
- Is it possible to remove the “standard” post format?
- Woocommerce add to simple product attribute programmatically [closed]
- Programmatically Set First Image as Featured
- Issue with get_theme_mod returning a blank value instead of the saved value
- Removing default image size list in Media Box
- How to remove row-actions from pages table?
- How to use get_template_directory_uri() to load an image that is in a sub-folder of my theme?
- 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 create thumbnails for PDF uploads?
- How to reduce original image quality on upload?
- Modify search function in WordPress (TwentyTwelve)
- How to customize search result page title?
- Is it possible to maintain image aspect ratio when scaling images?
- Set image size on media and text block with a function
- Generating Responsive Background Image Sizes in PHP
- How to fix a theme with page.php Default Template that accidentally deleted?
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Removing Unnecessary Text from Admin Menu without CSS
- Set default options for inserting media
- Create thumbnail on PDF upload with Gravity Forms
- Default or Preset Content for Custom Post Types
- Gallery images titles – get from post
- Remove images from get_the_excerpt
- Remove tag on my RSS Feed
- Display WordPress Search
- How do I change parameters without changing the core
- Problem with extract() with custom shortcode
- Use Exact Image Size using add_image_size
- add sidebar area to header of child theme
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Preferred Use of home_url()?
- Display height and width properties with the_post_thumbnail() or related function
- Custom function for user register in wp?
- Insert width & height attributes to all images displayed on each page
- Custom image size in Media Dropdown
- Best way to programatically add “rel” attributes to page and post images
- Theme-Config tab in admin Pannel?
- Creating loop within functions.php
- How to Add a Random Custom Gravatar in the WordPress Comments?
- Removing the first image in content
- Function to auto-set a featured image that is already in use
- Delete pages and Create default pages for all new network sites
- Woocommerce Storefront WordPress Ignore Media Gallery Images and use External 3rd Party Host
- Overwrite Parent Theme add_image_size in Child Theme
- Images uploaded using media uploader are appearing upside down
- Post thumbnail relative link and HTML modify
- Refactor create_function
- How to add css class to image attached in all the posts?
- adding custom classes for tables
- Add custom image size not working
- Pass Shortcode Attribute to footer Script
- How to mass-delete inline styles in WordPress custom post type automatically?
- How would go about if I just want a temporary function?
- Remove the deleted users avatar from list
- How to call custom function from functions.php in site-wide template files?
- Customizer field value into functions.php variable
- Count total number of images in post and echo results as number
- PHP files included in functions.php don’t seem to work
- How can you limit srcset on a single type of page?
- 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_dropdown_categories and custom attribute
- str_replace function in theme
- wp_get_attachment_image function size argument not working if I also add a custom class
- Ajax not working properly
- is_customize_preview() like function to check if Customizer “Live Preview” in JavaScript
- Understanding and altering the structure of posted images
- Overwriting Parent Theme’s Image Sizes
- Right way to update widget on dynamic new input field
- How to customly read JWPlayer’s “Playlist” items by using its “playlistid”?
- Image rotation fails to regenerate custom sized thumbnail
- Publish/Update post is changing image links from file url to post url
- Include default functions and methods
- Hard-coding custom menu elements for menu manager
- 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]
- Disable auto-resizing of uploaded images, but only for certain filename
- Display images that are not in the content
- How can I modify all existing tags while keeping the urls themselves?
- Best practices regarding the creation of custom widgets?
- IF Statement on Button to make it complete and “uncomplete” based on user action / click