• 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 » Using wpmem_block to block a category

Using wpmem_block to block a category

Chad Butler · May 24, 2012 ·

Suppose you want to block any post that is in a certain category.  This example shows you how to do that using the wpmem_block filter hook.

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 category, filters, hooks, wpmem_block

Welcome to RocketGeek Interactive › Forums › Using wpmem_block to block a category

Tagged: category, FeedWordPress, filters, hooks, wpmem_block

  • This topic has 20 replies, 8 voices, and was last updated 3 years, 6 months ago by Chad Butler.
Viewing 10 reply threads
  • Author
    Posts
    • May 24, 2012 at 1:41 pm #679
      Chad Butler
      Keymaster

      Suppose you want to block any post that is in a certain category.  This example shows you how to do that using the wpmem_block filter hook.
      [See the full post at: Using wpmem_block to block a category]

    • June 18, 2013 at 12:24 am #2755
      PeterGray
      Participant

      The last filter will set the value $blocked_cat to true or false based on the last category found. I think the following is probably more what was intended:

      add_filter( ‘wpmem_block’, ‘my_blocked_cat’ );
      function my_blocked_cat( $block )
      {
      /**
      * This example blocks any post that is in the category ID 6.
      * You could use this example as-is and adjust the cat_ID
      * number based on the cat_ID you wish to block.
      *
      * This snippet is for situations where a blocked post might
      * exist in the blocked category AND an unblocked category.
      */

      global $post;
      $category = get_the_category( $post->ID );
      $blocked_cat = false;
      foreach( $category as $cat ) {
      $blocked_cat = ( $cat->cat_ID == 6 ) ? true : $blocked_cat;
      }

      $block = ( is_single() && ( $blocked_cat ) ) ? true : false;

      return $block;
      }

    • June 18, 2013 at 1:57 pm #2758
      Chad Butler
      Keymaster

      Good point, Peter.

      I think that putting in a break of the foreach loop if $blocked_cat is true would do it as well.  Once you get a true value, there’s no need to continue. This foreach loop in the original (2nd) snippet would probably do it:

      foreach( $category as $cat ) {
      $blocked_cat = ( $cat->cat_ID == 6 ) ? true : $blocked_cat;
      if( $blocked_cat )
      break;
      }

    • August 9, 2013 at 12:11 pm #2975
      HelloBrandy
      Participant

      How would I adjust this code to block a custom category? For example, I am using  WooCommerce and I need to block one of the product categories I have created named Resellers. The cat ID for this goes product_cat=resellers.

      Thank you!

    • August 9, 2013 at 10:22 pm #2979
      Chad Butler
      Keymaster

      Hello Brandy 😉

      I think you could use a slightly modified version of the example code presented, where instead of just looking for the category ID number, you use get_cat_name to get the category name, and then if it is “resellers”, return a $block value of true.  That would look something like this:

      
      add_filter( 'wpmem_block', 'my_blocked_cat' );
      function my_blocked_cat( $block )
      {
      	global $post;
      	$category = get_cat_name( $post->ID );
      	$block = ( is_single() && $category == 'resellers' ) ? true : false;
      	return $block;
      }
      
    • June 11, 2014 at 6:07 am #5083
      bardiasol
      Participant

      In which file should these hooks be placed?

      • June 11, 2014 at 4:36 pm #5087
        Chad Butler
        Keymaster

        Most times, custom functions will live in your theme’s (or child theme’s) functions.php file.

    • July 8, 2014 at 6:34 am #5335
      anvigo
      Participant

      How can I unblock one or more categories? (by default all category are blocked)

      • July 8, 2014 at 1:05 pm #5338
        Chad Butler
        Keymaster

        So to clarify the code snippet assumes that posts are unblocked by default and you want to block a specific category by ID.

        To do the opposite – you have posts blocked by default but want a specific category to be unblocked, I think you could reverse the true/false logic. So change:

        ? true : false;

        to this:

        ? false : true;

    • October 31, 2014 at 8:52 am #6280
      dreamsight
      Participant

      Is it possible to block all child categories under the main block a category as well?

      • October 31, 2014 at 3:02 pm #6287
        Chad Butler
        Keymaster

        I looked at a couple different possible methods to do this and I was unsuccessful (so far!). I’ll look at this some more as I’m sure I was missing something. My initial thought is to test an array of categories, but I also tried testing to see if the parent category was blocked.

        I am working on integrating category blocking into the main body of the plugin for (hopefully) inclusion in either 2.9.8 or 2.9.9 so your question on looking into this is timely.

        • November 3, 2014 at 5:55 am #6312
          dreamsight
          Participant

          Thanks for getting back so quickly. How do I add multiple categories? I’ve tried adding all the cat_ID’s of the child categories, separated by comma and it didn’t work

          • November 3, 2014 at 2:38 pm #6328
            Chad Butler
            Keymaster

            I had set out to answer the child category and multiple category but hit a wall when I was testing. It turned out to be something really stupid on my part – one of those things where I just could not get it to work correctly and it turned out to be something I had set up incorrectly. Doh!

            Anyway, I got past that and put together a snippet that handles multiple categories. Not only that, I think it is pretty close to cut-and-paste code – all you need to do is update the array of blocked post IDs. This snippet has been added to the post.

          • November 4, 2014 at 6:02 am #6338
            dreamsight
            Participant

            Thanks for the quick support response. It worked a treat

          • November 4, 2014 at 9:24 am #6348
            Chad Butler
            Keymaster

            Great!! That’s good news as I am working to incorporate some of this directly into the plugin. The main part that is left undone as of yet is to build it into the admin.

    • February 9, 2015 at 4:22 pm #7150
      jkirker
      Participant

      Currently blocked posts will still show up on the main categories page along with the summary or lead into the post. Is there a hook that will also stop these blocked articles or blocked categories from being listed on the category pages?

      • February 9, 2015 at 11:10 pm #7158
        Chad Butler
        Keymaster

        There is a way to do this via pre_get_posts. I’ve been planning a post on this subject because, while not overly complicated, it requires careful consideration depending on how the plugin is configured and what is intended to be shown/hidden. I’ll get something together and posted on this.

    • February 10, 2015 at 11:30 am #7163
      jkirker
      Participant

      Great – thanks Chad! You’ve always been so awesome at supporting us! John

      • February 10, 2015 at 12:55 pm #7169
        Chad Butler
        Keymaster

        No problem John. This is one I’ve meant to get published for awhile and just hadn’t finished it up. So thanks for asking because it motivated me to get it finished.

        https://rocketgeek.com/tips-and-tricks/how-to-hide-posts-completely/

        Categories is the first example.

    • August 21, 2017 at 10:41 am #13550
      Uwe
      Participant

      Hi Chad,
      we like to use the Handling Multiple Categories, but there is one category we can not block. This category includes posts imported with FeedWordPress-Plugin.

      The posts look normal and are imported completely.
      Any idea why we can not block this category ?
      We look several times for the correct cat-ID.

      • August 21, 2017 at 3:27 pm #13551
        Chad Butler
        Keymaster

        I took a look at the plugin – it appears that it runs a filter on the_content. This happens to be the filter that WP-Members also hooks to in order to replace blocked content with the login form. WP-Members runs at a late point (99). Feedwordpress runs at a priority 10000. Not sure why they picked such a ridiculously late priority but that basically means that it inserts content after the WP-Members process has run.

        Basically, you can work around the process by reapplying WP-Members’ the_content filter later (after their filter has run):

        add_action( 'template_redirect', function(){
        	global $wpmem;
        	add_filter( 'the_content', array( $wpmem, 'do_securify' ), 10001 );
        });
  • Author
    Posts
Viewing 10 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