Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter ballooninging

    (@ballooninging)

    The filters work is that you ALWAYS have to return the “content” that is passed to your function via the filter (whether you change it not).

    If you don’t return anything then that means you modified the content and returned and empty string (because that’s allowed if that’s what you want to do).

    So if you don’t return anything, it means you want to show nothing which is what it was doing when you weren’t returning it correctly.

    Right, got it. Thanks much!

    Thread Starter ballooninging

    (@ballooninging)

    Found the issue – I was not returning $smb_content at the end of the conditionals. Once that was returned, the login and registration pages began displaying the correct output:

    add_filter('the_content','smb_replace_content', 1, 1);
    
    function smb_replace_content($smb_content) {
        if (is_single()) {
            if(!SwpmMemberUtils::is_member_logged_in()) {
                $smb_content = preg_replace('/<a href="https:\/\/docs.google.com\/spreadsheets\/d\/.+"/', '<a href="#" hoho' ,$smb_content);
            }
        } return $smb_content;
    }

    I also removed the redundant “else” statement – I was wondering if I needed to specifically return $smb_content, but it turns out that I just needed to return it outside the conditionals.

    Just curious, any idea why this is the case? Am happy to learn more about WP development.

    Thread Starter ballooninging

    (@ballooninging)

    Hello,

    I did up a very simple, rough plugin that does the following:

    1. Identifies if the current web page is a “post”
    2. If it is, and if the link matches the regex, replace the link with another URL (it’s currently a placeholder)
    3. If not, does nothing.

    Code as shown below:

    add_filter('the_content','smb_replace_content');
    
    function smb_replace_content($smb_content) {
        if (is_single()) {
            if(!SwpmMemberUtils::is_member_logged_in()) {
                $smb_content = preg_replace('/<a href="https:\/\/docs.google.com\/spreadsheets\/d\/.+"/', '<a href="#"' ,$smb_content);
                return $smb_content;
            } else {
                return $smb_content;
            }
        }
    }

    This works well so far. However, I realised that this line:

    add_filter('the_content','smb_replace_content');

    is conflicting with the simple-membership-plugin, such that it refuses to display any content on the login page and the registration page.

    Is it a priority issue? Were there cases where anyone has tried to extend this plugin by filtering through the the_content hook, and encountered this error?

    Any possible ways this may be resolved?

    Thanks in advance!

    Thread Starter ballooninging

    (@ballooninging)

    Hi, you might like to check the following documentation. Here you will find some PHP tweaks that will help you with what you are trying to achieve.

    Thanks! I’ve already went through this (which was how I got the utility SwpmMemberUtils::is_member_logged_in()), I’m currently stopped by how to implement this in the first place – will keep this thread updated on how I solved this problem.

Viewing 4 replies - 1 through 4 (of 4 total)