rankmath / seo-by-rank-math

Rank Math is a revolutionary WordPress SEO Plugin that combines the features of many SEO tools and lets you multiply your traffic in the easiest way possible :bulb: :chart_with_upwards_trend: →
https://rankmath.com
107 stars 50 forks source link

[Feature] Rank Math Compatibility with the multisite-global-media plugin #282

Open codedbyglenden opened 3 months ago

codedbyglenden commented 3 months ago

Is your feature request related to a problem? Please describe.

We have a various clients that make use of the Global Media Library plugin for WordPress, which allows a multi-site instance to use a singular site as a hub for all media assets across multiple sites in a network.

Plugin link: multisite-global-media

The conflict between Rank Math & MGM happens when rank math tries to generate an OG Image tag. When you select an image stored in the Global media library, the image id is saved to the post in a slightly different format, so that MGM can identify between images stored on the current site or those saved to the hub site. The format saved is in the following format:

{hub_site_id}000000{actual_attachment_id_saved_on_the_hub_site}

If we for example set a featured image stored in the hub site on a post, the id reference saved in the post_thumbnail field might be:

12300000012

123 = The Hub site id 12 = The attachment id saved in the network

When rank math tried to generate the OG Image tag the first function it runs into is:

wp_attachment_is_image

This will fail because the id format is non-standard, and technically an attachment with this id doesn't actually exist. And unfortunately this is an function without filters or actions.

Describe the solution you'd like

My proposed solution is to add a do_action before the wp_attachment_is_image check, as well as a filter for the attachment id. This will allow me to add a site switch inside of the do_action (in my theme or plugin) then add a another do_action after the get_variations function call to switch the site back to the current site.

I've attached a screenshot with the proposed changes below. New code is labelled with a "new code" comment. The filter and action names are purely placeholder names to demonstrate the code changes I would like to see.

Google Keep

Describe alternatives you've considered

As we can not filter the code before the image check there is no alternative to get OG image tags generated in Rank Math SEO currently.

pratikrm commented 2 months ago

Hi @codedbyglenden

Thank you for submitting a PR. I checked this on my local setup and was able to reproduce the issue. I believe hooks are needed to switch and reset the blogs. If so, shouldn't we pass $attachment_id to the hook? I have also added a comment in the PR: https://github.com/rankmath/seo-by-rank-math/pull/283/files#r1642583643