John Davidson

php - how can I set content-type text/xml for xml RSS feed?

0 comments
Message:


can I set content type text/XML of creating a file in WordPress without changing htacess file?


header('Content-Type: text/xml;charset='.get_option('blog_charset'), true);
$my_file =fopen("rss_feed.xml", "w");
$file_contents .= '<?xml version="1.0"?><rss version="2.0">';
$file_contents .='<channel>';
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while ($post_query-> have_posts() ) :
$post_query->the_post();
$file_contents .= '<item><title>' . get_the_title_rss().'</title>' ;
$file_contents .='</item>';
endwhile;
}
$file_contents .='</channel></rss>';
fwrite($my_file, $file_contents);
fclose($path);


My Answer:
Yes, you can set the content type to text/xml for an XML RSS feed without changing the .htaccess file in WordPress. You can use the header() function to set the content type before creating the XML file. Here is an example of how you can do this:

php
header('Content-Type: text/xml;charset='.get_option('blog_charset'), true);
$my_file = fopen("rss_feed.xml", "w");
$file_contents = '';
$file_contents .= '';
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while ($post_query->have_posts() ) :
$post_query->the_post();
$file_contents .= '' . get_the_title_rss().'' ;
$file_contents .= '
';
endwhile;
}
$file_contents .= '
';
fwrite($my_file, $file_contents);
fclose($my_file);


In this code snippet, the header() function is used to set the content type to text/xml with the charset specified by the blog's charset option. Then, the XML content is generated and written to the rss_feed.xml file. Finally, the file is closed using fclose().

Rate this post

4 of 5 based on 8228 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us