Search code examples
wordpresstexthide

How do I remove a specific string of text that has variables


I am using a wordpress site. I want to remove or replace some text. This is content from a plugin I am unable to change the output (at least from all of my attempts to).

For example with the brackets;

(The Text: date1⁠–date2)

(The Text: Feb-09-Feb-12)

The two dates are variable, as in they will change all of the time. Is there a way I can set a wildcard to say whatever is in data1 and data2 replace the string date1-date2 with X or remove that whole string "(The Text: date1⁠–date2)" altogether.

Is there a function I can add to my themes function.php file?

Or as an alternative using css. This text is within a div that I can set to disply:none but inside that I need another div to display. My thinking was to just remove the text, as above. Again this is content from a plugin I am unable to change the output (at least from all of my attempts to). I tried visibility but it leaves the space where that text was.

How can I display something inside a div that is set to display:none?

So

<div style="display:none">
  <div>
    display something
  </div>
</div>

Solution

  • You can't display something inside an element set to display: none;. You could try and set the font-size of the element to 0, and use the :after selector to add your text to the content and set the font-size back to something readable:

    div { font-size: 0; }
    div:after { font-size: 16px; content: "This Text is shown"; }
    <div>(The Text: date01-date02)</div>

    Alternatively, it's a bit of a "hammer to hit a fly" solution, but using the template_redirect hook, you can turn on Output Buffering and run preg_replace() on the content in the buffer, it's a technique I like to call "Runtime find & replace".

    add_action( 'template_redirect', 'global_find_replace', 99 );
    function global_find_replace(){
        ob_start( function( $buffer ){
            $text   = 'Your Text Here';
            $buffer = preg_replace('/\(The Text: .*\)/', $text, $buffer );
    
            return $buffer;
        });
    }
    

    Documentation & Function Reference

    Function Linked Description
    template_redirect Fires before determining which template to load.
    preg_replace() Perform a regular expression search and replace