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

Wrong og:url meta tag value #21430

Open
2 tasks done
smazur opened this issue Jun 6, 2024 · 5 comments
Open
2 tasks done

Wrong og:url meta tag value #21430

smazur opened this issue Jun 6, 2024 · 5 comments

Comments

@smazur
Copy link

smazur commented Jun 6, 2024

  • I've read and understood the contribution guidelines.
  • I've searched for any related issues and avoided creating a duplicate issue.

Please give us a description of what happened

Hi! We have a WooCommerce store and use custom code to dynamically create products as follows:

$product = new WC_Product_Simple();
// Here is product category and attributes assigned.
$product->save();

Also we have rewrite rules setup that uses category slug and few other attributes in product url. When a new product is created i noticed that og:url meta tag has wrong product url.

That's because \Yoast\WP\SEO\Integrations\Watchers\Indexable_Post_Watcher::build_indexable is hooked directly to wp_insert_post so the index record is created immediately after product post created and before any taxonomy attributes assigned to it. So it makes get_permalink return wrong url for the product.

I think this is a bug and \Yoast\WP\SEO\Integrations\Watchers\Indexable_Post_Watcher::build_indexable call should be defferred like you do in \Yoast\WP\SEO\Integrations\Watchers\Indexable_Post_Meta_Watcher::update_indexables

As a quick fix i used following snippet.

// Rebuild yoast index on shutdown
function __my_yoast_rebuild_post_index_on_shutdown( $post_id ) {
    static $post_ids = null;

    if( null === $post_ids ) {
        $post_ids = [];

        register_shutdown_function( static function() use( &$post_ids ) {
            /** @var Yoast\WP\SEO\Integrations\Watchers\Indexable_Post_Watcher $indexable_post_watcher */
            $indexable_post_watcher = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Watchers\Indexable_Post_Watcher::class );

            foreach( array_keys( $post_ids ) as $post_id ) {
                $indexable_post_watcher->build_indexable( (int) $post_id );
            }
        });
    }

    $post_ids[$post_id] = true;
}

function __my_yoast_rebuild_post_index_on_shutdown_init() {
    if( function_exists( 'YoastSEO' ) && YoastSEO()->classes->container->has(Yoast\WP\SEO\Integrations\Watchers\Indexable_Post_Watcher::class ) ) {
        add_action( 'wp_insert_post', '__jt_yoast_rebuild_post_index_on_shutdown' );
    }
}

add_action( 'init', '__my_yoast_rebuild_post_index_on_shutdown_init' );

Looking forward for you reply. Thanks!

@josevarghese
Copy link
Contributor

Hi @smazur

Thanks for using the Yoast SEO plugin.

If you reset the Yoast indexables, will the og:url meta tag be updated after completing the reindex? More info about resetting the indexable at here: https://yoast.com/help/how-to-reset-yoast-indexables/

It seems like the issue you have noticed is due to this one: #15246, which was reported by other users before. However, our development team was not able to replicate it before. So, can you share more details on how to reproduce it with deeper steps on that issue to help us better replicate it and fix it?

@smazur
Copy link
Author

smazur commented Jun 13, 2024

Hi @josevarghese

After reindexing the og:url gets correct value.

Here are the steps you can try to replicate this issue:

  1. Under Settings -> Permalinks -> Product permalinks choose option Shop base with category
  2. Setup Woocommerce product category under Products -> Categories
  3. Try to create new product using following code:
$p = new WC_Product_Simple();
$p->set_name( 'Test product' );
$p->set_status( 'publish' );
$p->set_category_ids([ PUT_YOUR_CATEGORY_TERM_ID_HERE ]);
$p->save();
  1. Examine og:url meta tag value on single product page you just created.

This issue is also replicated when i create new product using following wp cli command:

wp wc product create --name='Cli product test' --type=simple --status=publish --categories='[{"id":PUT_YOUR_CATEGORY_TERM_ID_HERE}]' --user=PUT_ADMIN_USER_ID_HERE

@josevarghese
Copy link
Contributor

Hi @smazur

I tried the WP-CLI command and code but got some errors while checking both on my lab site. When I tried the WP-CLI wp wc product create --name='prdctname' --type=simple --status=publish --categories='[{"id":1013}]' --user=7, I am getting the error "Error: Invalid parameter(s): categories {"status":400,"params":{"categories":"categories[0] is not of type object."},"details":{"categories":{"code":"rest_invalid_type","message":"categories[0] is not of type object.","data":{"param":"categories[0]"}}}}". So can you please share the code and CLI command correctly?

Thanks in advance.

@smazur
Copy link
Author

smazur commented Jun 25, 2024

@josevarghese

On my setup wp cli command works without any error.

Did you try the php code above? You can run it from wp shell line by line.

@amboutwe
Copy link
Member

I reproduced the issue with the following steps. Also confirmed that adding a post through WP CLI does not experience the same issue.

Starting with a vanilla installation (category permalinks):

  1. Install and activate Yoast SEO (free)
  2. Import theme unit test content (check Download and import attachments)
  3. Install, activate, and set up WooCommerce (options below)
  4. Import WooCommerce sample data (steps below)
  5. Run SEO data optimization
  6. Create a post and product with WP CLI (Replace IDs as needed)
    • wp wc product create --name='CLI product test' --type=simple --status=publish --categories='[{"id":17}]' --user=1
    • wp post create --post_type=post --post_title='CLI post test' --post_category=87 --post_status=publish
  7. Check product URL with wp post get 1907 --field=url (Replace ID as needed)
  8. Open URL and view source code in a browser
  9. See wrong og:url
    • <meta property="og:url" content="http://example.com/shop/uncategorized/cli-product-test/" class="yoast-seo-meta-tag" />
  10. Check post URL with wp post get 1908 --field=url (Replace ID as needed)
  11. Open URL and view source code in a browser
  12. See the correct og:url

WordPress settings

Permalink: /%category%/%postname%/
Woo Permalink: /shop/%product_cat%/

WooCommerce options:

Setup path: /wp-admin/admin.php?page=wc-admin&path=%2Fsetup-wizard

Introduction
Uncheck 'I agree to share my data'
Click 'Set up my store'

Step 1: Which one of these best describes you?
Select 'I'm just starting my business'
Click 'Continue'

Step 2: Tell us a bit about your store
Enter store name (optionally leave it the domain)
Select 'Clothing and accessories' for type of products
Select 'United States (US) -- California' for the store location
Click 'Continue'

Step 3: Get a boost with our free features
Click 'Skip this step' (top corner of page)

Import Products
Go to Admin > Products
Click 'Start Import'
Browse to WooCommerce plugin folder '\wp-content\plugins\woocommerce\sample-data'
Select 'sample_products.csv'
Click 'Continue'
Click 'Run the importer'
Wait for success message

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

No branches or pull requests

3 participants