0

I have a function I'm using to display a login notification when a user without access tries to view a single post. The function also displays the post excerpt publicly on archive, widgets, etc , but shows the login notification only when the full post is viewed, i.e. is_singular

This basically allows users without access to view a snippet of the post on archive pages, but when they click & try to view the full post they're prompted with a notification to login or register.

The issue I'm having is that when using a related posts widget, the login notification is displayed when it shouldn't be. I'm pretty sure it has to do with how I'm defining the post variable in my function which is causing it to not differentiate between posts displayed as excerpts in the widget vs the full single post. Looking for some insight.

Content Protection function:

/*Show Excerpt for Protected Content*/

//Show mm content even if the user doesn't have access
function customContentProtection($data) 
{ 
error_log('customContentProtection');
return true; 
} 
add_filter('mm_bypass_content_protection', 'customContentProtection');

//then we add the filters for the archive content
add_filter( 'the_content', 'sbm_mm_content_filter' ); 
function sbm_mm_content_filter( $content ) { 
global $post;
global $post_id;

if ( !is_singular()) 
return $content;

$new_content = $content; 

// define what users without access view  
if ( mm_access_decision(array("access"=>"true")) != true ){
//Need to apply a filter to get the excerpt if the template doesn't include it
    $new_content = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id)); 


/* 
//This is where we output the excerpt & login form
    echo '<div>' . $new_content . '</div><br><div>' . do_shortcode('[mm-form-c form="red" url="/member-registration"]') .'</div>';  
*/

//Instead of the excerpt let's output a blurred text image  
    $url = site_url();
    echo 
    '<div class="no-access-wrapper">
        <div class="no-access">
        <img src=" ' . $url . '/wp-content/themes/splash-child/images/blurred-text.png"></div>
    <div class="no-access-top">
    <h3 style="text-align:center;">This content is for members only.  Please login or sign up.</h3>
    ' . do_shortcode('[mm-form-c form="red" url="/member-registration"]') . '
    <br>
    <p style="text-align:center;"> If you are already logged in and are having trouble viewing this content, please <a href="mailto: [email protected]">contact support</a></p>
    </div>
    </div>';
}

else return $new_content; 
}

Here's the section of my single.php file that outputs the related posts widget:

 <?php
                        }
                        $related_post_number = $bdp_settings['related_post_number'];
                        $col_class = '';
                        if ($related_post_number == 2) {
                            $post_perpage = 2;
                        }
                        if ($related_post_number == 3) {
                            $post_perpage = 3;
                        }
                        if ($related_post_number == 4) {
                            $post_perpage = 4;
                        }
                        $related_post_by = $bdp_settings['related_post_by'];
                        $title = $bdp_settings['related_post_title'];
                        if (isset($bdp_settings['display_related_post']) && $bdp_settings['display_related_post'] == 1) {
                            $related_post_content_length = isset($bdp_settings['related_post_content_length']) ? $bdp_settings['related_post_content_length'] : '';
                            $args = array();
                            if ($related_post_by == "category") {
                                global $post;
                                $categories = get_the_category($post->ID);
                                if ($categories) {
                                    $category_ids = array();
                                    foreach ($categories as $individual_category)
                                        $category_ids[] = $individual_category->term_id;
                                    $args = array(
                                        'category__in' => $category_ids,
                                        'post__not_in' => array($post->ID),
                                        'posts_per_page' => $post_perpage // Number of related posts that will be displayed.                            'caller_get_posts' => 1,
                                    );
                                }
                            } elseif ($related_post_by == "tag") {
                                global $post;
                                $tags = wp_get_post_tags($post->ID);
                                if ($tags) {
                                    $tag_ids = array();
                                    foreach ($tags as $individual_tag)
                                        $tag_ids[] = $individual_tag->term_id;
                                    $args = array(
                                        'tag__in' => $tag_ids,
                                        'post__not_in' => array($post->ID),
                                        'posts_per_page' => $post_perpage // Number of related posts to display.
                                    );
                                }
                            }
                            $my_query = new wp_query($args);
                            if ($my_query->have_posts()) {
                                ?>
                                <div class="related_post_wrap">
                                    <?php
                                    do_action('bdp_related_post_detail', $theme, $post_perpage, $related_post_by, $title, $related_post_content_length);
                                    ?>
                                </div>
                                <?php
                            }
                        }
                        // If comments are open or we have at least one comment, load up the comment template.
                        if ($bdp_settings['display_comment'] == 1) {
                            if (comments_open() || get_comments_number()) {
                                comments_template();
                            }
                        }

                    // End of the loop.
                    endwhile;
                    if ($theme == "offer_blog" || $theme == "winter") {
                        echo '</div>';
                    }
                    ?>
                </div>
                <?php do_action('bdp_after_single_page'); ?>
            </main><!-- .site-main -->
        </div><!-- .content-area -->

2 Answers 2

0

I think the solution is to check if it's the main query. WordPress has a function for that, is_main_query()

The query that you're running in your related posts widget is a secondary query, so is_main_query() will return false.

You can take advantage of that, by changing your code like this:

if ( ! is_main_query() || ! is_singular() ) return $content;

0

From the looks of that code, you're using the Blog Designer Pro plugin.

Check admin/functions.php. There's a function that handles the related posts output. They use get_the_content for the $excerpt variable. Just change that to get_the_excerpt and your function should work.

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.