After some more testing, we have discovered that attached files are redirecting correctly, but unattached files are going to the attachment page. Is there any way to resolve this so that unattached and attached files all redirect to the media attachment?
Update: Had to rollback 2 versions in order for the attachment redirect to start working again. This is obviously not ideal.. please advise on how we can get unattached (orphan) attachments to continue redirecting to the guid.. we have thousands of such docs.
Hello @vireshm,
Thank you for contacting Rank Math support, and sorry for any inconvenience that might have been caused due to that.
To check if the issue is with your rank-math.php file, please try adding the filter using a different method:
https://rankmath.com/kb/wordpress-hooks-actions-filters/#code-snippets-plugin
If the filter works by using the plugin, then you may need to check again the rank-math.php file in your child theme directory.
The file permission should be the same as all your WordPress files (644).
Let us know how it goes.
Hi. Please see the additional context I supplied above. We had to rollback 2 versions as the latest versions of the plugin do not use the redirect for unattached files. If the file is attached, the redirect works. We need it to work for unattached files. The inserting of the script and permissions etc is not the issue (we added the script to a rank-math.php file in the theme folder)
Hello @vireshm,
We had to make some changes in the plugin code to prevent the option to redirect orphan attachments to show a blank page, so now we verify if there’s a parent post associated with the attachment before making the redirection to prevent this issue.
The downside is that the custom filter we had before to redirect the attachments to the actual files will not work for unattached pages.
We’ll talk with our writers to update the KB article in question to include this new information.
Thank you.
Hi.
That is unfortunate. We have over 15000 documents and images in our library so we make use of a media library plugin to create a physical folder structure within the uploads folder. This means that most of our documents are uploaded directly into these folders and are unattached.
Seems the only recourse (if we continue using rankmath) is to switch to using direct links rather than permalinks throughout the site. Not ideal.
Thanks
Hello @vireshm,
Please try running your redirect code directly in the wp hook and see if that works for you:
add_action( 'wp', function () { if ( ! is_attachment() ) { return; } // Code to redirect attachment. }, 10 );
Let us know how that goes. Looking forward to helping you.