jaredatch / Shared-Counts

WordPress plugin that leverages SharedCount.com API to quickly retrieve, cache, and display various social sharing counts.
GNU General Public License v2.0
47 stars 16 forks source link

Having trouble getting the shared_counts_link_url to work correctly #101

Closed robertdevore closed 4 years ago

robertdevore commented 4 years ago

I'm working on an update to a website that needs to alter the shared link, and found the shared_counts_link_url filter.

https://github.com/jaredatch/Shared-Counts/blob/develop/includes/class-shared-counts-front.php#L502

Below is an example of the function I've created to filter the URL.

https://gist.github.com/robertdevore/1a9ff27cb5b0400c4ddacfb2d2ed506f

When I print_r() the $url instead of returning it, the correct (new) url will display on the front-end of the site.

https://cln.sh/qdDvc8

But for some reason, it's not changing the URL that gets shared via each social network. Any ideas on what could be causing this to happen and a possible solution?

Thanks!

billerickson commented 4 years ago

Could there be any other filters running later (like on the shared_counts_link filter) that might also be modifying the URL?

When I need to modify the shared URL I typically use the shared_counts_link filter. Ex: https://sharedcountsplugin.com/2019/03/27/use-production-url-when-in-development-or-staging/

Your code looks fine though and it should work.

robertdevore commented 4 years ago

@billerickson thanks for the quick response! No other filters running that I'm aware of - I installed the plugin and wrote this filter and there's no other ones in the theme I'm editing.

Thanks for that link, I'll try that out and see if it fixes the issue and report back here. Thanks!

robertdevore commented 4 years ago

I used the example code and it changed the share link, but it removed all of the social aspects of the button links:

original: www.twitter.com/share?url=URLHERE new: www.billerickson.net/

After some tweaking to the code and using the original shared_counts_link_url filter, I was able to get the shared link updated with the new URL:

https://gist.github.com/robertdevore/891114055154d5fcd4157c20a9987c9a

So now when you hover over one of the share buttons, the link parameter has been updated with the new URL

https://cln.sh/3sZrsH

billerickson commented 4 years ago

Correct, the shared_counts_link filter lets you customize the entire URL of the share button, which is why you need to do a str_replace() for just the part of the URL you want to change.