- This topic has 7 replies, 2 voices, and was last updated 3 years, 5 months ago by Leo.
-
AuthorPosts
-
October 23, 2020 at 8:00 am #1501432Janine
Hiya.
I want to prevent the featured images from being pinned.
I tried to use these two bits of code, but neither worked for me:
/**Disable pinning of all featured images.
*/
add_filter( ‘post_thumbnail_html’, function( $html ){
// Something already has ‘data-pin-nopin’, so we don’t need to modify.
if ( false !== stripos( $html, ‘data-pin-nopin’ ) ) {
return $html;
}
// Add ‘data-pin-nopin’ to the HTML tag.
$html = str_replace( ‘<img ‘, ‘<img data-pin-nopin=“true” ‘, $html );
return $html;
});AND this…
add_filter( ‘wp_get_attachment_image_attributes’, function ( $attr ) {
$attr[‘data-pin-nopin’] = ‘true’;
return $attr;
} );The second function was said to work with GP, but it didn’t for me.
How can I do this with GP?
Thank you.
October 23, 2020 at 8:51 am #1501488LeoStaffCustomer SupportHi there,
The code looks good to me.
Can you link me to the page in question with the code added using one of these methods?
Adding PHP: https://docs.generatepress.com/article/adding-php/Make sure to clear and disable your caching plugin if you are using any.
October 23, 2020 at 9:21 am #1501529JanineHi, Leo.
I created added the codes to Code Snippets. Have two snippets. Which one do you want me to use?
Plus, I am doing this on a staging site. The link is the private information area.
Thank you.
October 23, 2020 at 10:43 am #1501606LeoStaffCustomer SupportTry adding both to start.
The page you linked requires a username and a password to view.
Not sure where you got the code from but GP uses the default featured image option from WordPress, so if the code works in a twenty series WP theme, then it should work in GP.
October 23, 2020 at 11:10 am #1501631JanineHi, Leo.
What I want is visitors to only be able to pin the pins I design for pinning. I want all other images to be no pin.
I have been trying to add data-pin-nopin=”true” individually to my images within the posts and pages. And my success has been inconsistent. It works on the homepage.
BUT it is not working on other pages. When I enter the data-pin-nopin=”true”, it doesn’t stick even after I update. It also changes how the image displays on the backend.
Can you help accomplish my goal?
I enabled the two code snippets in the Code Snippets plugin.
I got the codes from these two websites:
https://tinyurl.com/yxanhqk8
https://tinyurl.com/y6trwnorI have cleared and disabled caching plugins.
October 23, 2020 at 5:01 pm #1501861LeoStaffCustomer SupportThis code here should definitely work for the featured images in single posts:
/** * Disable pinning of all featured images. */ add_filter( 'post_thumbnail_html', function( $html ){ // Something already has 'data-pin-nopin', so we don't need to modify. if ( false !== stripos( $html, 'data-pin-nopin' ) ) { return $html; } // Add 'data-pin-nopin' to the <img> HTML tag. $html = str_replace( '<img ', '<img data-pin-nopin="true" ', $html ); return $html; });
I just tested it using a fresh install and you can see it here:
https://www.screencast.com/t/xgAvw1gUdyAnd this code here works for the featured images on the blog/archive pages:
add_filter( 'wp_get_attachment_image_attributes', function ( $attr ) { $attr['data-pin-nopin'] = 'true'; return $attr; } );
I also tested this and you can see it here:
https://www.screencast.com/t/iyMWMxeoI did notice that when I copied the code from the website, the quotations got a little funky so make sure you fix those or copy my code here.
October 24, 2020 at 1:33 am #1502066JanineHi, Leo.
Thank you! Your codes seem to be working.
BE SAFE.
October 24, 2020 at 10:16 am #1502633LeoStaffCustomer SupportNo problem 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.