4

I'm trying to modify a snippet of code I found here for a wordpress website: http://support.membermouse.com/support/discussions/topics/9000007386

Here is the code:

<?php
add_filter( 'the_content', 'restrict_mm_access' );

function restrict_mm_access($content) {

if(is_super_admin()){
    return $content;
}

$moreLink = '<span id="more-' . get_the_ID() . '"></span>';

$contentData = explode( $moreLink, $content );

if ( count( $contentData ) > 1 ) {
    $return = $contentData[0];
    $return .= "[MM_Member_Decision isMember='false']";
    $return .= '<div id="access-restricted-box">
                    <h3>You have to be a member to access this content.</h3>
                    <h4>Already a Member?</h4>
                    <a href="/login/?redirect_to=' . get_the_permalink() . '" class="button">Log In for Access</a>
                    <h4>Not a Member Yet?</h4>
                    <a href="/join/" class="button">Register Now</a>
                </div>';
    $return .= "[/MM_Member_Decision]";
    $return .= "[MM_Member_Decision membershipId='2']";
    $return .= $contentData[1];
    $return .= "[/MM_Member_Decision]";

    return $return;
} else {
    return $content;
}
}
?>

I'm using the Member Mouse membership plugin to protect custom posts; so they are only displayed for paid members. Our blog postings are publicly available.

The above snippet works perfect for protecting blog postings - which isn't what I want. I want two custom post types protected (Tools, and Articles), and the blog postings to display to everyone.

I recognize that Custom post types are not posts - so there's the problem. I just don't know how to modify the snippet to work.

Any ideas?

2
  • since this is specific to your plugin, you will have to ask the author Apr 12, 2016 at 17:06
  • 1
    It is not specific to a plugin. The basic problem is WordPress specific and quite on topic. For a first question, this one is actually much better than usual.
    – fuxia
    Apr 12, 2016 at 18:56

1 Answer 1

3

Whenever the_content is called, there is a global WP_Post object. So you can look into that to find its post type:

if ( ! in_array( get_post()->post_type, [ 'tool', 'article' ] ) )
    return $content;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.