Skip to content

Instantly share code, notes, and snippets.

@espiat
Forked from smatplacid/README.md
Created August 5, 2021 12:54
Show Gist options
  • Save espiat/dfb312a33bae936e5bb6d6ff2ff751f8 to your computer and use it in GitHub Desktop.
Save espiat/dfb312a33bae936e5bb6d6ff2ff751f8 to your computer and use it in GitHub Desktop.
Oxygen Child Theme Support

Oxygen 'Child Theme' Support

This function is for developers, which use Oxygen and still want to override plugin-files when it reads in their documentation such as

  1. Create a folder in your theme folder called search-filter.
  2. Copy the file wp-content\plugins\search-filter\templates\results.php from the templates folder in to the newly created folder in your theme – wp-content\themes\your-theme-name\search-filter\results.php

Where to put such files, if Oxygen is not a theme and won't uses themes ?

Usage

Put the code in the root-file of your custom plugin, like wp-content\plugins\your-custom-plugin\plugin.php

Now you can copy the "child-theme-files" to wp-content\plugins\your-custom-plugin\oxygen-child-theme\<respective-folder> or change the value in the oxygen_child_theme_dir_name = 'oxygen-child-theme'; variable.

Tested with

  • WordPress 5+
  • Oxygen 2+ and Oxygen 3+

and the plugins

  • Search & Filter Pro 2+
  • Shortcodes Ultimate 5+
  • WooCommerce 3+

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

<?php
/**
* @since 01/09/2019
*/
add_filter( 'stylesheet_directory', 'oxygen_filter_stylesheet_directory', 10, 3 );
function oxygen_filter_stylesheet_directory( $stylesheet_dir, $stylesheet, $theme_root )
{
$oxygen_child_theme_dir_name = 'oxygen-child-theme'; // CHANGE IT TO YOUR NEEDS
$stylesheet_dir = trailingslashit( dirname( __FILE__ ) ) . $oxygen_child_theme_dir_name;
defined('STYLESHEETPATH') or define('STYLESHEETPATH', $stylesheet_dir);
return $stylesheet_dir;
}
@espiat
Copy link
Author

espiat commented Aug 5, 2021

How to override WooCommerce templates using a custom functionality plugin: (without a child theme) - useful for Oxygen too
https://wpdevdesign.com/how-to-override-woocommerce-templates-using-a-custom-functionality-plugin/

@espiat
Copy link
Author

espiat commented Aug 5, 2021

My preferred method to make customizations in WooCommerce is using actions and filters. But there could be some situations where it makes sense to duplicate the WooCommerce templates to the active child theme and edit them.

What if you don’t want to or can’t (like when using Oxygen) override from within the theme and instead want to use a custom plugin though?

Here’s how it can be done.

Step 1
Install and activate My Custom Functionality plugin.

Step 2
Using a FTP client or cPanel file manager, navigate to

/wp-content/plugins/my-custom-functionality-master

Create a directory called woocommerce.

Copy the file(s) you want to override from /wp-content/plugins/woocommerce/templates while maintaining the folder structure.

Ex.: To override cart.php, you would copy

/wp-content/plugins/woocommerce/templates/cart/cart.php

to

/wp-content/plugins/my-custom-functionality-master/woocommerce/cart/cart.php

while creating the directories as needed (in this example, “cart”).

Step 3
Add the following in

/wp-content/plugins/my-custom-functionality-master/plugin.php:

add_filter( 'woocommerce_locate_template', 'intercept_wc_template', 10, 3 );
/**
 * Filter the cart template path to use cart.php in this plugin instead of the one in WooCommerce.
 *
 * @param string $template      Default template file path.
 * @param string $template_name Template file slug.
 * @param string $template_path Template file name.
 *
 * @return string The new Template file path.
 */
function intercept_wc_template( $template, $template_name, $template_path ) {

	if ( 'cart.php' === basename( $template ) ) {
		$template = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/cart/cart.php';
	}

	return $template;

}

Replace cart.php and cart/cart.php in woocommerce/cart/cart.php as needed.

To override multiple files:

add_filter( 'woocommerce_locate_template', 'intercept_wc_template', 10, 3 );
/**
 * Filter the cart template path to use cart.php in this plugin instead of the one in WooCommerce.
 *
 * @param string $template      Default template file path.
 * @param string $template_name Template file slug.
 * @param string $template_path Template file name.
 *
 * @return string The new Template file path.
 */
function intercept_wc_template( $template, $template_name, $template_path ) {

	if ( 'cart.php' === basename( $template ) ) {
		$template = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/cart/cart.php';
	} elseif ( 'form-billing.php' === basename( $template ) ) {
		$template = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/checkout/form-billing.php';
	}

	return $template;

}

Here we are overriding cart.php and form-billing.php.


custom-plugin

  • assets
  • woocommerce
    -- cart
    --- cart.php
    -- checkout
    --- form-billing.php

What if you want to override a number of files and do not want to specify the file names/paths each time in the code?

Use the following dynamic code instead:

add_filter( 'woocommerce_locate_template', 'intercept_wc_template', 10, 3 );
/**
 * Filter the cart template path to use cart.php in this plugin instead of the one in WooCommerce.
 *
 * @param string $template      Default template file path.
 * @param string $template_name Template file slug.
 * @param string $template_path Template file name.
 *
 * @return string The new Template file path.
 */
function intercept_wc_template( $template, $template_name, $template_path ) {

	$template_directory = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/';
	$path = $template_directory . $template_name;

	return file_exists( $path ) ? $path : $template;

}

After this, you can override any WooCommerce template simply by copying it to corresponding directory in your custom plugin’s woocommerce directory and editing it.

Credits:
Woocommerce - overriding the template through a plugin:
https://stackoverflow.com/a/43776583/778809

https://pluginrepublic.com/override-woocommerce-template-plugin/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment