Customer Capability php code for pages (as opposed to posts)

The following code is based on examples in s2Member documentation. Since categories and tags appear to be post specific, is there a creative way to accomplish this type of custom capability logic for pages?

if (is_category (“music”) && !current_user_can (“access_s2member_ccap_music”))
{
header ("Location: ". S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
exit ();
}

Thank you!

Hi james,

That conditional seems fine. I’m not sure I understand what your question is, though. Do you mean checking that condition for all pages, without adding it to each individually? If so, you’d need to find a hook/filter when showing a page, and add your hack there.

:slight_smile:

Hi Cristian,

My assumption was category labels and the if_category function only works for posts. In my situation I’m using pages. If if_category also works on pages how do I add a category to a page?

(I only showed a snippet of the code. Code including filter/hook per s2Member documentation example was added to the theme’s functions.php file.)

Thanks for you help.

James

I haven’t tried it, but maybe with a plugin that lets you use WP’s taxonomy with pages. I think this one does that: https://wordpress.org/plugins/simple-tags/

Then try the category or tag restriction and see if it gets applied to those pages.

Let me know how it goes. :slight_smile:

Hi Cristain,

I tried a couple different plugins for adding categories to pages. I ended up on Taxopress. All plugins tested seemed to do a good job adding categories to pages. But the php code kept failing.

After spending time debugging the php code it became apparent the problem was the is_category function. It was always returning false even if it appeared it should return true.

After doing some research I tried using the in_category function instead. Using this function, the php code began working as intended. Solved.

On a related note, while poking around at all the category screens in WordPress, I stumbled across the “Require Custom Capabilities?” field on the WordPress Page -> Edit -> sidebar. I had not noticed that before. It provides the same functionality I was originally looking for without the php code. Would have saved me a few hours of php debugging!

Thanks for your help,

James

1 Like

Hi James,

Thanks for the update! Well done sorting that one out.

I see, I thought you tried the editor’s s2 box first, and then looked for a custom solution, maybe for the more complex condition, or to apply it to many pages in bulk.

:slight_smile: