There are times when you’re in a rush and need to apply something quite fastly, to suit client’s needs, right?

This happened to me – I was working on a solution with s2Member plugin and I had to use its filter hooks to change its behavior quite drastically.

By default s2Member allows blocking access to certain pages if they haven’t been paid for. This had to be filtered – a visitor had to be able to see that page, mainly its teaser content.

Unfortuntely s2Member had only one conditional function to check whether page is blocked or not, and if it was it’s been doing the rerouting process – I had to use the same exact function to display / hide paid content.

Here’s what I did.

1. I created a mu-plugin directory in which I placed s2hacks.php file.

2. Inside the s2hacks.php file I placed the following code:

[php] /**
* Force s2member to always return true, disabling redirections to Membership Options Page
* @todo check if works in different circumstances
*
* @hook ws_plugin__s2member_sp_access_excluded
* @hook ws_plugin__s2member_sp_access
* @return boolean
*/
function jl_s2member_allow_specific_access() {
return true;
}

add_filter(‘ws_plugin__s2member_sp_access_excluded’, ‘jl_s2member_allow_specific_access’ );
add_filter(‘ws_plugin__s2member_sp_access’,’jl_s2member_allow_specific_access’ );
[/php]

This caused the c_ws_plugin__s2member_sp_access::sp_access( $id ) funciton to return true all the time, disabling the redirections to membership page.

The problem was that I needed to use this function on that page, to actually check if the user has paid for the access or not – member who did would see the paid content, and the one who didn’t would see some teaser videos.

So how did I overcome that? Inside the content-jl_course.php file (which was a page template used for courses) I placed the following filters at the top:

[php]
remove_filter( ‘ws_plugin__s2member_sp_access_excluded’, ‘jl_s2member_allow_specific_access’ );
remove_filter( ‘ws_plugin__s2member_sp_access’, ‘jl_s2member_allow_specific_access’ );
[/php]

This little trick allowed me to use the same function in two different ways without any core changes.

Sample usage

1. Temporary content filtering, can be used if we display content on the same page couple of times (for example in nested loops)

[php]
echo the_content();
remove_filter(‘the_content’, ‘filter_function_name’); ?>
[/php]