How to use WordPress custom code snippets

Additional features and minor customisations can be achieved via the use of custom code snippets.

Use the functions.php file

These snippet go in the functions.php file of your (child) theme. You can find more info on this in the WordPress codex here: https://codex.wordpress.org/Functions_File_Explained
Be careful though. Any syntax or coding error will make your site crash.

Use a functions plugin

Alternatively you can use a plugin instead: https://wordpress.org/plugins/my-custom-functions/ – which will not prevent the crashing.

Create your own custom plugin

Even better and more elegant. Construct this into a mini plugin. If your code is faulty, 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.

<?php
/*
Plugin Name: My super duper custom plugin
*/

/*custom code snippet START*/
function my_custom_code () {
    //here comes the good stuff
}
add_action( 'get_header', 'my_custom_code' );
//etc. 🙂

/*custom code snippet END*/

You can read more about plugin headers here: http://codex.wordpress.org /Writing_a_Plugin #File_Headers

Related Posts

Powered By Related Posts for WordPress

Leave a Reply

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