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

How to write custom php in wordpress

By admin | Last updated on Mar 18, 2016

If you are just writing text articles then you never need to write any custom php in wordpress. In case you want to write dynamic articles or pages, you will very soon need to write custom php.

Some reasons for writing custom php

  1. You want to include a source code or some other content from a file.
  2. You may have some device specific code for mobiles (based on user agent).
  3. You may have a custom query param and based on that want to show some specific information on a wordpress page.
  4. You want to customize the theme you are using
  5. You want to customize some wordpress default functionality.

Writing custom wordpress plugin

You can write your own plugin (with a php file at a minimum with any name). You will have to put the plugin folder in .../wp-content/plugins/ folder. e.g. If your plugin name is foo and it has a file foo.php, then foo.php should be placed at path .../wp-content/plugins/foo/foo.php and then it can be activated like a any plugin from wordpress admin ui.

Two types of php code (among many others) you can write with plugin are:

  1. shortcode: Here is how shortcode code snippet looks like:
    function display_ip_impl ($atts) {
      echo $_SERVER['REMOTE_ADDR'];
    }
    add_shortcode('display_ip', 'display_ip_impl');
  2. Filter: You can apply filter on title, content, etc. of your post. This can be useful if you want to change some part of your wordpress post or page. Here is sample code snippet to change h1 title for wordpress tag page:
    function my_single_tag_title($prefix, $display=false ) {
      // Some logic to decide title
      return $title;
    }
    add_filter('single_tag_title', 'my_single_tag_title');
    

    Few examples where you can use filter are:

    1. Insert author information at the end of a post.
    2. Or change html title and append a common suffix.
    3. Change h1 heading for tag and category pages
    4. Append related information or links to every post based on some data outside wordpress tables.

Suggested posts:

  1. Using ssh sftp updater support for updating wordpress plugins and themes
  2. How to display date in wordpress pages
  3. WordPress – how to add filter to description meta tag
  4. Vim – how to go back to last edited line/context
  5. How to setup MailChimp Rss email campaign
  6. How to include bootstrap javascript and css in wordpress post
  7. How to use Google custom search with lazy loading approach
  8. How to hide a post from home and RSS feed in wordpress
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