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 customization tutorials

  • Handle pagination in wordpress custom php code
  • How to and why disable wordpress cron
  • How to customize wordpress image alt tag
  • How to display date in wordpress pages
  • How to display wordpress page list with specific custom field value
  • How to display wordpress top level pages
  • How to hide a post from home and RSS feed in wordpress
  • How to include bootstrap javascript and css in wordpress post
  • How to include wordpress pages in archives
  • How to override priority and change frequency in Yoast xml sitemap
  • How to remove xmlrpc from wordpress headers
  • How to setup wordpress custom query params with pretty url format
  • How to show wordpress pages on front page with skip_home custom field
  • How to use google custom search for wordpress site
  • How to write custom php in wordpress
  • No frills social share links for Wordpress sites
  • Using Wordpress custom field for displaying code
  • Wordpress - add content filter after shortcode execution
  • Wordpress - customize category and tag links
  • Wordpress - customize facebook plugin opengraph meta tags
  • Wordpress - customize posts per page for tag, category and date archive pages
  • Wordpress - exclude specific posts from archive pages
  • Wordpress - get posts/pages with missing meta key
  • Wordpress - how to add filter to description meta tag
  • Wordpress - how to create custom tag cloud
  • Wordpress - how to exclude specific tag posts from a tag archive page
  • Wordpress - write custom php log to separate file
  • Wordpress how to check if a post is being viewed by admin
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress customization

Using WordPress custom field for displaying code

By admin on Dec 1, 2015

WordPress custom fields (post meta keys) can be used to store key value pairs for a post. This can be very useful to store code related to that post. This way it can be used by other pages also. Here are steps to use WordPress post custom field for storing and displaying code snippet.

Add post custom field

In wordpress post admin interface, add a custom field with key=code1 as shown below:
wordpress-custom-field-for-php-code-exampel

Plugin to read code from custom field and display it with pre

Now we can write a simple plugin to read the custom field and display it using html pre tag and html entities. Here is the code for it:

function handle_my_code ($atts) {
  global $post;
  $ret = "";
  extract( shortcode_atts( array(
    'key' => '',
    'postid' => $post->ID,
  ), $atts ) );
  if ($key) {
   $codeval = get_post_meta($postid, $key, true);
   $ret .= "<pre>" . htmlentities($codeval)  . "</pre>\n";
  }
  return $ret;
}
add_shortcode('my-code', 'handle_my_code');

Using the plugin

Use the following code to use the plugin and embed code from custom field in any post:

[my-code key="code1"]

Here is how it will appear on site:
wordpress-custom-field-php-code-display-using-pre

Suggested posts:

  1. How to setup MailChimp Rss email campaign
  2. How to use google custom search for wordpress site
  3. How to setup wordpress custom query params with pretty url format
  4. Find all user created wordpress custom field keys
  5. Fetch wordpress rss feed as FeedBurner user agent on command line
  6. How to display date in wordpress pages
  7. How and why disable android auto update
  8. How to include bootstrap javascript and css in wordpress post
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Tutorials, Wordpress, Wordpress customization
  • 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