There’s good reason to install plugins but if you can replace a plugin with PHP code it’s often easier to do so. But too many plugins can cause your site to run much slower and feel surprisingly bloated. You’ll find more code snippets which you can use to create custom functions on Github and Pastebin.Every new WordPress install can be improved with plugins. You can also use custom functions with your themes hooks to display content in any hook location like you header. Let’s take a look at using custom functions to add widgets in WordPress themes which use hooks. This method can cause problems when updating your theme because the template files will be overwritten meaning your custom coding will be lost. If you’re not using a theme which offers different hook locations, you’ll need to add the widget using a different method and modify your themes template files. Using a theme framework which offers built in hooks makes it easy to display your widgets content in that specific location.
You might also want to display content on multiple pages using conditional tags in a custom function. Replace the above function call your_template_tag() with the template tag your plugin uses. This custom functions outputs immediately after the entry content and enables you to add:Īdd_action( 'genesis_entry_footer', 'your_function' ) Let’s take a look at some code for registering a widget and creating the function to hook the widget into a location your theme offers hooks. Now that you understand how custom functions work with theme specific hooks and conditional tags, you can take these coding snippets further.
single-title class as your selector to style the content in this location. This code displays your content after your post titles on single posts only. This code displays your content in your header on the home page only. To prevent this you can simply add a conditional tag after the function.
If you used any of the above code snippets, you’ll also notice your custom content also displays on all your archive pages like your home, blog, category and author pages. title-text class as your selector for styling your content in this area of your theme. Use this code snippet to display text or HTML after all your titles. add-text as your selector to style your text or HTML. Simple Custom FunctionsĪdd text or HTML between the opening and closing div tags. You can replace the genesis hooks in this code to your own theme specific hooks, change the hooks to another location in your theme or modify the code to create the function you need.Īll custom functions and modifications to PHP code should be done in your child themes functions.php file. Use these code snippets to add custom functions and modify existing functions in your theme.