X

Inclure un fichier externe Shortcode

Snippets by IsItWP

Les fichiers externes sont utilisés pour afficher du contenu externe sur votre site web WordPress. Avec l’aide d’un shortcode, vous pouvez facilement y parvenir.

Instructions : Ajoutez le code suivant au fichier functions.php de votre thème WordPress pour inclure le shortcode de fichier externe.

function show_file_func( $atts ) {
  extract( shortcode_atts( array(
    'file' => ''
  ), $atts ) );
 
  if ($file!='')
    return @file_get_contents($file);
}
 
add_shortcode( 'show_file', 'show_file_func' );

Vous pouvez maintenant utiliser le shortcode ci-dessous avec votre lien de fichier externe pour inclure le fichier externe dans vos articles et pages WordPress.

[show_file file="http://www.mysite.com/somefile.html"]

Vous pouvez également apprécier la création d’un formulaire de téléchargement de fichier dans WordPress.

Commentaires   laisser une réponse

  1. Please consider to add a second version of your code which uses a transient which expires eg. every 24h or similar to avoid too frequent external file_get_contents() calls.

    developer.wordpress.org/apis/handbook/transients/

  2. Just Wow! I had a code working before but then the recent update messed it up and it’s now returning an error. I spent hours fixing it but turned out hopeless. I searched for a fix and found yours. Perfectly fits my code now. Thanks!

    1. Hey, you’re welcome. We are glad to know that our code helped.

  3. You’ve provided the solution to my countless hours of research in a minute. You are a darling.

  4. Many thanks for the well explained and simple contribution.
    I have searched for a long time at Google and Co. for such a code. All code’s tried so far have not worked.
    But this one from the post works very well and has been written in an understandable way.

    Only one little thing still bothers me.
    If I call the link to be included directly, all letters are displayed correctly.
    If I look at the included link in the WP, ö,ä,ü become funny with ? characters are displayed.
    Why this, that in the file (link) to be included I have deposited the following code.

    I also placed the line at WP, where I also placed the including link, but it didn’t work.

    How can I still fix this small error?

    1. Could you post an example of how the link changes?

  5. It’s done. Thank you very much.

    1. Glad it helped, Baba!

  6. WP variables aren’t recognized in the included file. Also tried $_GET, $_POST but to no avail. How to fix this?..

    1. This is not something that I have tried to do, however I’m not sure why you would want to include WP variables when you could do this within the post or using the functions.php and shortcode. What is it that you are trying to do, this may help me understand the best option.

  7. Chris Olbekson août 6, 2013 à 2:52 pm

    This snipp has serious security implications. It could allow any editor or user to access and read the contents of any file on the server. You could use the WordPress built in HTTP API and use wp_remote_get instead of file_get_contents().

  8. Good

  9. Its sounds good but how to include code that works ? I mean, I have a nextgen gallery stuff I would include this way, but shortcode does not works (it show nextgen shortcode and not the thumbnails) also plugins does not works (such as auto-hyperlink, even css does not works) on the remote included file’s contents.

  10. very nice!

  11. Will this work in a widget area?

    1. You would need to enable shortcode for widgets, eg:

      add_filter(‘widget_text’, ‘do_shortcode’);

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 !