Skip Navigation

[Resolved] Showing lock-icon in front of listed post title for restricted users

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Shane 5 months, 1 week ago.

Assigned support staff: Shane.

Author
Posts
#1576963

Tell us what you are trying to do?

I have restricted content for registered members. The restriction of the content and levels happen using Ultimate Membership Pro.

I'm trying to show a little lock-icon (see screen) before title of restricted content for non registered users. I'm using this code below to achieve this.

<?php
function pmsc_add_icon_next_to_restricted_post($post_title, $post_id) {
if (is_admin()) {
return '';
}
if (pms_is_post_restricted($post_id)) {

return '<i class="icon-lock" aria-hidden="true">' . ' ' . $post_title;

}
return $post_title;
}

add_filter('the_title', 'pmsc_add_icon_next_to_restricted_post', 10, 2);

?>

I see the lock appears in the archive pages listings (see archive.png) but I don't see it appear on the front page listings (see front.png)

Is there special html code that has to be added in toolset so that the title can append an icon?
Any help or clue from your side would be highly appreciated

Best regards,

#1577659

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Suzan,

Thank you for getting in touch.

We can actually get this to work in views if we wrap it as a custom shortcode. Add the following to your custom code in Toolset -> Settings -> Custom Code and ensure that it is enabled


function wp_cust_icon( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
		),
		$atts
	);

	if (is_admin()) {
	return '';
	}
	if (pms_is_post_restricted($atts['id'])) {
	
	return '<i class="icon-lock" aria-hidden="true">';
	
	}

}
add_shortcode( 'wp_cust_icon', 'wp_cust_icon' );

Once you have done this you should be able to use the shortcode [wp_cust_icon] which will generate the lock icon beside your post titles if they are restricted.

So how you will implement this in your view is

[wp_cust_icon][wpv-post-title]

Please let me know if this helps.
Thanks,
Shane

#1579821

Hi Shane!

Thanks a lot for your help

So I will tell you step by step what I did and the result.

1- I revised and implemented the following code using the standard wordpress function <is_user_logged_in> . The code tested without error.

https://developer.wordpress.org/reference/functions/is_user_logged_in/

<?php

function wp_cust_icon( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);

if (is_admin()) {
return '';
}

if (! is_user_logged_in($atts['id'])) {

$title = "<i class='icon-lock' aria-hidden='true'> {$post_title}";
}

return $title;

}
add_shortcode( 'wp_cust_icon', 'wp_cust_icon' );

?>

2- I added the shortcode before my title in one view module to test the results
3- I got the lock icon now in front of all titles instead of only the restricted ones. I expected the icon to appear only before the restricted content for unregistered visitors and not before the free ones too.

This is a link to one restricted content

hidden link

I look forward to hearing from you.

#1579931

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Suzan,

My apologies, I forgot to mention that the shortcode also takes the current post's ID as a parameter. To make this work in the view you will need to format the shortcode like this

[wp_cust_icon id="[wpv-post-id]"][wpv-post-title]

Where you are passing the id into the shortcode to determine if the lock icon should be there or not.

Please let me know if this new format helps.
Thanks,
Shane

#1580141

Hi Shane

I implemented shortcode as you instructed but I still see the lock icon appearing before all titles, restricted content and not restricted content.

I'm using to pass the post to this wordpress function: is_user_logged_in

If the user is not logged in (! is_user_logged_in), return the title with a lock icon in the view where I use your shortcode. Is this what you mean?

You can see on the front page

hidden link

#1580153

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Suzan,

As you mentioned you modified the code by checking if the user is logged in and you are displaying the lock if the user is not logged in.

This is in contradiction to your initial report where the code you provided was to lock the post if they don't have permissions to access that post.

I've written the code as a shortcode for that purpose.

function wp_cust_icon( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'id' => '',
        ),
        $atts
    );
 
    if (is_admin()) {
    return '';
    }
    if (pms_is_post_restricted($atts['id'])) {
     
    return '<i class="icon-lock" aria-hidden="true">';
     
    }
 
}
add_shortcode( 'wp_cust_icon', 'wp_cust_icon' );

Which would be the one above that uses the pms_is_post_restricted function. Also the is_user_logged_in() function doesn't take a parameter and would display the lock beside every post when the user is not logged in. Also the log won't display if the user is logged in.

So for the function to put the lock on the post if the user is NOT logged it. It would be.


<?php

function wp_cust_icon( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);

if (is_admin()) {
return '';
}

if (!is_user_logged_in()) {

$title = "<i class='icon-lock' aria-hidden='true'>";
}

return $title;

}
add_shortcode( 'wp_cust_icon', 'wp_cust_icon' );

?>

Thanks,
Shane

#1580307

Hi Shane!

I'm sorry about this confusion

So indeed this is the code I'm using now in the snippet section and I use the shortcode as you instructed but still not satisfactory outcome. Because as I mentioned, with this solution the lock-icon appears before all post titles; restricted and not restricted.

CODE:
---------

<?php

function wp_cust_icon( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);

if (is_admin()) {
return '';
}

if (!is_user_logged_in()) {

$title = "<i class='icon-lock' aria-hidden='true'>";
}

return $title;

}
add_shortcode( 'wp_cust_icon', 'wp_cust_icon' );

?>

SHORTCODE
-----------------

[wp_cust_icon id="[wpv-post-id]"][wpv-post-title]

Is there anything more I could try?

#1580781

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Susan,

This will in fact happen if the user is Logged out.

If they are logged out then the lock will appear because of the logic of the code. For clarity is it that you want the lock to appear when the user is logged out and if that post is restricted? This means that the lock will only appear on these posts ?

Please let me know.
Thanks,
Shane

#1580797

Hi Shane!

Thank you for your patience!

Yes that's exactly what I want. Only show the lock before restricted post titles when the user is not logged in.

Best regards,

#1581079

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Suzan,

Try this below

<?php
 
function wp_cust_icon( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);
 
if (is_admin()) {
return '';
}
 
if (!is_user_logged_in() && pms_is_post_restricted($atts['id']) ) {
 
$title = "<i class='icon-lock' aria-hidden='true'>";
}
 
return $title;
 
}
add_shortcode( 'wp_cust_icon', 'wp_cust_icon' );
 
?>

Use it like this now [wp_cust_icon id="[wpv-post-id]"][wpv-post-title]

If this doesn't work please let me know as well as provide me with admin access so that I can debug the issue further. Please provide a link to the page to test this as well.

Thanks,
Shane