InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress beginner tutorials

  • Getting started with wordpress and some useful plugins
  • How to add tag and category to wordpress pages
  • How to become Google verified author of your wordpress blog using Google+ profile
  • How to handle permanent redirects in wordpress
  • How to write custom css in wordpress
  • Using ssh sftp updater support for updating wordpress plugins and themes
  • Wordpress - how to display code from a file in textarea
  • Wordpress - how to make a post sticky on home page
  • Wordpress themes for beginners worth considering
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress beginner

WordPress – how to display code from a file in textarea

By admin | Last updated on Feb 11, 2016

Using textarea in wordpress can cause lot of issues as wordpress automatically converts newlines to line-break (<br>) and paragraph (<p>) tags. Often switching from text to visual editor can also mess up formatting. Here is the workflow which I found worked best for me.

  1. Install wpautop-control wordpress plugin. This plugin lets you switch on and off wpautop per post or page.
  2. You may have to enable show custom field option (if not already) as shown:
    wordpress-post-edit-screen-options-collapsedwordpress-post-edit-screen-options
  3. In the posts where you want to use textarea, disable wpautop by setting the custom field “wpautop” to false
    wpautop-custom-field
  4. Now to include source code from a file (or some other text) in the textarea, The following plugin code can be used:
    function include_my_code ($atts) {
      extract(shortcode_atts(array('my_code_file' => ''), $atts));
      if ($my_code_file) {
        $content = file_get_contents($my_code_file);
        $content =  htmlentities($content);
      }
      return $content;
    }
    add_shortcode('include-my-code', 'include_my_code');

    To use this in wordpress (e.g. within textarea field) just include this in your post:

    <textarea>[include-my-code my_code_file="some_path/somefile.txt"]</textarea>

    Note that the path should be either absolute path or relative to WordPress root dir.

Security notes

This shortcode will allow anyone to display any file from the server in a post. If this is a concern, you can allow only specific directory/directories in shortcode php.

Suggested posts:

  1. How to setup wordpress custom query params with pretty url format
  2. Vim – how to go back to last edited line/context
  3. How to include bootstrap javascript and css in wordpress post
  4. Web fonts – beginner’s survival guide to web typography
  5. How to setup MailChimp Rss email campaign
  6. How to use html pre tag to display code
  7. How to display date in wordpress pages
  8. WordPress – how to add filter to description meta tag
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Tutorials, Wordpress, Wordpress beginner
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress