Search

הצגת פוסטים מבלוג אחר באתר שלכם באמצעות REST API

כפי שבטח שמעתם כבר, REST API מספק עבורכם את האפשרות לבצע אינטראקציה עם אתר הוורדפרס שלכם מבחוץ, למשל מאפליקציית מובייל או מאתר אחר.

בוא נראה דוגמת קוד קצרה המתארת כיצד ניתן למשוך פוסטים מהאתר CSS Tricks באתר שלכם:

// connect to the website endpoint with wp_remote_get() function
// pass params as URL query args, full parameter list is here https://developer.wordpress.org/rest-api/reference/posts/
// at this moment you can use any parameter with Context: View
// it would be strange if you can fetch drafts or private posts, right?
$response = wp_remote_get( add_query_arg( array(), 'https://css-tricks.com/wp-json/wp/v2/posts' ) );

if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {

	$remote_posts = json_decode( $response['body'] ); // our posts are here
	foreach( $remote_posts as $remote_post ) {

		// display post titles and excerpts
		echo '<h2>'. $remote_post->title->rendered . '</h2><p>' . $remote_post->excerpt->rendered . '</p>';
		// need more parameters? print_r( $remote_post )

	}
}

ניתן להוסיף גם פרמטרים לקריאה, למשל מספר הפוסטים שאתם מעוניינים למשוך תתבצע על ידי הוספת פרמטרים למערך. הנה אותו קוד המגביל את מספר הפוסטים שאתם מושכים מ CSS Tricks לארבעה בלבד (שימו לב להבדל בשורות 5-7) :

// connect to the website endpoint with wp_remote_get() function
// pass params as URL query args, full parameter list is here https://developer.wordpress.org/rest-api/reference/posts/
// at this moment you can use any parameter with Context: View
// it would be strange if you can fetch drafts or private posts, right?
$response = wp_remote_get( add_query_arg( array(
	'per_page' => 4
), 'https://css-tricks.com/wp-json/wp/v2/posts' ) );

if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {

	$remote_posts = json_decode( $response['body'] ); // our posts are here
	foreach( $remote_posts as $remote_post ) {

		// display post titles and excerpts
		echo '<h2>'. $remote_post->title->rendered . '</h2><p>' . $remote_post->excerpt->rendered . '</p>';
		// need more parameters? print_r( $remote_post )

	}
}

איך לבטל לגמרי את REST API /wp-json באתר שלכם

מה אם אינכם רוצים שמישהו יכול לבצע אינטראקציה עם ה API של אתר שלכם בכדי לקבל למשל פוסטים ללא רשות? במקרה זה עליכם לבטל את /wp-json/. הקוד הבא יעבוד בוורדפרס 4.7 ומעלה:

function sv_no_rest_api( $access ) {
 
	return new WP_Error( 'rest_cannot_access', 'Sorry - Access Denied', array( 
		'status' => 403 
	) );
 
}
add_filter( 'rest_authentication_errors', 'sv_no_rest_api' );
ביטול wp-json & REST API

עד כאן.

רועי יוסף
רועי יוסף

מפתח אתרים ותבניות וורדפרס יעודייות על בסיס עיצוב. אוהב טיפוגרפיה, צבעים ומה שבינהם ומכוון לספק אתרים רספונסיבים עם ביצועים גבוהים, מותאמים למנועי חיפוש ובעלי קוד ולידי, סמנטי ונקי.

2 תגובות...
  • איש נחמד 20 פברואר 2021, 20:03

    זהירות, לפני שאתם מבטלים את ה-REST API באתר שלכם תבדקו אם הוא אינו נדרש לאחד מהתוספים שלכם.
    חלק מפייג' בילדרים לא יעבדו אם תבטלו את הדבר הזה

    • רועי יוסף 20 פברואר 2021, 20:59

      נכון, תודה שציינת…

תגובה חדשה

Up!
לבלוג