Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for overriding or adding new services in selected theme #116

Open
jruns opened this issue Mar 31, 2023 · 0 comments
Open

Add support for overriding or adding new services in selected theme #116

jruns opened this issue Mar 31, 2023 · 0 comments

Comments

@jruns
Copy link

jruns commented Mar 31, 2023

I needed to modify the code of the services built into the plugin, and to have the ability to create my own services. So I added the following to my site's version of Shariff Wrapper, copied from the em_locate_template() function of the Events Manager plugin (which is distributed under the GPL). You can either use this or create your own version if that license doesn't allow you to include it in your plugin.

In shariff.php:

Change both instances of
$path_service_file = dirname( __FILE__ ) . '/services/shariff-' . $service . '.php';

To
$path_service_file = shariff3uu_locate_template( 'services/shariff-' . $service . '.php', false );

Add this PHP function:

/**
 * Works much like <a href="https://developer.wordpress.org/reference/functions/locate_template/" target="_blank">locate_template</a>, except it takes a string instead of an array of templates, we only need to load one.
 * @param string $template_name
 * @param boolean $load
 * @uses locate_template()
 * @return string
 */
function shariff3uu_locate_template( $template_name, $load=false, $the_args = array() ) {
	//First we check if there are overriding tempates in the child or parent theme
	$located = locate_template(array('plugins/shariff/'.$template_name));
	if( !$located ){
		$located = apply_filters('shariff3uu_locate_template_default', $located, $template_name, $load, $the_args);
		if ( !$located && file_exists(dirname( __FILE__ ).'/'.$template_name) ) {
			$located = dirname( __FILE__ ).'/'.$template_name;
		}
	}
	$located = apply_filters('shariff3uu_locate_template', $located, $template_name, $load, $the_args);
	if( $located && $load ){
		if( is_array($the_args) ) extract($the_args);
		include($located);
	}
	return $located;
}

Then in your theme folder you create a plugins/shariff/services folder. And create php files prefixed with shariff- that mirror the PHP structure of services included in the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant