In WordPress theme development, removing code duplications is crucial for optimizing performance and maintaining readability. By identifying and eliminating redundant segments, developers streamline themes, improving loading times and user experiences. Through refactoring, common patterns can be abstracted into reusable components, reducing the codebase size and simplifying future updates.
To optimize your code while creating a custom theme in WordPress, you may use two approaches as 1) function declaration and calling and 2) with get_template_part(); WP function.
1) Function Declaration & Calling Function in Pages & Posts
In WordPress development, declaring functions and calling them in pages and posts is like having a toolkit for your website. By defining functions in one place, you can reuse them wherever needed, making your code cleaner and more organized. Want a banner on your page? Just call the banner function! Need to process a form? Use the form function! It’s a simple way to keep your site running smoothly and your development process hassle-free. Let’s dive into how this handy technique makes building WordPress websites a breeze.
1.1) Function Declaration
Function declaration in WordPress theme development is the process of defining reusable blocks of code to perform specific tasks. These functions can be created in the theme’s functions.php file or within custom plugins. Once declared, functions can be called from any template file within the theme, enhancing code organization and promoting reusability.
Function declaration defines a function’s name, return type, and parameters. They’re typically created at the beginning of a program or in a separate file for organization. Arguments are values passed to a function, allowing it to perform tasks. Functions can have zero or more arguments. Additionally, functions may have a body containing the code to execute when called.
function pageBanner($thePassedArgs = NULL){
echo $thePassedArgs['custom_title'];
echo $thePassedArgs['your_subtitle'];
}
This function pageBanner(); can be used anywhere in the template to pullout the section and background image for the section
1.2) Calling The Function
To make the function flexible, you have to pass array of arguments while calling the function like:
pageBanner(array( 'custom_title' => 'Hello this is the title', 'your_subtitle' => 'The subtitle here' ));
1.3) Fallback array data if the data is not passed with the array
You can set default values for passed arguments if they’re empty by using if statements before utilizing the argument values in the template. This means checking if the arguments are empty before using them inside the template. If they are empty, you can assign default data to them. Then, you can safely use the arguments inside the template.
<?php
function pageBanner($passedArgs = NULL){
if (!isset($passedArgs['title'])) {
$passedArgs['title'] = get_the_title();
}
if (!isset($passedArgs['subtitle'])){
$passedArgs['subtitle'] = get_field('page_banner_subtitle');
}
?>
<h1><?php echo $passedArgs['title'] ?></h1>
<h5><?php echo $passedArgs['subtitle'] ?></h5>
<?php }
?>
What is $passedArgs = NULL I used in the function declaration? In the function declaration process, “$passedArgs = NULL” sets a default value of NULL for the parameter “$passedArgs“, meaning if no argument is passed when calling the function, it will default to NULL. This allows the function to be called without providing any arguments, while still ensuring the code inside the function can handle the absence of specific arguments gracefully.
2) Creating Template Parts That Can Be Pulled with get_template_part()
Creating Template Parts allows you to modularize your WordPress theme, making it easier to manage and reuse code across different templates. These parts are snippets of code that can be pulled into various templates using the “get_template_part()” function. By calling this function within your page and post templates, you can effortlessly integrate predefined sections of code, such as headers, footers, or custom content, enhancing the flexibility and maintainability of your WordPress theme. This approach streamlines development, promotes consistency, and facilitates updates across your entire website. Steps to create template parts file & call with get_template_part(); include.
- Create a PHP File: Make a new PHP file in your theme directory containing any type of reusable code such as HTML, PHP … , the file name “past-events.php“
- Write Template Code: Inside the PHP file, write the HTML and PHP code for the section you want to reuse.
- Call with
: In your template file, useget_template_part()to include the template part.get_template_part('past-events')
This approach helps keep your code organized, promotes code reuse, and simplifies maintenance.
2.1) Simplifying Template Part Inclusion in WordPress
When utilizing , you have the flexibility to include files with names containing dashes (–) by passing two name slugs as arguments. For instance, using get_template_part() will match the file named ‘folder-name/past-events‘. Additionally, you can dynamically include template parts by utilizing functions like get_template_part('folder-name/past', 'events'). For example, get_post_type()dynamically fills in the post type after a dash. This approach streamlines template part inclusion in WordPress themes.get_template_part('folder-name/past', get_post_type())
Conclusion
Creating function & calling it VS Using the get_template_part()
In WordPress templates, reducing duplications in code can be achieved through two methods. Firstly, creating a function in a centralized location and calling it within page or post templates is beneficial when the same code needs to be used with different values. Secondly, employing by creating new template files is ideal for pulling content that doesn’t require dynamic values as arguments.get_template_part()
You might wonder which one should I use, in which situations?
- If you are creating the same code in pages template that contain different values & want to create function & pass values as argument for the function, Creating function & Calling is best choice
- If you are trying to pull the same content that doesn’t need values like arguments inside template,
get_template_part()is best choice.
—- Thankyou for Reading —-





