X

Intégrer Google Books dans vos articles à l’aide d’un shortcode

Snippets by IsItWP

Voulez-vous utiliser un shortcode pour intégrer des livres Google dans vos articles WordPress ? Bien qu’il existe probablement un plugin pour cela, nous avons créé un extrait de code rapide que vous pouvez utiliser pour intégrer les livres Google dans vos articles à l’aide de shortcodes dans WordPress.

Instructions:

Tout ce que vous avez à faire est d’ajouter ce code dans le fichier functions.php de votre thème ou dans un plugin spécifique à votre site:

add_shortcode('gbooks', 'sc_embed_google_books');
function sc_embed_google_books( $atts ){
	extract(shortcode_atts(array(
		"id" => '',
		"width"  => '600',
		"height" => '900',
	), $atts));

	return '<script type="text/javascript" src="http://books.google.com/books/previewlib.js"></script>
        <script type="text/javascript">
        GBS_insertEmbeddedViewer("'.$id.'", '.$width.','.$height.');
        </script>';   
}

Vous remarquerez également que certains livres Google ont plusieurs numéros ISBN comme 0578022702, 9780578022703. Les deux peuvent fonctionner avec le shortcode, mais vous devez prendre le premier avant la virgule. Si le livre n’a pas de numéro ISBN, vous pouvez obtenir l’ID à partir de l’URL.

Voici le code court que vous pouvez utiliser pour afficher les livres Google dans vos articles. Remplacez simplement le numéro ISBN ou l’ID et ajoutez le numéro ISBN de votre livre Google.

[gbooks id="0578022702" width="400" height="400"]

Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez consulter notre guide sur la manière d’ajouter correctement des extraits de code dans WordPress, afin de ne pas endommager accidentellement votre site.

Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : 9 meilleurs plugins d’accordéon WordPress et comment configurer le suivi des téléchargements sur WordPress avec Google Analytics.

Commentaires   laisser une réponse

  1. Oh… and regarding, “While there’s probably a plugin for this” … I found only one. And by the looks of the screenshots it requires giving permission to display their branding. So again, this snippet you’ve created is like a gold nugget for those wanting to display Google Books on their site.

  2. Thanks for providing this code. This is the only reference I’ve found online as to how to achieve this. I am a little surprised it’s not a topic more people are discussing.

    Do you have any suggestions on how, if even possible, to make the display size responsive? For instance, working off the vw (viewport width) as a percentage? So that it can display nicely on different devices?

    Jonathan

  3. Hello, I want to modify the preview of an embed book in the post.
    Because many people misusing this code and showing my books in their posts (Sites).
    Can you help me to modify the code. How can I make a contact in email with you?

  4. Hi, I realize nobody has posted here in a while, but hopefully someone can help.

    I pasted the code in my functions.php and added the shortcode to a page. I added the google book id (no ISBN yet, but want to display a free view on site). Also pasted the id in the function code where it says id=>”
    Nothing shows up on the page, but the shortcode does disappear. Any thoughts? I’m using the Avada theme.

    1. So I got it to work with your example book. I’m thinking the problem is that my book is uploaded to “My Books” on Google. It doesn’t have an ISBN yet and is not publicly published yet. Any thoughts on how to get it to work?

  5. Kevin,
    Many thanks.  Much appreciated.  The first fruit of this you can now see at
    http://www.theologyethics.com/2011/09/23/bonhoeffers-poetry-wannenwetsch-northcott-and-odonovan-articles/
    Hope I’ve given proper due acknowledgments to you and Chad
    As I get round to developing the site and blog I think I will use this shortcode quite a bit so many thanks.

    Andrew

    1. Hi Andrew no problem, glad that I could help. Site is looking great always!

  6. This looks a really useful addition for my site but I’m totally new to WP and have not played around with code before.  I have found functions.php in my Graphene 1.4.1 theme and I added the code by using the clipboard button and then pasting it in right at the end and saving the new file.  However, when I then tried to use it by going to new post I got some sort of error code (to do with header and calling something) which stuck with me.  Fortunately I’d pasted the original file into Notepad so I simply repasted it back over the edited file and saved and all now seems to be back to normal but of course I cannot use the shortcode.
    It may be this is a bit too complex for someone new but I wonder if I’m just pasting the code in the wrong place in the file or doing something else that is easily rectified.  Can you advise me at all?
    Thanks,
    Andrew

    1. If you post the code within the functions.php file on http://pastebin.com/ I can post a revised version with the shortcode added. It may be something within your theme that the code conflicts with.

      1. Thanks.  Just pasted as Goddardfunctions.php  Never used pastebin before but think I’ve done it OK.  One other thought – if my theme is updated I presumably need to reinsert the code so should keep a copy of whatever works.  Thanks for your kind help.  Andrew

        1. Hi Andrew ill need the URL to where you pasted the code pastebin creates a random short url for each posting. 

      2. Kevin,
        Thanks. Not sure my first attempt to tell you got through.  Looks like it is to be found at  http://pastebin.com/DQtJcd8g

        1. Hi Andrew,
          I added the google books shortcode so things should work now,
          http://pastebin.com/YVkcPLUh

          just add the shortcode to your post and things should work great,
          [gbooks id=”0578022702″ width=”400″ height=”400″]

          Let me know,

  7. Gabriel Merovingi juillet 30, 2011 à 11:48 am

    There is a ” missing for the width.

    Should be:
    [gbooks id=”0578022702″ width=”400″ height=”400″]

    1. Thanks Gabriel, good call.

  8. WordPress shortcode available for Google Books | ButlerBlog juillet 29, 2011 à 9:48 am

    […] first guest blog post over at WordPress Code Snippets is a code snippet to add the Google Books viewer into your posts using an easy shortcode.  Go check it out.  While you’re there, be sure to check out the rest of the site for other […]

Ajouter un commentaire

Nous sommes heureux que vous ayez choisi de laisser un commentaire. N'oubliez pas que tous les commentaires sont modérés conformément à notre privacy policy, et que tous les liens sont en nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Engageons une conversation personnelle et constructive.

WordPress Launch Checklist

L'ultime liste de contrôle pour le lancement de WordPress

Nous avons rassemblé tous les éléments essentiels de la liste de contrôle pour le lancement de votre prochain site Web WordPress dans un ebook pratique.
Oui, envoyez-moi le gratuit !