Skip to content

Keeping My WordPress Blogroll in Sync With More Than One OPML Endpoint

I’m often the first to want to somehow modify the behavior of my own WordPress plugins. I also, however, prefer small plugins that do one thing well instead of bloated plugins that are pure hell to maintain.

The solution? Hooks! The right hook lets you modify everything without breaking anything.

In the case of Sync OPML to Blogroll, I added 'sync_opml_blogroll_feeds', making the array of feeds that should be imported or synced filterable.

Here’s one way I’ve used said filter to allow for an additional OPML endpoint (this code could go in a must-use plugin):

add_filter( 'sync_opml_blogroll_feeds', function( $feeds ) {
  if ( ! class_exists( '\Sync_OPML_Blogroll\OPML_Parser' ) ) {
    // This (obviously) requires the plugin to be installed. Also, check the path is correct!
    require dirname( __FILE__ ) . '/../plugins/sync-opml-to-blogroll/includes/class-opml-parser.php';

  // Fetch the extra OPML file.
  $response = wp_remote_get( esc_url_raw( '' ) ); // Not my main reader's OPML endpoint.

  if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
    // Something went wrong.

  // Parse OPML.
  $parser      = new \Sync_OPML_Blogroll\OPML_Parser();
  $extra_feeds = $parser->parse( $response['body'], $categories_enabled );

  // Merge with default feed array.
  $feeds = array_merge( $feeds, $extra_feeds );

  // Remove duplicates. We're using site URLs here.
  $link_urls = array_unique( array_column( $feeds, 'url' ) );
  $feeds     = array_intersect_key( $feeds, $link_urls );

  return $feeds;
} );

Note: I know these posts (with lots of code and little context) aren’t for everyone, and I’m sorry for that. Just need to get things out of my system.