You use preg_replace
function incorrectly. This function returns replaced content:
add_filter( 'the_content', 'wpdu_image_replace' );
function wpdu_image_replace( $content ) {
return preg_replace( '/<img.*?src="https://wordpress.stackexchange.com/questions/55241/(.*?)".*?>/', '<a href="$1">Image file</a>', $content );
}
Also pay attention that you don’t have to use global variable $post
, because content of the post is passed to your function as first argument.
Related Posts:
- Insert Custom HTML After Shortcode
- How to apply content filter permanently?
- How to wrap pattern-matches from the_content in [wiki]-shortcode tags
- Add class to all parent elements inside the_content
- apply_filters(‘the_content’, $content) vs do_shortcode($content)
- the_content and is_main_query
- How to appending to the_content using add_filter with custom post type?
- How to wrap an element around an iframe or embed in content automatically?
- WordPress Internal @ Mentions
- Where to hook into post content?
- Add whitespace between Chinese and other letters
- Hook added to the_content seems to be called multiple times
- Get excerpt from $post->post_content
- Add Class to Specific Paragraph of the_content()
- add_filter for specific pages
- What params are available with the_content filter?
- Using variable from one filter in another filter
- apply_filters(‘the_content’, $content) alternative
- What effect does the_content filter have on performance?
- How can I display image metadata?
- Add post/page ID to inserted links within the_content
- How to prepend text to the_content, but after img/shortcode
- Format content value from DB outside of WordPress filters
- What is the filter hook for custom fields content?
- What functions are included in apply_filter(‘the_content’)
- Filter Gutenberg Blocks Content
- Return array of images after content
- Nested calls the the_content filter
- Replacing with(out) regex link to attachment by links to files
- Editing
- the_content filter – checking the post
- Wrap First Character after in a tag
- Apply the_content filter, but prevent other hooked actions
- Wrap h1-h6 in a div
- How to change the order (priority) of registered filters (or actions) (e.g. for the_content)?
- Function the_content not working
- Why won’t my preg_replace work with content_save_pre?
- ‘the_content’ filter removing html tags from post content
- Modify Image Source With The_Content Filter?
- add_filter(‘the_content’, ‘…’) stops pagination from working
- Post Content, Special Characters and Filters
- String regex match replace for role ‘contributor’ only
- Filter on the_content doesn’t update the content being searched via register_rest_route
- How to hide Ads in between posts on AMP? [closed]
- Add content as soon starts
- Remove Content Filter
- tag removed not using a filter
- Replace audio links with jplayer using the_content filter
- Add a div of content within the_content after a certain block
- How do you get specific tags from the_content?
- Can i use multiple ‘the_content’ filters?
- get_post() containing gallery is outputting an unmatched closing div at the end of the content
- Load Posts’ individual body content on index starting at char 200 of each post
- Excerpt is being added above the content, but it should be after it
- Using preg_replace() with the_content filter
- What is the earliest hook to modify post content?
- Filter content for get_the_content()
- Why is overwriting $GLOBALS killing the_content()?
- Stripping URLs & Email from post submissions
- add_filter the_content doesn’t work
- How can I get the default content of WordPress post?
- Email Obfuscation: Is antispambot() Acceptable? [closed]
- alternative to the_content filter
- add_filter to the_content after apply_filters
- How to stop DOMDocument destroying embeds?
- Scanning for custom embed and prefetching
- the_content getting current page content instead specified ID
- get_post() with filters applied
- Strange behaviour of REGEX in a WordPress filter (trying to suppress emtpy paragraphs)
- Set Microsoft Word links to open in new window/tab
- How do I isolate the reason a wordpress filter is not running?
- Remove External Links from WordPress posts Using add_filter() in Theme functions.php
- the_content filter not working when Jetpack activated. Any idea?
- Apply a filter str_replace on specific caracters array in the_content()
- changing size image within the content
- Can’t get content of all (19) posts – Incomplete Chunked Encoding
- filter on the_content stopped working when I updated to WP 3.6.1
- Replace image scr with it’s surrounding href
- After adding filter to plugin’s code, post’s content doesn’t display
- Limit total tags in the_content
- Valid characters for actions, hooks and filters
- AJAX filter posts on click based on category
- Can I add custom meta for each image uploaded via media-upload.php?
- Some questions regarding filter
- Problem getting single_template filter to work – I want to serve a different single.php file for posts in a certain category
- return values from hooks do_action and apply_filters, which is better
- get_title without filter(the_title)
- Does a plugin with a AJAX button filter exist? [closed]
- Translate custom order status through a filter?
- WP 4.5 hide core customizer sections
- Custom nav walker: How to acces the $args parameter?
- Same URL for portfolio and for a page creates 404 error. Is there any filter that i can use for a child page?
- How to get different html tags from the content using regex?
- $content is empty while using DOMDocument
- Custom xmlrpc request does not pass parameters?
- WordPress URL rewrite problem
- Which hook should be used in this case?
- Why does using excerpt_more filter change link location?
- Query Multiple Filters, one with Meta
- How to remove a filter that is an object method?