Introduction
Running a modern online store requires familiarity with various technologies that allow you to integrate your business with different platforms. One such technology is creating a Yandex Market YML feed, which enables you to synchronize your website’s catalog with Yandex services, such as maps and others.
While there are ready-made plugins available in WordPress for this purpose, there are situations where more flexible feed customization is needed. In such cases, creating a feed manually becomes necessary. Fortunately, this process is quite straightforward, and in this article, I’ll guide you through the steps.
Creating the Feed
The first step is to create the feed. This can easily be done using the add_feed() function. Simply open the functions.php file of your theme and insert the following code:
// Provides Yandex YML feed. add_action( 'init', function () { add_feed( 'products-yml', function () { header( 'Content-Type: application/xml; charset=UTF-8' ); get_template_part( 'parts/feed/products', 'yml' ); }); });
This code registers a new feed in WordPress, which will be accessible at site.com/feed/products-yml. In the callback function, we specify that the content for this page will be loaded from the file located at wp-content/themes/your-theme/parts/feed/products-yml.php
Generating the Feed
Next, you need to create the products-yml.php file in the directory specified above and insert the following code:
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <yml_catalog date="<?php echo wp_date( 'c' ); ?>"> <shop> <!-- Shop --> <name><?php bloginfo( 'sitename' ); ?></name> <company><?php bloginfo( 'sitename' ); ?></company> <url><?php echo home_url(); ?></url> <platform>WordPress</platform> <!-- Categories --> <categories> <?php foreach ( get_terms([ 'taxonomy' => 'product_cat' ]) as $category ) : ?> <?php printf( '<category id="%d"%s>%s</category>', esc_attr( $category->term_id ), $category->parent ? " parentId=\"{$category->parent}\"" : '', esc_html( $category->name ), ); ?> <?php endforeach; ?> </categories> <!-- Offers --> <offers> <?php $product_query = new WP_Query([ 'post_type' => 'product', 'posts_per_page' => -1, ]); ?> <?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?> <?php $product = wc_get_product(); ?> <!-- <?php the_title(); ?> --> <offer id="<?php echo esc_attr( $product->get_sku() ); ?>"> <name><?php the_title(); ?></name> <description><![CDATA[<?php the_content(); ?>]]></description> <url><?php the_permalink(); ?></url> <?php if ( has_post_thumbnail() ) : ?> <picture><?php the_post_thumbnail_url( 'full' ); ?></picture> <?php endif; ?> <?php foreach ( $product->get_gallery_image_ids() as $image_id ) : ?> <picture><?= wp_get_attachment_image_url( $image_id, 'full' ); ?></picture> <?php endforeach; ?> <price><?php echo $product->get_regular_price(); ?></price> <?php if ( $product->get_sale_price() ) : ?> <oldprice><?php echo $product->get_sale_price(); ?></oldprice> <?php endif; ?> <currencyId><?php echo get_woocommerce_currency(); ?></currencyId> <?php $categories = wp_get_post_terms( get_the_ID(), 'product_cat', [ 'fields' => 'ids' ] ); foreach ( $categories as $category_id ) { printf( '<categoryId>%d</categoryId>', esc_html( $category_id ), ); } $brands = wp_get_post_terms( get_the_ID(), 'product_brand', [ 'fields' => 'names' ] ); foreach ( $brands as $brand ) { printf( '<vendor>%s</vendor>', esc_html( $brand ), ); } ?> </offer> <?php endwhile; ?> <?php wp_reset_postdata(); ?> </offers> </shop> </yml_catalog>
This code serves as a template and assumes you are using WooCommerce. Creating such a feed requires some knowledge of WordPress programming and PHP.
Accessing and Configuring the Feed
Once the feed is set up, you can proceed to Yandex Business or any other required service. In the “Products” section, configure the feed import by specifying its URL: https://site.com/feed/products-yml/
If accessing the feed URL results in a 404 error, you’ll need to refresh WordPress’s rewrite rules. The easiest way to do this is by going to Settings → Permalinks in your WordPress dashboard and simply clicking Save.
Alternatively, you can temporarily add the following line to your hook to force a rewrite flush:
add_action( 'init', function () { ... flush_rewrite_rules(); });
After visiting any page on your site to trigger the update, make sure to remove that line. Once done, the feed should load correctly.
Useful Resources
The feed template provided above does not include all possible fields. If you wish to customize the feed in more detail, you can refer to Yandex’s official documentation on YML feed configuration: