WordPress protecting custom fields (amember_filter_posts)

Discussion in 'Setting-up protection' started by amcdermott, Apr 14, 2011.

  1. amcdermott

    amcdermott aMember Pro Customer

    Joined:
    Feb 9, 2011
    Messages:
    26
    I'm using the latest beta Amember WordPress plugin. I have my primary content in WordPress Custom Post Type (in several custom fields). I want to protect some of these fields only. It looks like the protection is happening in amember.php:

    Code:
    add_filter("the_posts", "amember_filter_posts");
    Has anyone successfully modified the plugin to allow protection of certain custom fields?
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Not sure I understood how this should work?
    Do you want to display these fields only for active users? If so this is possible to setup such protection in theme template for that custom post.
    Can you provide more info about how such protection should work?
  3. amcdermott

    amcdermott aMember Pro Customer

    Joined:
    Feb 9, 2011
    Messages:
    26
    Hi alexander; yes, that's exactly what I want to to.

    I have a custom post type Lesson.

    Lesson has several custom fields:

    • Module
    • Objective
    • Steps
    • Video ID

    In my theme (specifically, in single-lesson.php) I have code like this (simplified for this example):

    Code:
    <?php
    /**
     * Template Name: Single Lesson 
     *
     * A custom page template for lessons
     */
    $local_lesson_module = get_post_meta($post->ID, 'lesson_module', TRUE);
    $local_lesson_objective = get_post_meta($post->ID, 'lesson_objective', TRUE);
    $local_lesson_steps = get_post_meta($post->ID, 'lesson_steps', TRUE);
    $local_lesson_video_id = get_post_meta($post->ID, 'lesson_video_id', TRUE);
    
    get_header(); 
    ?>
    <div id="container">
    <h1 class="entry-title"><?php the_title(); ?></h1>
    <?php 
    if ($local_lesson_objective != '') echo "<div id='local_lesson_objective'><h2>Objective</h2> $local_lesson_objective </div>";
    if ($local_lesson_objective != '') echo "<div id='local_lesson_steps'><h2>Steps</h2> $local_lesson_steps </div>";
    if ($local_lesson_module != '') echo "<div id='local_lesson_module'><img src='/module-icons/<?php echo $local_lesson_module; ?>.jpg' alt='<?php echo $local_lesson_module; ?>' /></div>";
    if ($local_lesson_video_id  != '') { // If we have a video, show it full width ?> 
    	<div class="no_overlay" id="player2" style="text-align:center;">
    	<iframe title="YouTube video" width="960" height="540" src="http://www.youtube.com/embed/<?php echo $local_lesson_video_id;?>?hd=1" frameborder="0" allowfullscreen></iframe>
    	</div><?php 
    } 
    get_footer(); ?>
    Let's say I want the lesson objective to be hidden from non-active users. Can I change that block of code to something similar to the following?

    Code:
    if ($local_lesson_objective != '') echo "[amember_protect user_action='error' user_error='amember_error_default_user' visitor_action='error' visitor_error='amember_error_default_user']<div id='local_lesson_objective'><h2>Objective</h2> $local_lesson_objective </div>[/amember_protect]";
    
    I know this exact code won't work as it doesn't parse the shortcodes. Can you give me some sample code (or links to sample code) for this type of usage?
  4. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    This is possible another way.
    aMember store levels info in wordpress user's meta, so you can do this:
    PHP:
    if ($local_lesson_objective != '' && in_array('level_name'$current_user->_amember_levels)){ echo "something"
    So code will be displayed only if user have level_name
  5. amcdermott

    amcdermott aMember Pro Customer

    Joined:
    Feb 9, 2011
    Messages:
    26
    Super, thanks alexander. I'm going to use that to wrap several blocks as follows:
    Code:
    if (in_array('active_user', $current_user->_amember_levels)) { 
    	// This is a paid up member; show protected content here
    } else {
    	// This is NOT a paid up member; show guest content here
    }
    
  6. distressedpro

    distressedpro New Member

    Joined:
    Aug 24, 2009
    Messages:
    27
    I'm trying to do the same thing I have a custom post type with a lot of custom fields. I'm replacing the loop like this

    function lender_loop(){
    if (in_array('lender_directory', $current_user->_amember_levels)){
    //my custom loop
    } else {
    echo "is this working?";
    }
    }

    And I am logged in, I have the permissions on my user and I'm seeing "is this working?" instead of my loop. What am I going wrong please?
  7. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Try to define global $current_user; at the top of your function.
    Also print_r($current_user) to check amember levels.

Share This Page