Categorias:

Como pegar imagens recentes de um perfil no Pinterest, com o WordPress

Com esse código conseguimos puxar as imagens recentes de um perfil do Pinterest com feed, precisando somente do nome do usuário e a quantidade de fotos que quer mostrar.

Pegar as imagens do perfil

Adicione o código abaixo no functions.php

/**
 * get pinterest profile latest pins
 * via feed rss
 * @author Difluir https://difluir.com/
 */
if ( ! function_exists('difluir_modules_get_pinterest_feed') ) :
	function difluir_modules_get_pinterest_feed( $args = array() ) {
        $defaults = array(
            'username' => '',
            'limit'    => 4
        );

        $args = wp_parse_args( $args, $defaults );
        $args = array_merge( $defaults, $args );

		$username = ( ! empty($args['username']) ) ? esc_attr( $args['username'] ) : '';

		if ( empty($username) ) {
			return false;
		}

		$number = absint( $args['limit'] );

		$key  = 'difluir_pins_'.esc_attr( $username );
		$data = get_transient( $key );

		/* create cache */
        if ( $data === false ) {
			$data = array();

			include_once(ABSPATH.WPINC.'/feed.php');
			$pins_url  = 'https://www.pinterest.com/'.esc_attr( $username ).'/feed.rss';
			$rss       = fetch_feed( $pins_url );
			$maxitems  = $number;
			$rss_items = '';

			if ( ! is_wp_error($rss) ) {
				$rss->set_timeout(60);
				$maxitems = $rss->get_item_quantity((int)$number);
				$rss_items = $rss->get_items(0,$maxitems);
			}

			if ( $rss_items ) {
				foreach ( $rss_items as $item ) {
					$new_caption = htmlentities( $item->get_title(), ENT_QUOTES, get_bloginfo('charset') );
					preg_match( '/src="([^"]*)"/', $item->get_content(), $matches );

					$data[] = array(
						'url'     => esc_url($item->get_permalink()),
						'images'  => array(
							'thumbnail' => ( isset($matches[1]) ) ? $matches[1] : '',
							'large'     => ( isset($matches[1]) ) ? str_replace( array('/192x/', '/236x/'), '/564x/', $matches[1] ) : ''
						),
						'caption' => $new_caption
					);
				}
                /* update every 24 hours */
				set_transient( $key, $data, 24 * HOUR_IN_SECONDS );
			}
		}

		return $data;
	}
endif;

Criando um shortcode para mostrar as imagens

Adicione o código abaixo também no functions.php.

/**
 * Pinterest Shortcode
 * @author Difluir https://difluir.com/
 */
if ( ! function_exists('difluir_shortcode_pinterest') ) :
	function difluir_shortcode_pinterest( $atts, $content = null ) {
		$atts = shortcode_atts( array(
	        'username' => '',
			'limit'    => 4
	    ), $atts, 'difluirpin' );

		$pins = difluir_modules_get_pinterest_feed( $atts );

        if ( ! $pins or $pins == false ) {
            return;
        }

        $output = '<ul>';
            $p = 1;
            foreach ( $pins as $key => $value ) {
                $caption = ( isset($value['caption']) && !empty($value['caption']) ) ? esc_attr( $value['caption'] ) : esc_attr( __('image without description', 'difluirtextdomain') );
                $output .= '<li class="item-'.$p.'">';
                    $output .= '<a href="'.esc_url( $value['url'] ).'" target="_blank">';
                        $output .= '<img src="'.esc_url( $value['images']['large'] ).'" alt="'.$caption.'" />';
                    $output .= '</a>';
                $output .= '</li>';
                $p++;
            }
        $output .= '</ul>';

	    return $output;
	}
endif;

if ( ! shortcode_exists('difluirpin') ) {
	add_shortcode( 'difluirpin', 'difluir_shortcode_pinterest' );
}

Insira o shortcode no post, página ou widget

[difluirpin username="USUARIO" limit="4"]

No shortcode, altere o USUARIO para o nome do usuário no pinterest que quer mostrar as imagens. E altere o 4 para a quantidade de imagens que quer mostrar.

Esse código só funciona no WordPress, e ele vai criar um cache com transient que é atualizado a cada 24 horas.