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 to see the link]
-
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:
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 that you can use to search for the content and delete them from the database.
Best regards,
Christopher Amirian
thanks..by the way..is there a way to show that image in the url instead?
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
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
aaahh i was executing it as cod ein phpmyadmin…
what about to screen the image in the url instead of deleting?
Hi there,
Yes it is possible by replacing the code in functions.php file of your theme with this one:
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 thealt
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
What is the part in functions.php to be replaced by your code?
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
ok i have added to my functions.php this 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/
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 1 year, 9 months ago by
stefanocps.
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:
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
wonderful!!it’s working now..Can i edit the size of the image with this function?
Hi there,
You can change the code to apply an HTML class to the image:
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:
.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
that is not the featured image..is still ok the code?
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
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
-
This reply was modified 1 year, 9 months ago by
- The topic ‘remove url from post’ is closed to new replies.