How to filter WordPress post content

Sometimes there is a need to show or hide parts of a post or page content based on who browses your website. With free AAM 4.0 or higher you can archive this with the shortcode [aam].

If you are copying and pasting shortcode from our website, make sure that correct quotes are preserved. Sometimes, depending on the operation system, quotes are not transferred correctly and you would have to manually change them.

For example if you want to exclude part of your page’s content for all visitors you can simply wrapped restricted content with following shortcode:

[aam hide="visitor"]...[/aam]

Another example is when you want to show for your subscribers something like “Please upgrade your account to the Contributor in order to read more”, then wrap the content with following shortcode:

[aam limit="subscriber" message="Please upgrade your account to the Contributor in order to read more"]...[/aam]

There are three attributes show, limit and hide for the shortcode that define content visibility. These attributes may contain user ID, role ID, user IP address or reserved key all (means all users including visitors). You can find out user and role IDs on the AAM page as shown on the screenshot below.

For your information! The visibility attributes have priorities where the lowest priority is hide, then limit and finally show. This mean that the attribute with higher priority will overwrite attribute with lower priority. For example you may want to hide content for all users and visitors, however would like to display “Please upgrade your account to the Advanced role” and show full content for users with Advanced role. In this case you can wrap your content in the shorcode like this:
[aam hide=”all” limit=”subscriber” message=”Please upgrade your account to the Advanced role” show=”advanced”]…[/aam]

In case you need to filter content based on the user IP address, you can define either full IP address, IP mask or range or IP addresses. For example: 04.75.230.12, 192.123.* or 15-34.10.10.1.

Shortcode attribute reference

show

Comma-separated list of user IDs, role IDs, user IP addresses to show wrapped content to. Use all to show for all users. Use visitor to show content to visitors.

Example. Show part of the content for all visitors.

[aam show="visitor"]...[/aam]

hide

Comma-separated list of user IDs, role IDs, user IP addresses to hide wrapped content to. Use all to hide for all users. Use visitor to hide content to visitors.

Example. Hide content for all subscribers and user with ID 4 however show the content for all editor, contributors and administrators.

[aam hide="subscriber,4" show="editor,contributor,administrator"]...[/aam]

limit

Comma-separated list of user IDs, role IDs, user IP addresses to limited wrapped content to. Use all to limit for all users. The message attribute has to be defined when limit attribute is set because the defined message is used literally to replace limited content.

Example. Replace content with the message “Please sign up in order to read the remaining article” for all visitors.

[aam limit="visitor" message="Please sign up in order to read the remaining article"]...[/aam]

message

A custom message the wrapped content is replaced with when access is limited. See the limit attribute for more information.

callback

Custom PHP callback function that will process the wrapped content. The callback function accepts one argument which is the instance of AAM_Shortcode_Strategy_Content class. The instance contains shortcode attributes and wrapped content.

Example. Process the wrapped content with custom callback function processContent.

[aam callback="CustomFilter::filterContent"]
class CustomFilter {

    /**
     * Filter post content
     * 
     * Based on user IP address, show different HTML content
     * 
     * @param AAM_Shortcode_Strategy_Content $shotcode
     * 
     * @access public
     * @return string
     */
    public static function filterContent(AAM_Shortcode_Strategy_Content $shotcode) {
        $ip      = AAM_Core_Request::server('REMOTE_ADDR'); //AAM API: $_SERVER[REMOTE_ADDR]
        $content = $shotcode->getContent();
        
        if (preg_match('/^65\.', $ip)) {
            //loadPartial method simply loads specified relative file path
            $content = $this->loadPartial('usa.html');
        } elseif (preg_match('/^4\.', $ip)) {
            $content = $this->loadPartial('switzeland.html');
        }
        
        return $content;
    }
}

Conclusion

With AAM shortcode you have additional flexibility to manage access not only to post itself but also to very specific parts of the content; and you can do that for any individual user, role, visitors as well as based on user’s IP address.

Related Forum Topics

Get notified about important updates and new features (no more than one email per month).