Skip to content

Instantly share code, notes, and snippets.

@audrasjb
Last active February 2, 2022 12:44
Show Gist options
  • Save audrasjb/ef2aaa79c8be2465fbf129e067263ece to your computer and use it in GitHub Desktop.
Save audrasjb/ef2aaa79c8be2465fbf129e067263ece to your computer and use it in GitHub Desktop.
ADEME – Génération d'un flux RSS personnalisé sur WordPress
<?php
function ademe_test_feed_init() {
/*
* Génère un flux personnalisé et détermine une fonction d'affichage.
*
* Le premier paramètre indique l'url du flux généré.
* Le second paramètre détermine la fonction d'affichage associée.
*/
add_feed( 'ademe_press_feed_taxo_theme', 'ademe_press_feed_taxo_theme_response' );
}
add_action( 'init', 'ademe_test_feed_init' );
function ademe_press_feed_taxo_theme_response() {
header( 'Content-Type: ' . feed_content_type( 'rss' ) . '; charset=' . get_option( 'blog_charset' ), true );
status_header( 200 );
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:ademe="http://ademe.fr/"
>
<channel>
<?php /* Nom du site + description rapide de la requête */ ?>
<title><?php bloginfo( 'name' ); ?> – Theme "Test"</title>
<?php /* Description détaillée de la requête et de ses spécificités */ ?>
<description>Remontée des 20 derniers articles du terme "Test" de la taxonomie 'taxo_theme".</description>
<?php $date = new DateTime( get_the_time( 'c' ) ); ?>
<lastBuildDate><?php echo $date->format( DATE_RSS ); ?></lastBuildDate>
<language>fr-FR</language>
<?php /* Remplacer ademe_press_feed_taxo_theme par le endpoint du flux */ ?>
<link><?php bloginfo( 'url' ); ?>/ademe_press_feed_taxo_theme</link>
<?php /* Remplacer ademe_press_feed_taxo_theme par le endpoint du flux */ ?>
<atom:link href="<?php bloginfo( 'url' ); ?>/ademe_press_feed_taxo_theme" type="application/rss+xml" />
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 20,
'post_status' => array( 'publish' ),
'tax_query' => array(
array(
'taxonomy' => 'taxo_theme',
'field' => 'slug',
'terms' => 'test',
),
),
);
$query_feed = new WP_Query( $args );
if ( $query_feed->have_posts() ) : while ( $query_feed->have_posts() ) : $query_feed->the_post();
?>
<item>
<guid><?php the_permalink(); ?></guid>
<title><?php the_title(); ?></title>
<link><?php the_permalink(); ?></link>
<?php $date = new DateTime( get_the_time( 'c' ) ); ?>
<pubDate><?php echo $date->format( DATE_RSS ); ?></pubDate>
<description><![CDATA[<?php the_excerpt_rss(); ?>]]></description>
<content:encoded><![CDATA[<?php the_content_feed( 'rss2' ); ?>]]></content:encoded>
<?php /* Sera vide si pas d'image. */ ?>
<ademe:thumbnail><?php echo get_the_post_thumbnail_url( get_the_ID(), 'full' ); ?></ademe:thumbnail>
<?php
/*
* Si besoin d'ajouter des metadonnées (c'est par exemple le cas pour le flux agenda), le faire de cette façon.
* Si la métadonnée renvoie un tableau PHP ou un objet, le sérialiser. Penser au namespace "ademe:".
*/
?>
<?php if ( get_post_meta( get_the_ID(), 'slug_meta', true ) ) : ?>
<ademe:slug_meta><?php echo get_post_meta( get_the_ID(), 'slug_meta', true ); ?></ademe:slug_meta>
<?php endif; ?>
<?php /* Exemple avec date de début et date de fin pour agenda. */ ?>
<?php if ( get_post_meta( get_the_ID(), 'date_debut', true ) ) : ?>
<ademe:date_debut><?php echo get_post_meta( get_the_ID(), 'date_debut', true ); ?></ademe:date_debut>
<?php endif; ?>
<?php if ( get_post_meta( get_the_ID(), 'date_fin', true ) ) : ?>
<ademe:date_fin><?php echo get_post_meta( get_the_ID(), 'date_fin', true ); ?></ademe:date_fin>
<?php endif; ?>
</item>
<?php endwhile; endif; wp_reset_postdata(); ?>
</channel>
</rss>
<?php
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment