• I’m running a theme that has a custom post types built in, and essentially what I need to do is display a book cover on the post (actually a custom post) page when the ISBN was already entered into a custom field when the post was created.

    I understand the link that shows info from Google Books, but what I do not understand is how to pass that info onto WordPress (or even where to begin!). I would very much appreciate it if someone could at least point me in the right direction.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    If you can determine how to build a link to the correct image on Google’s servers from the ISBN, this is attainable, assuming it fits within Google’s TOS to deep link their images this way.

    There’s several approaches on how to display the image in a post. One would be to hook the filter ‘the_content’ and inject the HTML img tag with the requisite image src in front of the regular content.

    Thread Starter danf89

    (@danf89)

    Thank you so much for your reply! I did actually figure out more or less how to fetch the info from Google by studying how other sites do it. There’s a link that spits out a result with all sorts of info when appended with the ISBN. So I know how to get the info. What I need is guidance on how to a) pass the ISBN from the form into the link to first get the required info and then b) pass the required info back into WP.

    I fluently know HTML and CSS and can find my way around PHP (but by no means know it), but I do not know JavaScript. Perhaps I should begin learning?

    Thread Starter danf89

    (@danf89)

    I basically just have to use the isbn: query, e.g. https://www.googleapis.com/books/v1/volumes?q=isbn:0771595158, which returns a JSON response. By how do I pass the info from a form into the query and how do I pass info from the response back into WP, be it the form or a post?

    Moderator bcworkz

    (@bcworkz)

    You’ll be pleased to know PHP is a better choice than javascript in this case because you can’t know what filters or security the client has going to prevent access to third party sites.

    Is the URL for the image static? Will it never change over time? Then it’s best to grab the value when saving the post and insert the img tag with the URL directly into the post content. Otherwise, my initial suggestion to inject it into the content on each request would have to suffice. (And probably introduces a huge performance hit to load many images)

    Finding a hook that fires only when the post is first created and not subsequently is a bit tricky. ‘save_post’ will fire on any update as well as the initial save, so is unsuitable. One needs to experiment with the various parameters passed to the ‘transition_post_status’ action to find which variable action fired in the wp_transition_post_status() function will work. IIRC, that would be the action ‘draft_to_publish’. (You should verify this for yourself.) This action fires for every new post published, so your callback function must first check the post type is correct for inserting cover images. The current post object is passed to your hook callback for your use.

    At this point, all the post data has been saved, but I’m not sure about post meta, which is where the ISBN gets saved. If it has been saved, use get_post_meta(). If not, it should still be in $_POST. You’ll need to examine a var_dump() of it to determine the proper key name. Get the ISBN value and append it to your API query string.

    Then how to query another site for the image URL. This depends on your server configuration, some methods are not always available. If available, I like using cURL. There is also HTTP and failing that, fsock. Plenty of generic examples on using these transports can be found online.

    To interpret the JSON response, use json_decode(). Do a var_dump() of the returned array to figure out how to extract the URL you’re looking for. Then insert the HTML img tag with the src URL in front of the content (or where ever you want it to appear) and update the post data before returning.

    This entire process can be done from within your action hook into ‘draft_to_publish’ or whatever it is, just be sure to do some error handling in case of connection issues with an external site.

    I’ve obviously left out many details, but this will get you going in the right direction at least. Good luck!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to get book cover from Google Books utilizing ISBN entered in custom field??’ is closed to new replies.