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

Issue-124: Add Support for Open Graph Tags on Posts & Pages (v2.0) #132

Merged
merged 25 commits into from
Mar 5, 2025

Conversation

ellm
Copy link
Member

@ellm ellm commented Feb 20, 2025

Summary

Fixes #124. This pull request introduces a Gutenberg-native solution for managing Open Graph tags on individual posts and pages.

Description of Changes

  • Added a slotfill to the post editor sidebar for configuring Open Graph tags, allowing users to set the title, description, and image.
  • Saved the Open Graph data to postmeta and registered it with WordPress.
  • Included logic to add the Open Graph meta tags to the document head on single posts. Overrides are used when set; otherwise, the tags fall back to WordPress defaults:
    • Title => post_title
    • Description => excerpt
    • Image => featured image
  • Wrote tests to validate that the overrides and fallback defaults function as expected.
  • Currently, the feature is enabled for all post types by default.

Additional Notes

  • This implementation is for the Gutenberg editor only. Support for the classic editor is out of scope for this PR.
  • Future development could include a settings panel for admins to enable/disable Open Graph tags per post type, as suggested in the issue discussion.

Testing Instructions

  1. In the Gutenberg editor, check the sidebar for the Open Graph settings panel.
  2. Configure the Open Graph title, description, and image for a post.
  3. Verify that the meta tags are added to the document head when viewing the post on the frontend.
  4. Ensure that the logic falls back to WordPress defaults when overrides are not provided.
  5. Run automated tests to confirm functionality and regression coverage.

Related Issue

#124

@ellm ellm added enhancement New feature or request gutenberg Requires understanding Gutenberg php Requires understanding PHP labels Feb 20, 2025
@ellm ellm marked this pull request as ready for review February 20, 2025 22:59
Copy link

@mogmarsh mogmarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall, this looks great! I flagged a couple little things. 👍

get_post_types_by_support( 'open-graph' ),
'wp_seo_open_graph_title',
[
'sanitize_callback' => 'wp_kses_post',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably use sanitize_text_field here. I don't think we allow/need any html, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Updated in 14ef3a6

get_post_types_by_support( 'open-graph' ),
'wp_seo_open_graph_description',
[
'sanitize_callback' => 'wp_kses_post',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 14ef3a6

get_post_types_by_support( 'open-graph' ),
'wp_seo_open_graph_image',
[
'sanitize_callback' => 'wp_kses_post',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably use intval since it should always be a number

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 14ef3a6

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually went with absint here instead to ensure positive integer and better fallback to 0. Updated in 14ef3a6

$open_graph_image = get_post_meta( $post_id, 'wp_seo_open_graph_image', true );

if ( ! empty( $open_graph_image ) && is_numeric( $open_graph_image ) ) {
return wp_get_attachment_image_url( (int) $open_graph_image, 'full' );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would probably be safest to get the url, then check if it's set and not a wp_error, then return it. The media could get deleted from the media library after being set in the meta.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I've made that update in 4d80003

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional update in 4d9b484

]
)
->create();
$this->assertEquals( 007, Open_Graph::get_image( $post_id ) );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is failing because get_image returns the url of the image (or false in this case because the image doesn't exist), not the id. There should be a way to load an image into the media library, then get its url. #cc_testing should be able to help.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It took me a bit to sort out this but I think I came to a pretty good solution here: d123964. Post meta source is not identical but I think it works. I also figured out how to run phpunit tests locally in the process (much better than pushing to the remote branch 🤕 )

@ellm ellm merged commit a08ee88 into v2.0 Mar 5, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request gutenberg Requires understanding Gutenberg php Requires understanding PHP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants