简体   繁体   中英

How to create a facebook feed for wordpress?

I'm new to worpdress and completed a basic development in wordpress course, our final project it's to bring a facebook page data like states and pics, to be displayed in a word press site, to be specific to be listed in a page, I've been researching using facebook developers and found out, tha when querying this url, https://www.facebook.com/feeds/page.php?id=[pageID]&format=json y got the JSON with all de data, also I've tested in http://jsonviewer.net/ and looks good, no I'm stuck in how to make that JSON to be displayed on a page at my site.

Please need some help with this,

You can use a Shortcode for that [fb-page id="ID-NUM"] , use the function wp_remote_get() to pull the feed and then convert the returned JSON into array using PHP's json_decode() .

add_shortcode( 'fb-page', 'shortcode_so_25919996' );

function shortcode_so_25919996( $atts )
{
    if( empty( $atts['id'] ) )
        return 'Please, provide an ID';

    # Request URL content.
    $url = 'https://www.facebook.com/feeds/page.php?id=' . $atts['id'] . '&format=json';
    $response = wp_remote_get( $url );

    if ( is_wp_error( $response ) )
        return 'Error fetching the feed.';

    # Response OK. Decode the response body.            
    $json_to_array = json_decode( wp_remote_retrieve_body( $response ), true );

    # Print the array as code block. Use a loop to build the output as HTML string.
    return '<pre><code>' . print_r( $json_to_array, true ) . '</code></pre>';
}

Or you can call this same function as:

<?php echo shortcode_so_25919996( array( 'id' => 'ID-NUM' ) ); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM