• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Filters » Blocking custom post types

Blocking custom post types

Chad Butler · Feb 22, 2013 ·

Note: As of WP-Members 3.0, you can add custom post types to the plugin’s main blocking options. See the options documentation section on Custom Post Types.

With explosion of custom post types in WordPress 3.0, the WordPress world has changed somewhat from what it was when WP-Members was first developed.  If you have a theme or site that uses custom post types, you may find that you need to make some adjustments to accommodate what content you would like to be blocked.

This article is only available to WP-Members Support Subscribers. If you have an existing subscription, please login below. If you do not have a current support subscription, you can purchase a support subscription here.

Already a Member? Log In Here
   
Forgot password? Click here to reset

To gain full access to WP-Members premium content, a current support subscription is required. You can purchase an annual support subscription for as little as $59, which provides you with access to priority support, a customer support forum, access to code snippets, and more.

Why wait? Choose your subscription option here.

[
Why join?]

Filters, Tips and Tricks filters, get_post_type, wpmem_block

Welcome to RocketGeek Interactive › Forums › Blocking custom post types

Tagged: filters, get_post_type, wpmem_block

  • This topic has 28 replies, 9 voices, and was last updated 6 years, 6 months ago by Chad Butler.
Viewing 24 reply threads
  • Author
    Posts
    • February 22, 2013 at 9:56 am #2091
      Chad Butler
      Keymaster

      With explosion of custom post types in WordPress 3.0, the WordPress world has changed somewhat from what it was when WP-Members was first developed.  
      [See the full post at: Blocking custom post types]

    • March 8, 2013 at 12:01 pm #2227
      mortenballe
      Participant

      Hi Chad

      Thanks for writing this post.., exactly what I lokked for…

      However I can’t get it to do the trick. I’ve put the code-snippet in the bottom of the functions.php in my themes rootfolder… Changed it to this:

      // BLOCK FAQ PAGES

      add_filter( ‘wpmem_block’, ‘block_custom_post_type’ );
      function block_custom_post_type( $block )
      {
      if( get_post_type( $post->ID ) == ‘faq’ ) {
      $is_faq = false;
      } else {
      $is_faq = true;
      }

      if( is_single() && $is_faq ) {
      return false;
      }

      return;
      }

      in order to block faq elements (faq-plugin: Arconix FAQ)

      They are however not blocked, and the rest of my blocked posts/pages ar opened instead.

      Is it me..??

      Best,

      Morten Balle

       

       

    • March 12, 2013 at 3:50 pm #2248
      leakylabs
      Participant

      i have the same problem 🙁

    • May 2, 2013 at 10:56 am #2507
      Englishbydesign99
      Participant

      Hi Chad,

      Is there any way this can be adapted to work with Advanced Custom Fields plugin?

      Love WP-Members thank you!

      Cheers

      David

    • May 28, 2013 at 8:24 pm #2655
      fullcontactsites
      Participant

      What about the opposite?

      I have wp members blocking posts by default but on some custom post types I want all the posts unblocked.

      Right now I have the landing-pages plugin installed and running but the the wp-members login shows up on every landing page.

      add_filter( ‘wpmem_block’, ‘block_custom_post_type’ );
      function block_custom_post_type( $block )
      {
      if( get_post_type( $post->ID ) == ‘landing-page’ ) {
      $is_books = true;
      } else {
      $is_books = false;
      }

      if( is_single() && $is_landing-page ) {
      return true;
      }

      return;
      }

      What needs to be changed to have every post of this custom post type unblocked?

    • May 28, 2013 at 9:56 pm #2656
      fullcontactsites
      Participant

      Figured it out…    what about it there are 2 custom post types?

      add_filter( ‘wpmem_block’, ‘block_custom_post_type’ );
      function block_custom_post_type( $block )
      {
      if( get_post_type( $post->ID ) == ‘landing-page’ ) {
      $is_books = true;
      } else {
      $is_books = false;
      }

      if( is_single() && $is_landing-page ) {
      return false;
      }

      return;
      }

    • May 28, 2013 at 11:21 pm #2657
      Chad Butler
      Keymaster

      Actually, if you wanted every post of a specific custom post type to be unblocked, you shouldn’t have to do anything as the plugin does not by default deal with custom post types.  Did you find that to be the case with the landing pages?

    • May 28, 2013 at 11:26 pm #2659
      fullcontactsites
      Participant

      Yes the body of every landing page had the login dialog

      Other weird thing was in the code…   I added the following to my functions.php of my child theme in pagelines.com framework… and it works, the dialog is no longer displayed…

      eg. http://www.fullcontactsites.com/go/blogging-secrets/

       

      add_filter( ‘wpmem_block’, ‘block_custom_post_type’ );
      function block_custom_post_type( $block )
      {
      if( get_post_type( $post->ID ) == ‘landing-page’ ) {
      $is_books = true;
      } else {
      $is_books = false;
      }

      if( is_single() && $is_landing-page ) {
      return false;
      }

      return;
      }

       

       

      But if I change the above code where is reads $is_books   to $is_landing-page  the functions.php file won’t close and WordPress hangs with a white screen.

      ???? Don’t know why does it matter?

    • May 29, 2013 at 8:51 am #2661
      Chad Butler
      Keymaster

      Ahhh…  $is_landing-page is your problem.  You can’t use “-” in variable names in PHP.  It is read as a subtraction operator.  $is_landing_page would be valid.

    • May 31, 2013 at 1:05 pm #2689
      fullcontactsites
      Participant

      After I go this working.. I noticed that wp-members is NOT blocking any posts now ????

      So the landing pages plugin, is a custom post type and I understand that wp-members isn’t supposed to add the login dialog… but it does.

      I added the filter to not block the landing page type and that worked… but then every post was unblocked.

      Here’s what I’ve added to functions.php
      add_filter( ‘wpmem_block’, ‘block_custom_post_type’ );
      function block_custom_post_type( $block )
      {
      if( get_post_type( $post->ID ) == ‘landing_page’ ) {
      $is_landing_page = true;
      } else {
      $is_landing_page = false;
      }

      if( is_single() && $is_landing_page ) {
      return false;
      }

      return;
      }
      Suggestions?

       

    • May 31, 2013 at 1:35 pm #2690
      fullcontactsites
      Participant

      Think I figured it out… Don’t need the filter.  Pagelines is the theme that I’m using and it adds a “content” section to every post.  I had to create a template for “landing Pages” and disable the content section.

    • May 31, 2013 at 2:36 pm #2691
      Chad Butler
      Keymaster

      I’ll need to double check this, but I think you might have uncovered a flaw in the sample code from the post.  I think at the very end should return the value of $block.  So instead of:

      return;

      it should be:

      return $block;

      Otherwise, the securify function is getting an empty value back if the post does not fit the criteria for the landing page.

    • May 31, 2013 at 3:50 pm #2692
      fullcontactsites
      Participant

      I’m stumped..

      I added the filter to the pagelines child theme functions.php file

      add_filter( ‘wpmem_block’, ‘block_custom_post_type’ );
      function block_custom_post_type( $block )
      {
      if( get_post_type( $post->ID ) == ‘landing-page’ ) {
      $is_landing_page = true;
      } else {
      $is_landing_page = false;
      }

      if( is_single() && $is_landing_page ) {
      return false;
      }

      return$block;
      }

       

      I removed the “loop section and content section” out of the landing pages template and the wp-members login dialog is still showing up – argh.http://fullcontactsites.com/go/blogging-secrets/

      Any other suggestions?

       

    • June 1, 2013 at 8:57 am #2694
      Chad Butler
      Keymaster

      I took a look at that page and I am not seeing the login dialog. I do see the signup form you have, but that’s not the WP-Members login.

      Am I missing something?

    • June 1, 2013 at 9:08 am #2695
      fullcontactsites
      Participant

      I had it deactivated hang try now…

    • June 15, 2013 at 3:20 pm #2751
      ajs
      Participant

      Chad,

      I have confirmed that:

      return $block;

      is required. Figured it out after spending several hours trying to figure out why none of my pages or posts were blocked. After a few hours of banging my head against the desk, I decided to come back here and read the entire set of comments. . .

      Any chance you can fix the code example in the original post so others don’t go through this same ordeal?

    • June 15, 2013 at 4:58 pm #2752
      Chad Butler
      Keymaster

      Aron – I’ve updated the snippet – I’m not sure why that wasn’t updated earlier.  I’m really sorry about that.

    • September 17, 2013 at 5:23 pm #3077
      focusedcreative.com
      Participant

      Hi

      I tried the above code (the name of the custom post type is “wholesaler_specials”, so that is where the “books” is), but the WP-Members Meta box does not appear on the editor page.

      I’m using the amazing Advanced Custom Fields and Custom Posts UI plugins to create the custom post type ( that also will use some custom fields which all work fine). I don’t have any of the editor parts hidden.

      Is there something we have to do to have the “WP-Members meta box show up on the editor page of a custom post type? Shouldn’t there be another filter or hook to allow this?

      thanks

    • September 17, 2013 at 5:37 pm #3078
      Chad Butler
      Keymaster

      You bring up a good point.

      The snippet example given here operates under the assumption that you are blocking a specific custom post type.  However, it doesn’t really get into the question of unblocking certain individual

      But before we go down that road, let me ask this question.  Are you asking about the meta box because you would want to unblock certain specific posts that are part of this group?  The meta box only operates on the individual post level in order to simplify the process of setting an individual post to be the opposite of the default (i.e. if posts are blocked, then the meta box will be to set that individual post as unblocked).  If, in the case of your custom post type, they will all be blocked and you don’t need to unblock individual items, the meta box would not be needed.

      I haven’t really considered unblocking individual items with a blocked custom post type, mainly because no one has asked up until now.  It can certainly be done, but I’ll need to play around and update the snippet to incorporate that if that’s what you need.

    • September 18, 2013 at 11:56 am #3079
      focusedcreative.com
      Participant

      Hey Chad

      Actually in this case blocking the custom posts is what I want. I was just expecting the meta box like in the posts.

    • September 18, 2013 at 2:47 pm #3084
      Chad Butler
      Keymaster

      Ahhh… blocking the custom post type should be all OK then with this code snippet (let me know if it’s not working out for you).

      But, I like the track you got me thinking on, so I will be putting together a more advance version of this in the future to accommodate instances where some custom posts need to be the opposite of the default.

    • June 20, 2014 at 11:29 am #5202
      bp@jeden.pl
      Participant

      Hi,
      I’ve added code to my functions.php and I’ve blocked WooCommerce products as my clients wants to hide product details from not registered users. Form shows but below content. Content is still visible. How can I fix this?

      • June 26, 2014 at 9:35 am #5239
        Chad Butler
        Keymaster

        I am working on some integration projects – both have similar issues – Woocommerce and Page Builder plugins. Both of these things use a lot of post meta for extra data that display; and in the case of Woocommerce, special templates are used.

        As a note to myself (and others reading this thread later), Błażej and I have conversed via email on the Woocommerce issues and I think we’ve got some of this resolved following some techniques on working directly with the templates.

        As I write this, we are close to releasing 2.9.4 and I am building the framework for 3.0. I am considering these two types of plugins in the 3.0 build, although they may get addressed directly in a 2.9.x release for robustness testing.

        To reiterate a development policy that I have, I avoid building in specific compatibility for specific plugins. Woocommerce would fall into this. However, Woocommerce is widely used and I am getting more and more support questions about it. In reviewing some of these issues, I have found that some improvements to WP-Members may actually cover some of the Woocommerce integration as well as page builder plugins; kind of killing two birds with one stone. So this is a case were I would definitely seek to improve the plugin for a broader compatibility.

    • August 8, 2014 at 11:51 am #5602
      webcami
      Participant

      I want to block 5 posts types. I can’t seems to get it to work! How can I do multiple with this code example?

      • August 8, 2014 at 12:06 pm #5603
        Chad Butler
        Keymaster

        I would approach it by adding all my custom post type tags to an array and then changing the logical test to see if the current post type is in the array. Something like this:

        add_filter( 'wpmem_block', 'block_custom_post_type' );
        function block_custom_post_type( $block )
        {
        	global $post;
        	
        	$blocked_post_types = array(
        		'books',
        		'music',
        		'video',
        		'media',
        		'misc'
        	);
        	
        	if( in_array( get_post_type( $post->ID ), $blocked_post_types ) ) {
        		$is_books = true;
        	} else {
        		$is_books = false;
        	}
        	
        	if( is_single() && $is_books ) {
        		return true;
        	}
        	
        	return $block;
        }

        Note the only real difference here is that I added an array called $blocked_post_types and then instead of the original logical test, I am using in_array() to see if the result of get_post_type( $post->ID ) is in the $blocked_post_types array.

        Then everything else continues as before…

    • August 10, 2014 at 3:36 pm #5613
      webcami
      Participant

      I can’t seem to get this to work. Please take a look below (any help is greatly appreciated) I have shared the code that works for ONE CUSTOM POST TYPE (newsoct). Below is the code I created from the example above for more than one custom post type and it breaks the functions.php with a parse error:

      • August 10, 2014 at 4:34 pm #5617
        Chad Butler
        Keymaster

        The problem was with the original code that I posted. This line is incorrect:

        if( in_array( get_post_type( $post->ID ), $blocked_post_types ) {

        It is missing a closing parentheses:

        if( in_array( get_post_type( $post->ID ), $blocked_post_types ) ) {

        (Also, in both your code examples (which I actually removed just for readability of the thread – sorry), the first block filter that is just the code from the hook’s documentation doesn’t do anything other than return an unfiltered value – you can take that out.)

        Hope that helps.

    • August 11, 2014 at 12:00 pm #5633
      webcami
      Participant

      That did the trick. Thank you so much for your timely response!

      • August 11, 2014 at 1:33 pm #5634
        Chad Butler
        Keymaster

        No problem – I’m actually glad you had the issue or I wouldn’t have noticed the original code snippet I posted had an error 😉

  • Author
    Posts
Viewing 24 reply threads
  • You must be logged in to reply to this topic.
Log In

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy