Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filtering text in replies

  • @bhkh

    Participant

    Hello!

    I’m trying to filter links that appear in responses. More specifically, they are links that appear in attachments that have been added through GD bbPress Attachments if that matters. I’m fairly sure the code works because it can filter in the content of a page just fine. Here is the code:

    
    add_filter( 'the_content', 'myprefix_use_audio_short_tag' );
    
    function myprefix_use_audio_short_tag( $content ) {
        $reg = "#<a .+ href=\"(.+?\.mp3)\".+?</a>#";
        $content = preg_replace($reg, "[audio mp3='$1' class='asset-audio' title='']", $content);
    
        return $content;
    }

    I’m guessing that it shouldn’t be the_content, but I am really working beyond my actual understanding of WordPress. I don’t know if I should be putting a theme file name there or if there is some equivalent to the_content that targets responses in bbP.

    Feel free to criticize my regex as well. I’ve actually never used regex in WP/php before.

    BTW, my code has been adapted from here: https://stackoverflow.com/questions/55814952/how-to-generate-audio-shortcode-when-theres-an-mp3-in-a-link-tag-in-wordpress

    WP 5.8
    bbP 2.6.6

Viewing 9 replies - 1 through 9 (of 9 total)
  • @robin-w

    Moderator

    ok, 2 things I would say

    1. since this relates to bbpress attachments plugin specifically, it would be better to ask there – Milan should be able to help
    2. On looking at the attachments support forum I notice that you also asked about empty replies in that forum as well as this forum where I gave you a fix – it is very frustrating for plugin authors and helpers when they spend time on an issue only to find that the original poster has already fixed it, and if you post in multiple places and get a fix from one, it is good to update the other places and maybe even post the solution there as well to help others. It would be polite to do this ๐Ÿ™‚

    @bhkh

    Participant

    Thanks again for such a quick reply!

    1. since this relates to bbpress attachments plugin specifically, it would be better to ask there โ€“ Milan should be able to help

    So I just now tested putting the code (in this case a link) I’m trying to filter in the body of a reply in addition to having it be in the attachment (hadn’t thought about trying that before) but before I posted I saw <div class="bbp-attachments"> in the page source and so figured that attachments were something in core bbP, not just the GD bbPress Attachments plugin.

    And the result of that test now is that the link in the body of the reply is also not filtered. So I think I’m back to this being a bbP thing and not a GD bbPress Attachments.

    I notice that you also asked about empty replies in that forum as well as this forum where I gave you a fix โ€“ it is very frustrating for plugin authors and helpers when they spend time on an issue only to find that the original poster has already fixed it

    I totally get that! At the time, I had no idea what the response rate was going to be. Sometimes communities/companies reply within the hour (as you did, thanks!!) or if they will take weeks. So not being sure which was causing the problem, I did post in both places. Please forgive me. I was surprised that both places replied so quickly.

    It would be polite to do this

    I did! Within minutes of seeing his response. Wasn’t it visible to you? Should I have linked back to the forum as well? I’m happy to, I just thought the code was enough. I totally know the frustration of support posts that end with “Oh, never mind, I fixed it.” and no solution.

    Anyway, since I now know that my filter also doesn’t catch things in the body of the reply either (and I would like it to) are we back to this being a bbP thing? Are there other troubleshooting things I’m missing? I have disabled all other plugins and tried a default theme.

    @robin-w

    Moderator

    on 2. ok, I see this as just a question, with no response

    https://wordpress.org/support/topic/attachment-with-empty-text-message-fails/

    do you see it differently?

    @bhkh

    Participant

    Ah, I posted it also to his company forum. Honestly, I forgot I posted there. He didn’t reply there. I have just made a link to your solution. Thanks again!

    Do you know if there is a way to filter the content of replies in the same way as is working for my filter of the_content?

    @robin-w

    Moderator

    on 2. – no problem ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚ I recently spent several hours trying to fix an issue and when I posted all I got back was ‘already fixed elsewhere’ as the response, so this is a current hot button for me – apologies if I sounded irritable ๐Ÿ™‚

    on your filter – quite a few bbpress additional plugins authors use ‘bbp’ as a prefix to indicate the the class/code is bbpress related. bbpress itself does not have a class of bbp-attachments so suspect this is attachments plugin related.

    I would try Milan as a first port of call, he is usually helpful, but do come back if he can’t help.

    @bhkh

    Participant

    quite a few bbpress additional plugins authors use โ€˜bbpโ€™ as a prefix to indicate the the class/code is bbpress related.

    Oh, that’s really good to know. Thank you.

    I was able to find the event (is that the proper name for it??) bbp_get_reply_content and that allowed me to filter the reply content but not the attachment area. Then I changed the priority of the filter to 1000 and I was able to achieve my intended outcome. What I don’t know is if this is good coding practice or not. I thought I read somewhere that super high priorities are not a good idea. In any case, I did post to the plugin company’s forum to see if there is a better/more specific event to filter. I’ll report back what I learn.

    But a general bbPress question if I may. How is one supposed to learn about events such as bbp_get_reply_content? I did a search in the bbPress documentation and there are no results. I was only able to find it by discecting the GD bbPress Attachments code. I did come across a site called hookr.io. Is that the best resource?

    I recently spent several hours trying to fix an issue and when I posted all I got back was โ€˜already fixed elsewhereโ€™

    ๐Ÿ™ that’s a bummer.

    I really appreciate your work supporting the forum and bbPress. Open Source software is nothing without people who dedicate time to support users. Thank you and all who help here.

    @robin-w

    Moderator

    bbpress is very large, and finding the right bit of code not easy !!

    have you heard of notepadd++ ?

    Google it and install it.

    Then download bbpress 2.6 to your PC and unzip it.

    Then when you are looking in your browser at bbpress, you’ll usually find a class or ID wrapped around the code you want to change.

    In Notepad++ do ‘search>find in files’ and search the class ot ID name in the bbpress directory you created.

    That will get you to lots of bits of code which are usually around what you are trying to find

    I do this a lot !!

    @bhkh

    Participant

    That will get you to lots of bits of code which are usually around what you are trying to find

    I’ll give it a go, thanks!

    I heard back and GD bbPress Attachments doesn’t have anything I can hook into, so i will just have to increase the priority number.

    Thanks for your help

    @robin-w

    Moderator

    there’s no problem with using priorities, especially as you know what it is affecting, just keep in mind that if you add a further bbpress content realted plugin and it doesn’t do what you want it might be related to this

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Skip to toolbar