Attachment links not working
-
Hey there – love the plugin!
We’re having difficulty with attachments, the anchor of the link shows correctly (e.g. XXX.pdf) but clicking the link just redirects to the page, rather than being able to view or download the attached file. It looks like the link for the attachments are just the same as the doc link they are attached to.
Can you please advise?
Thank you!
-
Hi-
We do some redirects to protect private content (on the web generally, if someone has the address for an image, they can view it). For instance, if I have a BP Doc with the URL
http://bptest.local/docs/isnt-it-a-lovely-day/, an attachment might have the URLhttp://bptest.local/docs/isnt-it-a-lovely-day/?bp-attachment=JD_anchor_1.jpg.Lots of browsers don’t show query arguments (the
?bp-attachment=JD_anchor_1.jpgpart of the URL) to the user anymore in the location bar or even when hovering over a link, but you should be able to inspect the link.Please post here a couple of examples you find while browsing your site.
-David
-
This reply was modified 8 years, 3 months ago by
David Cavins.
Hi, thanks for getting back to me.
I have inspected the link and these are a few examples:
<a href="https://www.squarepeg.online/docs/info-about-the-sheffields-working-programme/" title="Sheffields-Working-Leaflet.pdf">Sheffields-Working-Leaflet.pdf</a><a href="https://www.squarepeg.online/docs/employer-champion-leaflet/" title="Employer-Champion-Leaflet.pdf">Employer-Champion-Leaflet.pdf</a>As you can see, the links are just directing back to the docs page of the particular member’s doc. Any ideas?
Thanks
Hi there,
Please can you provide any guidance on this one??
Thanks
Hello. Well, we’ll need to debug
bp_docs_get_attachment_url(). So, if you can make apage-test.phpfile in your theme or similar, and add a<?php ?>block to it, that would be a good start.I’d test these items: (You’ll need the ID for an attachment. You can find this by finding Employer-Champion-Leaflet.pdf in your media library and seeing what its ID is.)
var_dump( 'computed url', bp_docs_get_attachment_url( $attachment_id ) ); // Then, within that function, check a few pieces separately. var_dump( 'protection active', bp_docs_attachment_protection() ); var_dump( '$att_base', wp_basename( get_attached_file( $attachment_id ) ) );Hey David thanks for your reply. I’ve created a page-test.php in my theme folder and added the php tags. The next bit you lose me a little. How do I test the items? If I had an ID of 163 for example, how to I run the debug (do I visit a specific URL or amend the code)?
Thanks again
OK, if you’ve added a
page-test.phpfile to your theme, then WP will use it when you visit a page called “Test”. (See https://developer.ww.wp.xz.cn/themes/basics/template-hierarchy/#single-page)So create a page called test and visit it to execute the code in your
page-test.phptemplate part.With an attachment ID of 163, you could do this in page-test.php:
<?php $attachment_id = 163; var_dump( 'computed url', bp_docs_get_attachment_url( $attachment_id ) ); // Then, within that function, check a few pieces separately. var_dump( 'protection active', bp_docs_attachment_protection() ); var_dump( '$att_base', wp_basename( get_attached_file( $attachment_id ) ) ); ?>Hi David, thanks for that. Learning a lot!
This is what was returned after I ran the test on my local copy:
string(12) "computed url" string(52) "http://localhost:8888/squareroot/test/?bp-attachment" string(17) "protection active" bool(true) string(9) "$att_base" string(0) ""Any ideas?
Huh. When I use a known BP Docs attachment ID with that code, the output looks like this:
string 'computed url' (length=12) string 'http://bptest.local/docs/the-first-of-many/?bp-attachment=vacuum-press_1776.jpg' (length=79) string 'protection active' (length=17) boolean true string '$att_base' (length=9) string 'vacuum-press_1776.jpg' (length=21)Can you double check that 163 is the ID of the attachment and not of the parent doc? If you go to your media library at /wp-admin/upload.php and select the attachment, you’ll see the ID in the URL like /wp-admin/upload.php?item=56.
Think I finally got there?
string(12) "computed url" string(104) "https://www.squarepeg.online/docs/employer-champion-leaflet/?bp-attachment=Employer-Champion-Leaflet.pdf" string(17) "protection active" bool(true) string(9) "$att_base" string(29) "Employer-Champion-Leaflet.pdf"I am hoping we can find a solution with this π
That looks good to me! I even followed the link and the pdf downloaded fine. What did you change? Just the attachment ID, or something else?
Ok, so we know that the url is being calculated correctly, given the right attachment ID. So, if we can see that the right attachment ID is being passed to the function, then we’ll know that something else (I’m guessing the theme) is shaving off url parameter arguments.
In /wp-content/plugins/buddypress-docs/includes/templates/docs/single/attachments.php
, add this line:
<?php var_dump( 'attachment ID', $attachment->ID ); ?>
right after this statement.
<?php foreach ( bp_docs_get_doc_attachments() as $attachment ) : ?>If that ID (it’ll be printed out on the page when you visit https://www.squarepeg.online/docs/employer-champion-leaflet/ on your test machine) matches the ID you plugged into your code on page-test.php, then we’ll know that the problem is elsewhere.
Hi David, I just did the test again but on the live site rather than the test site. The ID was probably incorrect in the original test as well.
I’ve followed your instructions and the ID displaying on that page is the same that I placed in the code originally so it must be a theme issue. We are using BuddyBoss theme, what should I do next?
Thank you very much
Liam
Hi Liam-
I’d follow these steps for troubleshooting code interaction:
β’ Switch to a “default” theme like TwentySeventeen. If the problem is fixed, then contact the BuddyBoss authors, who are helpful people.
β’ Start disabling plugins to see if one of them is affecting this url.If you get down to BuddyPress and BP Docs using TwentySeventeen, then the problem is in BP Docs itself.
Hey David, thanks for your reply. I did as you advised and it turns out it’s a conflict with W3 Total Cache. Hoping I can change the settings about to resolve – have you experienced this before? Hope you can provide some insight!
Thanks again, Im really grateful
Remove query strings for static resources was the culprit if anyone else experiences this issue!
Congratulations on finding the root of the issue! I don’t use W3TC myself, I find WP Super Cache easier to set up and similarly effective. I also exclude the docs directory from the cache by path. You might look for something similar in the TC settings. They also may be a setting for allowing url parameter args. It seems like the plug-in would have to allow for meaningful args.
You’re almost there!
-
This reply was modified 8 years, 3 months ago by
The topic ‘Attachment links not working’ is closed to new replies.