Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URLs in og:image meta tag is not encoded #706

Open
kong0107 opened this issue Jan 22, 2025 · 1 comment
Open

URLs in og:image meta tag is not encoded #706

kong0107 opened this issue Jan 22, 2025 · 1 comment

Comments

@kong0107
Copy link

Describe the bug
If user uploads an image with its filename having non-ascii characters,
the output <meta property="og:image"> would have its content un-encoded,
which leads social websites (such as Facebook) ignore the tag.

How to reproduce the bug
Steps to reproduce the behavior:

  1. Have a file with non-ascii character in its name.
  2. Upload the file with WordPress native interface.
  3. Use the URL of the image as a social image of some page/post, or the site.
  4. Use Sharing Debugger - Meta for Developers to check the page/post/site.
  5. Check HTTP server access log, and you won't see logs that Facebook fetch the image.

Expected behavior
Non-ascii characters shall be encoded into RFC 3986 format.

Additional context
I append the following codes to my functions.php in theme directory to work around:

add_action('the_seo_framework_before_meta_output', function() {
	ob_start(function($html) {
		return preg_replace_callback('/ content="(https?:\/\/[^"]+)"/', function($matches) {
			$url = str_replace(
				array('%26', '%2E', '%2F', '%3A', '%3D', '%3F'),
				array('&', '.', '/', ':', '=', '?'),
				rawurlencode($matches[1])
			);
			return " content=\"$url\"";
		}, $html);
	});
});

add_action('the_seo_framework_after_meta_output', function() {
	ob_end_flush();
});
@sybrew
Copy link
Owner

sybrew commented Jan 22, 2025

Thank you for the bug report!

The workaround may invoke CSRF, so please be mindful before implementing it on sites with authors you do not fully trust.

I'm going to test the behavior before the next minor release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants