Trouble displaying post content with this plugin
-
Hello,
I am currently trying to get this plugin to function, and I can successfully access the posts on the WP site I’m trying to pull from, using a REST browser plugin. I am confused about how I get the posts to actually show up on another site though. I’ve used a shortcode, and it only ends up displaying a 1px box with a link that says Source: http://www.URL.com/wp-json/wp/v2/posts, but no content. If I go to that link, I can see all the code for posts.
My ultimate goal is to load blog posts from two external sites among the blog posts of one site (ex: site B and C blog posts display interspersed among site A’s blog posts on Site A). I am assuming this will involve modifying the get_posts code in my theme, and replacing it with code that pulls posts from site A, B, and C, which all have the Rest API enabled, and use the existing masonry grid post display code to display it. Just really confused about how to get there. If you have any advice on any of these questions, they’d be most appreciated.
-
My ultimate goal is to load blog posts from two external sites among the blog posts of one site (ex: site B and C blog posts display interspersed among site A’s blog posts on Site A)
To do that, you’ll need to use the
jeherve_post_embed_post_loopfilter from my plugin, to insert more posts (from another site) into an existing loop of posts returned by the shortcode.However, before to do that, you’ll need to get the shortcode to work for both of your sites, separately. Since you want to use the core REST API, here is how your shortcode should look like:
[jeherve_post_embed url="https://jeremy.hu" wpapi="true" include_content="true" number="1"]Could you try to paste that shortcode into a page on your site, and let me know how it goes? If the REST API Post Embeds plugin is properly activated on your site, it should work.
If that works, try replacing my site URL by one of yours, where the “WordPress REST API (Version 2)” plugin is activated:
https://ww.wp.xz.cn/plugins/rest-api/If you don’t see any post content from either of the sites, could you add the following to your site’s wp-config.php file?
define('WP_DEBUG', true); if ( WP_DEBUG ) { @error_reporting( E_ALL ); @ini_set( 'log_errors', true ); @ini_set( 'log_errors_max_len', '0' ); define( 'WP_DEBUG_LOG', true ); define('WP_DEBUG_DISPLAY', false); define( 'CONCATENATE_SCRIPTS', false ); define( 'SAVEQUERIES', true ); }Once you’ve done so, try reloading the page where you’ve inserted the shorcode, and check the
wp-content/debug.logfile for errors. You can paste the results here. You can then replacedefine('WP_DEBUG', true);bydefine('WP_DEBUG', false);in the code above.Thanks!
Thanks for your reply Jeremy. For whatever reason, I’m consistently getting the “Error in the response. We cannot load blog data at this time” error no matter what I put in there, even when I put your url there. I have very few plugins on this new dev site, so unsure of what’s causing this issue now. Will keep you posted as it resolves, because I’m very eager to try out your suggestions.
That’s interesting. I wonder if it could be related to your site’s ability to run outgoing queries. If you feel comfortable editing PHP files, could you try editing the
rest-api-post-embeds.phpunderwp-content/plugins/rest-api-post-embeds.php, look for line 472 (if ( is_wp_error( $response ) || empty( $response ) ) {, and place the following right above it:var_dump( $response );Could you let me know what it then output on the page, right above the “Error in the response. We cannot load blog data at this time” message?
Thanks!
Thanks for the response Jeremy. I updated the php file, and with your shortcode in place I got the following:
object(WP_Error)#560 (2) { ["errors"]=> array(1) { ["http_request_failed"]=> array(1) { [0]=> string(92) "cURL error 35: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error" } } ["error_data"]=> array(0) { } }With my shortcode in place ([jeherve_post_embed url=”http://www.URL.com/wp-json/wp/v2/posts” wpapi=”true” include_content=”true” number=”1″]
I got the following:object(WP_Error)#565 (2) { ["errors"]=> array(1) { ["http_request_failed"]=> array(1) { [0]=> string(89) "cURL error 6: Couldn't resolve host 'www.URL.com%2Fwp-json%2Fwp%2Fv2%2Fposts'" } } ["error_data"]=> array(0) { } }One thing to add if it’s helpful. On your site you have examples of this running via Jetpack. I can use that shortcode and successfully see your posts on my site, as a test:
[jeherve_post_embed url=”wptavern.com” number=”8″ include_images=”true” include_title=”false” include_excerpt=”false” image_size=”125,125″ tag=”jetpack” wrapper_class=”example-grid”]Thanks again for your help, it’s really appreciated.
That’s helpful, thank you!
First, let’s look at your shortcode:
[jeherve_post_embed url="http://www.URL.com/wp-json/wp/v2/posts" wpapi="true" include_content="true" number="1"]That won’t work, because of 2 issues:
- http://www.URL.com is not a valid URL
- You don’t actually need to add
wp-json/wp/v2/postsat the end of the URL. The site URL is enough, like in my example.
Back to my example, the output you got gives us some information:
cURL error 35: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal errorIf you google that error, you’ll see it’s actually quite common. The problem is related to your server configuration. I’d recommend contacting your hosting provider about this, and letting them know you get that error when trying to make outgoing requests from your site to a third-party server. They should be able to get this fixed for you.
On your site you have examples of this running via Jetpack
You could use that as well if you want. As long as Jetpack is installed and connected on the site you’re pulling posts from, and that the site owner activated Jetpack’s JSON API module there, you’ll be all set. Hopefully you won’t run into the same errors when doing that outgoing request.
Thanks Jeremy. I only had URL.com there to avoid posting the dev site url I’m using.
When I’m using the proper shortcode, it is the folllowing:
[jeherve_post_embed url="http://www.michaelgallegly.com" wpapi="true" include_content="true" number="1"]I don’t get any errors with that, and I do get the 1px outlined box with Source: michaelgallegly.com at the bottom right corner. Debugger does not return anything, and no content returned. Not sure if it matters, but there are blog posts on that homepage, but the actual blog url is http://www.michaelgallegly.com/blog/.
Additionally, at some time later, with the same short code, I started getting the following error via the debugger:
Warning: array_slice() expects parameter 1 to be array, object given in /home/mg33/public_html/mg332016/wp-content/plugins/rest-api-post-embeds/rest-api-post-embeds.php on line 302Warning: Invalid argument supplied for foreach() in /home/mg33/public_html/mg332016/wp-content/plugins/rest-api-post-embeds/rest-api-post-embeds.php on line 302That’s interesting; it appears that although the REST API works on your site, some of the routes are missing.
On this page for example, I would expect to find a
postsroute:
http://www.michaelgallegly.com/wp-json/wp/v2But it’s nowhere to be found:
http://i.wpne.ws/hDt9As a result, when trying to access that route directly, I get a 404:
http://www.michaelgallegly.com/wp-json/wp/v2/posts
http://i.wpne.ws/hD7jDo you use the most recent version of the REST API V2 plugin?
Do you use any other plugins that may edit or remove some of the routes?If you don’t, it might be worth checking with your hosting provider, to make sure they don’t blacklist some of the routes or endpoints on your site.
Let me know how it goes!
Thanks Jeremy. I’m using WordPress REST API (Version 2), and WP Version 4.6. I’m also using the plugin WP Rest API Controller, which allows me to toggle endpoints off/on, and the only one I have enabled is POST, set to http://www.michaelgallegly.com/wp-json/wp/v2/posts.
I’ve made the page accessible here: http://www.mg33.net/mg332016/resttest/
I’ll add that I’ve got two shortcodes enabled, one with only the “http://www.michaelgallegly.com” url, and the other with “http://www.michaelgallegly.com/wp-json/wp/v2/posts” just to see if that makes any difference.
I’ll add that I’ve got two shortcodes enabled, one with only the “http://www.michaelgallegly.com” url, and the other with “http://www.michaelgallegly.com/wp-json/wp/v2/posts” just to see if that makes any difference.
No need to use the latter. My plugin is meant to accept site URLs, without anything else.
I’m also using the plugin WP Rest API Controller, which allows me to toggle endpoints off/on, and the only one I have enabled is POST, set to http://www.michaelgallegly.com/wp-json/wp/v2/posts.
That’s the problem, though; that endpoint is not accessible right now, all I get is a 404:
http://i.wpne.ws/hD7jWhat happens when you disable that plugin?
Update! I disabled that plugin. Also disabled my cache plugin on the site I’m trying to pull posts from. Success! If you go to http://www.mg33.net/mg332016/resttest/ it’s working successfully now.
Massive thanks for your help Jeremy. Next step is integrating this with the jeherve_post_embed_post_loop part you talked about above in the site’s blog PHP. The end goal is to have blog posts work as I described above: On Site A, blog posts from sites A, B, and C display in the masonry-style grid of blog posts on this theme: http://andersnoren.se/themes/garfunkel/, and organized by date, with posts from B and C linking out to those respective sites. Will hopefully have some time soon to start figuring that out. If you have any tips, please let me know.
Thanks again for your help, sincerely.
Michael
I’d suggest looking at the plugin’s code and the different filters here:
https://github.com/jeherve/rest-api-post-embeds/blob/master/rest-api-post-embeds.phpI’ve commented each function and filter, so it should help you understand how this works.
You’ll want to look at the
get_posts()function, as well as thepost_list()function.Another option, if you don’t want to use filters, would be to create private pages, one for each site, to pull the posts. Each array of posts is stored for 10 minutes in a transient.
If you use a plugin like Transients Manager, you’ll see the transients generated for each site. You can then create your own function that uses get_transient to grab the array of posts for each site, and then merge the arrays to create a giant array of posts. You can then use that data in your theme.
The topic ‘Trouble displaying post content with this plugin’ is closed to new replies.