Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1331811

    I know this isn’t an enfold-specific question, but I haven’t been able to find an answer anywhere else, so I’m hoping you might be able to help or at least point me in the right direction. This client has a bunch of contributors and they’re always pasting text into posts, which brings in a bunch of <span style="font-weight: 400;"> tags (see https://prnt.sc/224iimz from https://newissue.wpengine.com/black-blue/ ). I’m trying to find a way to run some sort of search/replace on multiple posts to remove those tags while leaving the content between the opening/closing tags intact. Any idea how I might be able to pull that off? Thanks so much, and I understand if you can’t provide this kind of support.

    #1331824

    Hey sky19er,

    Thank you for the inquiry.

    Using this filter in the functions.php file might help.

    add_filter("the_content", function($content) {
    	preg_match_all('/<span style="font-weight: 400;">(.*)<\/span>/', $content, $matches);
    
    	foreach ($matches[0] as $key => $value) {
    		$content = str_replace($value, $matches[1][$key], $content);
    	}
    
    	return $content;
    }, 10, 1);
    

    Best regards,
    Ismael

    #1332250

    Thanks, Ismael — I was so hopeful, but it doesn’t seem to work. This post, for example, is still full of those tags: https://lunchticket.org/how-to-keep-writing-an-interview-with-lisa-locascio-nighthawk/ — maybe it doesn’t work on the Advance Layout Editor? Thanks again for trying!

    #1332287

    Hi,

    Thank you for the update.

    The greater and less than symbols in the html tags should be replaced with their html entity. For some reason, the forum converts them to the actual symbols.

    // https://www.toptal.com/designers/htmlarrows/math/greater-than-sign/
    // https://www.toptal.com/designers/htmlarrows/math/less-than-sign/

    To prevent the forum from converting the html entities, we added a spaces before lt and gt. Just copy the code in your editor, then remove the spaces.

    add_filter("the_content", function($content) {
    	preg_match_all('/& lt;span style="font-weight: 400;"& gt;(.*)& lt;\/span& gt;/', $content, $matches);
    
    	foreach ($matches[0] as $key => $value) {
    		$content = str_replace($value, $matches[1][$key], $content);
    	}
    
    	return $content;
    }, 10, 1);
    

    Best regards,
    Ismael

    #1333306

    Thanks again, Ismael, but it still doesn’t seem to be working — in fact apparently the instances of that tag in my test post actually increased from 81 to 107. I switched over to testing this in a dev environment, so I’m looking at https://newissue.wpengine.com/how-to-keep-writing-an-interview-with-lisa-locascio-nighthawk/ and here’s a screenshot of the filter: https://prnt.sc/23gu8os

    #1333403

    Hi,
    I copied your page html to an ALB test page using a text block to emulate your page, it contained 104 <span style="font-weight: 400;">
    I found that using this reduced it to zero and didn’t seem to lose any content or broken spans:

    add_filter("the_content", function($content) {
    	preg_match_all('/<span style="font-weight: 400;">(.*?)<\/span>/', $content, $matches);
    
    	foreach ($matches[0] as $key => $value) {
    		$content = str_replace($value, $matches[1][$key], $content);
    	}
    
    	return $content;
    }, 10, 1);

    2021-12-18_007.jpg
    2021-12-18_006.jpg
    The ? as a repetition quantifier changes this behavior into non-greedy, or Lazy, and in this case seems to work.

    Best regards,
    Mike

    #1333417

    Nice — that seems to work! Apparently, it doesn’t strip the code out of the editor, but I guess that doesn’t really matter — as long as it’s removed from the rendered page, eh?

    • This reply was modified 2 years, 11 months ago by sky19er.
    #1333432

    Hi,
    Glad this helped, shall we close this then?

    Best regards,
    Mike

    #1333565

    Yes — thank you so much!!

    #1333574

    Hi,
    Glad we were able to help, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.

    Best regards,
    Mike

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘remove extraneous code from posts’ is closed to new replies.