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
- You want to include a source code or some other content from a file.
- You may have some device specific code for mobiles (based on user agent).
- You may have a custom query param and based on that want to show some specific information on a wordpress page.
- You want to customize the theme you are using
- 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:
- 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');
- 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:
- Insert author information at the end of a post.
- Or change html title and append a common suffix.
- Change h1 heading for tag and category pages
- Append related information or links to every post based on some data outside wordpress tables.