Overriding Templates

The template files used by NM Gift Registry and Wishlist are located in the templates directory in the root folder of the plugin. Every one of these templates can be overridden by copying it to your theme and changing its content, the same way WooCommerce templates are overridden by copying them to your theme.

In many cases you may not need to override the template but instead use available action and filter hooks to modify its content. In the case of the plugin shortcodes which output template content for example, many of them come with pluggable functions and have standard filters both of which allow for completely overriding the default content displayed. This gives you a variety of ways to override the templates as you wish.

The table below lists the pluggable functions and filters available for overriding the output of plugin shortcodes.

ShortcodePluggable functionFilter to override template
[nmgr_account]nmgr_get_account_template()nmgr_account_template
[nmgr_account_wishlist]nmgr_get_account_wishlist_template()nmgr_account_wishlist_template
[nmgr_overview]nmgr_get_overview_template()nmgr_overview_template
[nmgr_profile]nmgr_get_profile_template()nmgr_profile_template
[nmgr_items]nmgr_get_items_template()nmgr_items_template
[nmgr_shipping]nmgr_get_shipping_template()nmgr_shipping_template
[nmgr_images] (Full version)nmgr_get_images_template()nmgr_images_template
[nmgr_orders] (Full version)nmgr_get_orders_template()nmgr_orders_template
[nmgr_messages] (Full version)nmgr_get_messages_template()nmgr_messages_template
[nmgr_settings] (Full version)nmgr_get_settings_template()nmgr_settings_template
[nmgr_share]nmgr_get_share_template()nmgr_share_template
[nmgr_search_form]nmgr_search_form
[nmgr_enable_wishlist_form]nmgr_get_enable_wishlist_form()nmgr_enable_wishlist_form

Example usage of pluggable function to override template

To modify the wishlist profile template, add this code to a custom plugin.

/**
 * Override the wishlist profile template by displaying only its title
 */
function nmgr_get_profile_template( $atts = '', $echo = false ) {
	$title = __( 'Profile template', 'my-plugin-textdomain' );

	if ( is_array( $atts ) && isset( $atts[ 'title' ] ) ) {
		$title = $atts[ 'title' ];
	}

	if ( $echo ) {
		printf( __( 'Modified %s', 'my-plugin-textdomain' ), esc_html( $title ) );
	} else {
		return sprintf( __( 'Modified %s', 'my-plugin-textdomain' ), esc_html( $title ) );
	}
}

Example usage of filter hook to override template

To modify the wishlist profile template, add any of the two code snippets below to a custom plugin or your theme’s function.php file.

/**
 * Override the wishlist profile template by displaying only its title
 */
function my_plugin_profile_template( $template_html, $vars ) {
	ob_start();

	if ( isset( $vars[ 'title' ] ) ) {
		printf( __( 'Modified %s', 'my-plugin-textdomain' ), esc_html( $vars[ 'title' ] ) );
	}

	return ob_get_clean();
}

add_filter( 'nmgr_profile_template', 'my_plugin_profile_template', 10, 2 );
/**
 * Override the wishlist profile template by displaying only its title
 * Substitute the plugin's profile template with one in your theme.
 */
function my_plugin_profile_template_2( $template_html, $vars ) {
	// Make sure 'profile-new.php' exists in the plugin's template directory in your theme.
	return nmgr_get_template( 'profile-new.php', array( 'vars' => $vars ) );
}

add_filter( 'nmgr_profile_template', 'my_plugin_profile_template_2', 10, 2 );

/**
 * Sample profile-new.php content
 *
 * This code should reside in my-theme/nm-gift-registry-lite/profile-new.php if using the lite version
 *  or my-theme/nm-gift-registry/profile-new.php if using the full version of the plugin.
 * 'my-theme' should be the name of your theme.
 *
 * <?php
 * if ( isset( $vars[ 'title' ] ) ) {
 * printf( __( 'Modified %s', 'my-plugin-textdomain' ), esc_html( $vars[ 'title' ] ) );
 * }
 */

The code snippet above uses a custom file profile-new.php to display the new template content instead of putting it in a function as the first did. This file is placed in the plugin directory in your theme folder and it is fetched with the function nmgr_get_template() with the variables the file needs to compose its content being passed to it as the second argument of the function.

Note: You can use the function nmgr_get_template() to get any template file from the plugin directory of your theme folder or from the templates directory of the NM Gift Registry and Wishlist plugin folder.

Using the plugin’s folder in your theme directory is a handy way to override templates. We will discuss this next.

Overriding templates directly by copying their files to your theme

While only the plugin shortcodes can be overridden using pluggable functions and direct filters, every single template file used by the plugin can be overridden by copying it to your theme and editing it from there. If you are familiar with the way WooCommerce templates are overridden, the templates for NM Gift Registry and Wishlist are overridden exactly the same way.

Firstly, in your theme’s root directory create a folder named nm-gift-registry-lite (if using the lite version) or nm-gift-registry (if using the full version).

Note: The name of the theme folder can be changed using the filter nmgr_theme_path. See code snippet below.

/**
 * Change the default templates directory name for 
 * NM Gift Registry and Wishlist in my theme
 */
function my_theme_nmgr_theme_path() {
	return 'nm-gift-registry-and-wishlist';
}

add_filter( 'nmgr_theme_path', 'my_theme_nmgr_theme_path' );

Secondly, copy the template files you want to edit from the plugin’s templates directory to the new plugin’s folder you created in your theme. If the template file is in a sub-directory of the plugin’s templates directory you should also copy the sub-directory with it. For example to override the wishlist profile template, copy wp-content/plugins/nm-gift-registry/templates/account/profile.php to wp-content/themes/[your-theme-folder]/nm-gift-registry/account/profile.php.

Now you can edit the profile.php file in your theme directory and it’s contents would override the contents of the plugin’s own profile.php when the profile template is displayed.

Note: The code nmgr()->theme_path returns the path (directory) where the plugin templates are copied to in your theme.

Most times however you wouldn’t need to use this code. You would rather simply use nmgr_get_template() to get a file specifically from the plugin’s directory in your theme.