Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get custom data from wordpress RSS with SimplePie

I have made some changes to a RSS feed in my Wordpress, and I'm using fetch_feed() to show data to another website. Imagine there are 2 websites called #Wordpress1 and #Wordpress2. This is the code i've added to #wordpress1's functions.php file

add_action('rss2_item', 'dw_add_data_to_rss');
function dw_add_data_to_rss(){
    global $post;

    if( $post->post_type == 'product' ) {
        $product = new WC_Product( $post->ID );

        $output = '';
        $thumbnail_ID = get_post_thumbnail_id( $post->ID );
        $thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'thumbnail');
        $output  = '<post-thumbnail>';
        $output .= '<url>'. $thumbnail[0] .'</url>';
        $output .= '<width>'. $thumbnail[1] .'</width>';
        $output .= '<height>'. $thumbnail[2] .'</height>';
        $output .= '</post-thumbnail>';

        $output .= '<price>' . number_format( $product->get_price() ) . ' ' . get_woocommerce_currency_symbol() . '</price>';

        echo $output;
    }
}

this code adds product price and thumbnail to Rss feed now we need to display these data on #Wordpress2 , but i don't know how to do it

$rss = fetch_feed( 'http://localhost/wp/feed/?post_type=product' );
if ( ! is_wp_error( $rss ) ) {
    $maxitems  = $rss->get_item_quantity( 10 ); 
    $rss_items = $rss->get_items( 0, $maxitems );
}

foreach ( $rss_items as $item ) {
    echo '<a href="'. $item->get_permalink() .'"><img src="{MY_IMAGE_FROM_RSS}"> <span class="price">{MY_PRICE_FROM_RSS}</span></a>';
}

what should i use instead of MY_IMAGE_FROM_RSS and MY_PRICE_FROM_RSS in above code

like image 957
Amin Avatar asked Nov 01 '22 04:11

Amin


1 Answers

You should use the get_item_tags() function and use blank for the required namespace.

For MY_IMAGE_FROM_RSS use $item->get_item_tags('','post-thumbnail')[0]['child']['']['url'][0]['data'] and for MY_PRICE_FROM_RSS use $item->get_item_tags('','price')[0]['data']

like image 90
Bass Jobsen Avatar answered Nov 14 '22 10:11

Bass Jobsen