Using WordPress Custom Code Snippets – How to

How to add custom functionalities in WordPress.WordPress mini plugin development.WordPress Plugin Development and function editing and making.

Adding custom features and customization in WordPress can be achieved via custom code snippets, hooks, filters but most common ways to achieve custom functionalities for themes are listed below.

Use the functions.php file

Place the desired snippet in functions.php of your child theme. Steps to add function in child theme.

  1. Open wp-content -> theme -> (child-theme) -> functions.php.
  2. Copy the code and paste below the file.
  3. Any mistake can crash your site (after removing the code site will fine).

You can find more info on this in the WordPress codex of Function File explained:

Use a functions plugin

Alternatively you can use a plugin instead: Custom Fuction Plugins – which will not prevent the crashing.

Create your own custom plugin

Want to write clean code then write your snippet in mini plugin. If your code has problem, then WordPress will refuse to activate the plugin and tell you why – ergo no crashes:

A WordPress plugin can be registered in seconds. All you need is a .php file and with a proper plugin header and place it in wp-content/plugins directory on your server. Here is an example.

 Plugin Name: My simple custom plugin

/*custom code snippet START*/
 function my_custom_snippet() {
 //here comes the good stuff
 add_action( 'get_header', 'my_custom_snippet' );

/*custom code snippet END*/

You can read more about plugin headers here: /Writing_a_Plugin #File_Headers

Leave a Reply

Your email address will not be published. Required fields are marked *