• Hi all,

    Im trying to edit some post content globally on all posts, that changes some text using regex to display twitter post from other data.

    I tried ALOT of ways to do this, but nothing worked.

    Wrote a small plugin for this, just simple one with this code:

    function twitter_filter_content( $text ) 
    {   
        if ( is_singular() && in_the_loop() && is_main_query() ) 
        {       
            $text = preg_replace('/<blockquote class="twitter-tweet".*<a href="(.*)">.*<\/a><\/blockquote>/s', '<!-- wp:embed {"url":"${1}","type":"rich","providerNameSlug":"twitter","responsive":true,"className":""} --><figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">${1}</div></figure><!-- /wp:embed -->', $text);             
        }
        
        return $text;
    }
    
    add_filter( 'the_content', 'twitter_filter_content', 1);

    This preg_replace code should work and been tested at regex101.com and online-free-tools.com/en/php_preg_replace_test_pattern and should be working, atleast there, but its not working on the site, my guess is, its looking at the html output code after render and that will never work.

    It need to change the post source text BEFORE wordpress changes to html post.

    Anyone knows a trick? ??

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Make sure your conditional checks are doing what you expect. See if removing the conditional structure makes a difference. If not, confirm the content is as you expect by adding var_dump( $text ); and checking the raw output. View by using the browser’s source HTML view so the whitespace and newlines remain in the formatting.

    Thread Starter intempodk

    (@intempodk)

    I tried that, it just shows
    string(7000) ”
    a copy of the post

    and the normal post after.

    it does not show the post “raw source” i can edit before render (the source you see when you edit the post).

    as im trying to force a twitter block in, it needs to be changed before turning into html output

    Thread Starter intempodk

    (@intempodk)

    What im trying to do is, it actually “edits” the post when a user clicks it.
    Like, when you manually open it in wordpress editor and edits it, then saves it.

    This just does it on the fly before its shown to the user.

    But adding a <– ! block in the_content wont work, as wordpress dont process it, it will just show up as “text” in the html source code

    Thread Starter intempodk

    (@intempodk)

    function twitter_filter_content( $text ) 
    {   
        if ( is_singular() && in_the_loop() && is_main_query() ) 
        {       
    		$text = preg_replace(
    		'/<blockquote class="twitter-tweet".*?>.*?\) <a href="(.*?)\?.*?">.*?<\/blockquote>/s',
    		'<!-- wp:embed {"url":"${1}","type":"rich","providerNameSlug":"twitter","responsive":true,"className":""} -->
    		<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">${1}</div></figure>
    		<!-- /wp:embed -->',
    		$text);
    
    		//var_dump( $text );  
        }
    	
        return $text;
    }
    
    add_filter( 'the_content', 'twitter_filter_content');

    This code DOES change the text correctly in var_dump, but its not working in the outputtet post, it just shows the twitter url, no twitter box, its like it dont use the block, maybe it needs to be run other way, earlier in post data?

    Think i read something about post() and apply_filters(), to make shortcodes work but not sure how to use that in a simple plugin like this.

    Moderator bcworkz

    (@bcworkz)

    If you want to insert HTML into block content using the editor, you need to use its code view (from 3 dot icon at upper right)

    If your code is working as you want according to var_dump, but the output is still incorrect, perhaps you need to hook later instead of earlier. Some other filter hook could be changing things. Try changing the add_filter() call’s priority argument from 1 (OP version) to something large like 9999. You may need to var_dump again to see what you need to match now as it may be different due to other filtering.

    If that doesn’t help, the problem is likely due to oEmbed behavior and is not easily fixable by filtering “the_content”. Could you get actual embed HTML code from Twitter and insert that instead of a WP embed block when filtering “the_content”?

    Thread Starter intempodk

    (@intempodk)

    Okay after fidling some more, it seems the problem now is the preg_replace, it makes it look like this with newline at url (not working)

    <!-- wp:embed {"url":"https://twitter.com/thename/76396939461923712","type":"rich","providerNameSlug":"twitter","responsive":true} -->
    <figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
    https://twitter.com/thename/76396939461923712
    </div></figure>
    <!-- /wp:embed -->
    

    when output must look like this (working)

    <!-- wp:embed {"url":"https://twitter.com/thename/76396939461923712","type":"rich","providerNameSlug":"twitter","responsive":true} -->
    <figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">https://twitter.com/thename/76396939461923712</div></figure>
    <!-- /wp:embed -->

    i tried changing the preg_replace code from this:

    
    $post['post_content']  = preg_replace(
    		'/<blockquote class="twitter-tweet".*?>.*?\) <a href="(.*?)\?.*?">.*?<\/blockquote>/s',
    		'<!-- wp:embed {"url":"${1}","type":"rich","providerNameSlug":"twitter","responsive":true,"className":""} --><figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">${1}</div></figure><!-- /wp:embed -->',
    		$post['post_content'] );
    

    to trim, and to preg_replace new lines, nothing removes the newline…

    trim(preg_replace('["\r\n", "\n", "\r"]', ' ', '<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">${1}</div></figure>'))

    It there some sort of character i can use to force it into one single line? something im missing?

    Moderator bcworkz

    (@bcworkz)

    It seems like it’s being added later, so it may be beyond your callback’s control. Your code has no newlines in the figure block, so it’d be hard to remove one. I don’t think it’d be picked up from the matched string since your regexp is using lazy matching in the capture group.

    Maybe try a different approach? You’re still relying upon oEmbed to work. What if your code fetched the oEmbed content itself and replaced the URL in the inner div with the oEmbed content? Get it with wp_oembed_get(). This does mean having to first do a preg_match() to extract the twitter URLs, but it’ll save the usual built-in oEmbed routine from having to preg_replace the same, so this approach would be no less efficient.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Change post data before render to screen’ is closed to new replies.