0

i have created a custom rss/xml feed for my wordpress page to list certain jobs - it works, except that the content type is changing automatically and very arbitrary from rss/xml to text/html, which makes it impossible to fetch the feed correctly. I really don't know why or how to reproduce the problem.

Is there something wrong about the code? I don't think so - maybe its du to false configuration of the server or something like that.

Here is the code:

class KIE_XML_Feed{

  public function __construct()
  {
    add_action( 'init', array( $this, 'kie_add_xml_job_feeds' ) );
    add_filter( 'feed_content_type', array( $this, 'kie_feed_type'), 10, 2 );
  }

  function kie_add_xml_job_feeds() {
    add_feed( 'joblift', array( $this, 'kie_render_joblift_job_feed' ) );
  }

  function kie_feed_type( $content_type, $type ) {
    if ( 'joblift' === $type ) {
      return feed_content_type( 'rss' );
    }
    return $content_type;
  }


  /*
  *
  * Joblift
  *
  */

  function kie_render_joblift_job_feed() {
    header( 'Content-Type: application/rss+xml ; charset=' . get_option( 'blog_charset' ), true );
    echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>';

    // Start feed
    ?><feed xmlns="http://www.w3.org/2005/Atom"><?php

    // Query
    $job_query = new WP_Query( [
      'post_type'      => 'jobs',
      'posts_per_page' => -1,
      'meta_query' => array(
        array(
          'key' => 'kie_xml_feed_joblift',
          'value' => '1',
          'compare' => '=',
          'type' => 'numeric'
        )
      )
    ]);

    // Loop
    if ( $job_query->have_posts() ) {
      while ( $job_query->have_posts() ) {
        $job_query->the_post();
        get_template_part( 'partials/feeds/joblift' );
      }
    }

    // End feed
    ?></feed><?php

    // Reset
    wp_reset_postdata();
  }
}

Thank you in advance.

NieCon
  • 23
  • 1
  • 7
  • See [this comment](https://developer.wordpress.org/reference/functions/add_feed/#comment-2086) - you should use either the `feed_content_type` hook or the function `header()`, but not both.. – Sally CJ May 06 '21 at 00:19
  • Thank you, I fixed that, but the problem still is there. – NieCon May 07 '21 at 09:16
  • How did you know that the feed's content type is sometimes HTML? After you applied the fix, did you try clearing your caches? – Sally CJ May 07 '21 at 15:00
  • Yeah i did clear the caches - here is a screen for you https://ibb.co/NsM6XBd – NieCon May 10 '21 at 10:18
  • Well, (as of writing) your feed is [not valid](https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2Fkennt-ihr-einen.de%2Fjoblift), so you'll need to correct that, e.g. use `<![CDATA[` and `]]>` to wrap HTML content. And if you're serving an Atom feed, then the `Content-Type` header should be `application/atom+xml` and not `application/rss+xml`. You can see a valid Atom feed [here](https://kennt-ihr-einen.de/feed/atom/). – Sally CJ May 10 '21 at 17:29
  • 1
    Thanks for the answer, I think I fixed it. Yes, the specified feed is not valid, but that was not the problem. Wordpress apparently delivers the created feeds under a different URL, namely the following one https://kennt-ihr-einen.de/feed/joblift - So, to make it simple - I forgot the /feed/ in the URL – NieCon May 11 '21 at 08:19
  • Okay, but you should ensure the feed content/markup is valid. And I suggest you to post that comment as an answer. – Sally CJ May 11 '21 at 11:15

1 Answers1

1

I think I fixed it. WordPress apparently delivers the created feeds under a different URL as I thought, namely the following one: kennt-ihr-einen.de/feed/joblift. So, to make it simple, I forgot the /feed/ in the URL.

Sally CJ
  • 15,362
  • 2
  • 16
  • 34
NieCon
  • 23
  • 1
  • 7