Title: remove url from post
Last modified: June 6, 2023

---

# remove url from post

 *  Resolved [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/)
 * Hello after cleaning my posts from shortcodes coming form a previous builder 
   i used (avada) all my post have a url instead of image at the beginning. Is is
   possibile to remove them with php or plugins?they are a lot so manual is not 
   an option. Also i should remove only those belonging to a specific category
 * Thank you
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fremove-url-from-post%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/remove-url-from-post/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/remove-url-from-post/page/2/?output_format=md)

 *  [christopher.amirian](https://wordpress.org/support/users/christopheramirian/)
 * (@christopheramirian)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16801066)
 * Hi there,
 * It is possible to remove these image URLs from your posts using PHP. Since you
   want to apply this only to a specific category, you can make use of the `the_content`
   filter along with a category check.
 * Here is a sample code snippet:
 *     ```wp-block-code
       function remove_image_url($content) {
           if (in_category('your-category-slug')) { // Replace 'your-category-slug' with your specific category slug
               $content = preg_replace('/<p>https?:\/\/[^<]*\.(jpg|jpeg|png|gif)<\/p>/', '', $content); // This regex assumes the URLs end with jpg, jpeg, png, or gif.
           }
           return $content;
       }
       add_filter('the_content', 'remove_image_url');
       ```
   
 * The code uses a regular expression to find and replace the `<p>` tags containing
   an image URL.
 * Please add this code to your theme’s functions.php file. Remember to replace ‘
   your-category-slug’ with the slug of your specific category.
 * **Please backup your site before making any changes to the code.**
 * Also, this code doesn’t permanently delete the image URLs from your posts. It
   only filters them out when the content is being displayed. If you want to permanently
   remove them, you would have to run a script to update each post in the database.
 * Alternatively, you can use plugins such as [search and replace](https://wordpress.org/plugins/better-search-replace/)
   that you can use to search for the content and delete them from the database.
 * Best regards,
 * Christopher Amirian
 *  Thread Starter [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16801402)
 * thanks..by the way..is there a way to show that image in the url instead?
 *  Thread Starter [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16802641)
 * the code has some error..actually i get error in each line, looks like some character
   are not expected
 * The search and replace plugin it is not good as i cannot choose a particolar 
   category posts
 *  [christopher.amirian](https://wordpress.org/support/users/christopheramirian/)
 * (@christopheramirian)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16804523)
 * Hi there,
 * I tested the code on my WordPress installation inside the functions.php of the
   theme and it did not give any errors.
 * Would you please be more specific on which errors you get so I can understand
   and help?
 * Best regards,
 * Christopher Amirian
 *  Thread Starter [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16804527)
 * aaahh i was executing it as cod ein phpmyadmin…
 * what about to screen the image in the url instead of deleting?
 *  [christopher.amirian](https://wordpress.org/support/users/christopheramirian/)
 * (@christopheramirian)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16804539)
 * Hi there,
 * Yes it is possible by replacing the code in functions.php file of your theme 
   with this one:
 *     ```wp-block-code
       function replace_image_url_with_tag($content) {
           if (in_category('your-category-slug')) { // Replace 'your-category-slug' with your specific category slug
               $pattern = '/<p>(https?:\/\/[^<]*\/([^<]*\.(?:jpg|jpeg|png|gif)))<\/p>/i';
               $replacement = '<p><img src="$1" alt="$2"></p>';
               $content = preg_replace($pattern, $replacement, $content);
           }
           return $content;
       }
       add_filter('the_content', 'replace_image_url_with_tag');
       ```
   
 * In this modified code, the regular expression pattern captures the URL and the
   normalized name of the image file (without the extension) using the parentheses`()`
   within the pattern. The captured values are then used in the replacement string.
 * The `$1` in the replacement string represents the captured URL from the regular
   expression pattern, and the `$2` represents the captured normalized name (without
   the extension) of the image.
 * Make sure to replace `'your-category-slug'` with the actual slug of your specific
   category.
 * With this modification, the code will add the `<img>` tag inside the `<p>` tag
   and set the normalized name of the image as the `alt` attribute of the `<img>`
   tag.
 * Remember to add this updated code to your theme’s `functions.php` file or in 
   a custom plugin file.
 * Best regards,
 * Christopher Amirian
 *  Thread Starter [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16804912)
 * What is the part in functions.php to be replaced by your code?
 *  [christopher.amirian](https://wordpress.org/support/users/christopheramirian/)
 * (@christopheramirian)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16809760)
 * Hi there,
 * If you used my previous code you should replace it with the new code that I suggested,
   if you did not add the previous code you do not need to replace it with anything,
   you can just add the code in functions.php.
 * Best regards,
 * Christopher Amirian
 *  Thread Starter [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16811725)
 * ok i have added to my functions.php this code
 *     ```wp-block-code
       function replace_image_url_with_tag($content) {
           if (in_category('arti-visive')) { // Replace 'your-category-slug' with your specific category slug
               $pattern = '/<p>(https?:\/\/[^<]*\/([^<]*\.(?:jpg|jpeg|png|gif)))<\/p>/i';
               $replacement = '<p><img src="$1" alt="$2"></p>';
               $content = preg_replace($pattern, $replacement, $content);
           }
           return $content;
       }
       add_filter('the_content', 'replace_image_url_with_tag');
       ```
   
 * i have changed the category slug with mine
 * but i don’t see any change
 * you can check it here
 * [https://www.counseloraroma.net/ozen-the-river-di-emir-baigazin/](https://www.counseloraroma.net/ozen-the-river-di-emir-baigazin/)
 * p.s. i had to disactivate the plugin WP optimize as i get an error at
 * wp-content/plugins/wp-optimize/vendor/matthiasmullie/minify/src/JS.php
 * disabling the plugin i can update the functions.php but if a enable again th 
   eplugin, and i try to update the functions.phhp again i get always that error
    -  This reply was modified 3 years ago by [stefanocps](https://wordpress.org/support/users/stefanocps/).
 *  [christopher.amirian](https://wordpress.org/support/users/christopheramirian/)
 * (@christopheramirian)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16811841)
 * Hi there,
 * The new URL that you shared has the image link embedded in aa P tag alongside
   other content inside that P tag. That is why you need to change the code to:
 *     ```wp-block-code
       function replace_image_url_with_tag($content) {
           if (in_category('arti-visive')) { // Replace 'your-category-slug' with your specific category slug
               $pattern = '/<p>(https?:\/\/[^<]*\/([^<]*\.(?:jpg|jpeg|png|gif)))/i';
               $replacement = '<p><img src="$1" alt="$2"></p>';
               $content = preg_replace($pattern, $replacement, $content);
           }
           return $content;
       }
       add_filter('the_content', 'replace_image_url_with_tag');
       ```
   
 * Best regards,
 * Christopher Amirian
 *  Thread Starter [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16812115)
 * wonderful!!it’s working now..Can i edit the size of the image with this function?
 *  [christopher.amirian](https://wordpress.org/support/users/christopheramirian/)
 * (@christopheramirian)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16812184)
 * Hi there,
 * You can change the code to apply an HTML class to the image:
 *     ```wp-block-code
       function replace_image_url_with_tag($content) {
           if (in_category('arti-visive')) { // Replace 'your-category-slug' with your specific category slug
               $pattern = '/<p>(https?:\/\/[^<]*\/([^<]*\.(?:jpg|jpeg|png|gif)))/i';
               $replacement = '<p><img class="post-featured-img" src="$1" alt="$2"></p>';
               $content = preg_replace($pattern, $replacement, $content);
           }
           return $content;
       }
       add_filter('the_content', 'replace_image_url_with_tag');
       ```
   
 * Then you can add CSS code to the style.css file of your theme to apply sizing
   and other styling:
 *     ```wp-block-code
       .post-featured-img {
           max-width: 100%;
           width: 300px;
       }
       ```
   
 * For example, the code above forces the size of the image to be 300 pixels.
 * Best regards,
 * Christopher Amirian
 *  Thread Starter [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16812187)
 * that is not the featured image..is still ok the code?
 *  Thread Starter [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * (@stefanocps)
 * [3 years ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16812198)
 * i have tried and it seems fine! great really thanks!
 * Can i ask you 2 more things about the same post
 * You see…there is at the end
 * ARTCOLI RECENTI (Latest post) which dn’t show anything..don’t know if i can show
   latest post there..or at least delete that writing
 * Also i want to delete the tagged part at the bottom
 *  [christopher.amirian](https://wordpress.org/support/users/christopheramirian/)
 * (@christopheramirian)
 * [2 years, 12 months ago](https://wordpress.org/support/topic/remove-url-from-post/#post-16820863)
 * Hi there,
 * The best way to get answer for new questions is to open up another forum thread
   as the threads are meant to handle one question for the sake of clarity and ease
   of use.
 * Thank you for your understanding.
 * Best regards,
 * Christopher Amirian

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/remove-url-from-post/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/remove-url-from-post/page/2/?output_format=md)

The topic ‘remove url from post’ is closed to new replies.

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 17 replies
 * 2 participants
 * Last reply from: [stefanocps](https://wordpress.org/support/users/stefanocps/)
 * Last activity: [2 years, 11 months ago](https://wordpress.org/support/topic/remove-url-from-post/page/2/#post-16893400)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
