latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Give specific post/page access with PHP

Home Forums Community Forum Give specific post/page access with PHP

This topic contains 7 replies, has 2 voices. Last updated by  Cristián Lávaque 4 years, 9 months ago.

Topic Author Topic
Posted: Tuesday Mar 13th, 2012 at 7:55 pm #8060

I’d like to write a plugin that will grant specific post/page access to certain members. I found lots of documentation on how to determine IF users have access to content, but I’m trying to actually set that answer with code. Is this possible?

List Of Topic Replies

Viewing 7 replies - 1 through 7 (of 7 total)
Author Replies
Author Replies
Posted: Wednesday Mar 14th, 2012 at 4:54 am #8096

Hi Brett.

I researched this for a bit and found a hook you can try: [hilite mono]"ws_plugin__s2member_sp_access_excluded"[/hilite].

http://www.s2member.com/codex/stable/source/s2member/includes/classes/sp-access.inc.php/#src_doc_line_103

Notice it has 3 arguments, which is important when you use add_filter.
http://codex.wordpress.org/Function_Reference/add_filter

I hope it helps. :)

Posted: Wednesday Mar 14th, 2012 at 2:55 pm #8146

I’m going to give this a try. Do you have any idea where this happens in the execution order? More to the poin, I’m wondering if I can add an action to the “template_redirect” hook in which I add this filter. Obviously, “template_redirect” has to happen BEFORE the filter gets applied for this to work.

Posted: Wednesday Mar 14th, 2012 at 6:02 pm #8172

No, I’m not familiar with the execution order for it, but I’d guess it happens before a redirection because the access restriction is checked before redirecting a person for not having the right access for that content.

Posted: Wednesday Mar 14th, 2012 at 8:16 pm #8187

I just realized that my question was a little bit strange. I was thinking in terms of do_action (which would need to get called in the right order) but this hack is using a filter, so it doesn’t need to be wrapped in another hook…

Good news:
It looks like the filter strategy is going to work. I couldn’t get it to work with “ws_plugin__s2member_sp_access_excluded” but I AM having luck with “ws_plugin__s2member_sp_access” (same link as above, just a couple lines lower in the code).

For anyone else who stumbles upon this thread, my code looks something like this:

//my custom capability is named "student"
add_filter("ws_plugin__s2member_sp_access", "special_access",1, 3 );

function special_access() {
		
		global $post;
		$current_id = $post->ID;
		$post_type = get_post_type($current_id);
		
		//add conditional test for whether stop-student is true/false
		if ( current_user_can("access_s2member_ccap_student") ) {
			return true;
		} else {
			//tough luck buddy
		}
}
Posted: Wednesday Mar 14th, 2012 at 8:44 pm #8190

But, that hook is after the page was excluded from the Specific Page restriction.

Did you try something like this with the hook I mentioned before?

[hilite pre_code]
add_filter('ws_plugin__s2member_sp_access_excluded', 'special_access', 1, 2);

function special_access($excluded, $vars) {
if (current_user_can('access_s2member_ccap_student')) {
$excluded = true;
return $excluded;
}
[/hilite]

or even

[hilite pre_code]
add_filter('ws_plugin__s2member_sp_access_excluded', 'special_access', 1, 2);

function special_access($excluded, $vars) {
return (current_user_can('access_s2member_ccap_student') ? true : $excluded);
}
[/hilite]

Posted: Sunday Mar 18th, 2012 at 9:24 am #8485

Huh. I’m going to try your version, but let me clarify one thing. Your code blocks students, right? I think I need to reverse it to allow students by setting $excluded to FALSE. Thanks for your help.
EDIT: No, your code works as it was written. I guess I need to revisit the source to better understand what’s going on.
EDIT: Right. I got confused by the variable name “excluded”. It doesn’t determine whether or not the user IS (=true) or IS NOT (= false) excluded from accessing the specific post/page. Rather the opposite. It determines whether the user is excluded from the sp post/page access requirements.

  • This reply was modified 4 years, 9 months ago by  Brett Adcock.
  • This reply was modified 4 years, 9 months ago by  Brett Adcock.
Posted: Tuesday Mar 20th, 2012 at 5:45 am #8591

Cool! Thanks for reporting back. Glad I could help. :)

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

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.