Replace template by plugin - woocommerce

The instruction to replace the template like cart.php is to create a duplicate in the theme directory like
/wp-content/themes/yourtheme/woocommerce/cart/cart.php.
But my question is, can I make it in a plugin instead of a theme like:
/wp-content/plugins/yourplugin/woocommerce/cart/cart.php
Will this work? If yes, is it a good practice or it’s not recommended?

Sure you can. WooCommerce provides a filter just for that:
// Allow 3rd party plugin filter template file from their plugin.
$filter_template = apply_filters( 'wc_get_template', $template, $template_name, $args, $template_path, $default_path );

Related

Is there some way to insert custom archive loop to any theme from plugin?

I'm coding plugin that create custom post type with custom fields. Now I need to build custom archive/category/tag templates which should contain custom fields.
The problem is that I couldn't insert template part inside archive loop from plugin.
Is there some hook to insert custom loop inside activated theme? Something like this:
add_filter('the_content', 'change_content_in_single_post');
Now I'm using this hook:
add_filter( 'template_include', 'change_whole_cpt_archive_template' );
... but when I use template_include hook it changes whole template and I need to do something like the_content filter. That hook get template of current theme and replace standart content template with my custom template - make it part of activated theme. It makes my CPT single page template compatible with any wp theme. Now I need to replace standart archive loop with my custom loop. For example: on archive page show posts without images... It must be something that replace result of standard get_template_part() function with custom template from plugin. That's what I`m searching for...
Anyway, maybe you know some better ways to make plugin (archive template) compatible with wp themes?
Huge thanks for any help!
you need to introduce some logic to only run on your cpt archive page.
add_filter( 'template_include', 'change_all_archive_template' );
function change_all_archive_template($template){
if ( is_post_type_archive('cptname') ) {
$template= 'find the template!';
//if( file_exists ) --> look in theme 1st??
//else --> load from plugin..
}
return $template;
}
There are quite a few plugins that use this technique to look in the active theme 1st and if not found, find the file in your plugin.
I found solution in woocommerce plugin files.
So... the best way to build custom archive template from plugin is to use template_include hook and in custom template file set header and footer from activated theme:
<?php get_header();
// custom archive loop
get_footer(); ?>
To make it more compatible with any wp themes use stanard wordpress and bootstrap classes.

Finding hooks Wordpress

I know there are lists of hooks for WordPress like --> http://adambrown.info/p/wp_hooks/hook
But if I want to find hooks for a plugin like WC Vendors there is a much shorter list of hooks on their website.
Are 'do_action' and 'apply filter' functions the only thing we can modify?
If given a class like --> https://github.com/wcvendors/wcvendors/blob/master/classes/admin/class-product-meta.php#L10, is there any way to modify it?
Are we limited to the do_action hooks or is there a way to modify other areas as well? Can we use the WordPress hooks to hook into the WC Vendors plugin as well?
Mostly you should try to accomplish any task with hooks, but some tasks are just not possible without actually modifying the actual code. But we all know its not good to modify core code, as all changes disappear after an update. So instead of modifying a class, you can extend it. You can override the current features and also add new ones. Extending a class is as easy as using a relavant hook in functions.php and then extending it in the same file or requiring it from another file. Here is an official tutorial on how to add a new shipping method to the woocommerce shipping class.
Sometimes you dont even need all the hooks, you just need to find the ones that are running on a specific page. For this you can use the code below to list all the current hooks.
$debug_tags = array();
add_action( 'all', function ( $tag ) {
global $debug_tags;
if ( in_array( $tag, $debug_tags ) ) {
return;
}
echo "<pre>" . $tag . "</pre>";
$debug_tags[] = $tag;
} );
Or you can use this plugin "simply show hooks"which is really helpful while development as it gives you an idea of where each hook is being triggered on the page.

Can I set wordpress theme via code

I have been working on wordpress for a while. I know that we can manage themes via the admin panel and the selected theme and its configuration details are stored in the database.
Is there any way I can set the theme via my wordpress code?
Have a look at the switch_theme function on the Codex
Description
Switches current theme to new template and stylesheet names.
Accepts one argument: $stylesheet of the theme. ($stylesheet is the name of your folder slug. It's the same value that you'd use for a child theme, something like twentythirteen.) It also accepts an additional function signature of two arguments: $template then $stylesheet. This is for backwards compatibility.
Usage
<?php switch_theme( $stylesheet ) ?>

Overwrite plugin files?

I want to overwrite the file plugins/the-events-calendar/tickets/meta-box.php of the plugin The Events Calendar.
I followed the tutorial Overwrite Plugin Files but this is not working for me.
How can I overwrite this file?
i want to add new metabox in evetns Ticket section how can i do without edit the plugin file other wise overwrite that plugin files.
I don't have the paid Ticket plugin you refer to, but I took a bit of time to skim through the available GitHub code:
https://github.com/moderntribe/the-events-calendar/
It looks like the overwrite option (i.e. moving a file from the plugin folder to the tribe-events/ folder within your current theme directory) only applies to the views templates in:
https://github.com/moderntribe/the-events-calendar/tree/master/views
You can see for example the definition of the getTemplateHierarchy() function here.
But this overwrite option doesn't apply to the /admin-views/tickets/meta-box.php file, since it's included here with the default PHP include():
include $this->path . 'admin-views/tickets/meta-box.php';
through this method call here:
TribeEventsTicketsPro::instance()->do_meta_box( $post_id );
where the metabox comes from:
add_action( 'add_meta_boxes',
array( 'TribeEventsTicketsMetabox', 'maybe_add_meta_box' ) );
Looking at the source of this file, I found this part:
<?php do_action( 'tribe_events_tickets_metabox_advanced',
get_the_ID(), NULL ); ?>
This might be what you are looking for, if you want to add some extra tickets UI to the event editor. Try to hook into this tribe_events_tickets_metabox_advanced action to add your extra UI.
Hope this help.

Customize search results for Custom Post Types

I am writing a Wordpress plug-in that creates several Custom Post Types (CPT). For they have their own custom fields, that need to be displayed in the search results, I need to customize the search results output.
Do I need to write my own theme for that or is there a hook (or other way) to solve this in my plug-in code?
You could hook into get_the_content and get_the_excerpt filters and test with is_search() to see if you should alter the returned value or not.
Not tested, but this is the idea:
add_filter( 'get_the_excerpt', 'my_search_excerpt' );
add_filter( 'get_the_content', 'my_search_excerpt' );
function my_search_excerpt( $content ) {
if ( is_search() ) {
$content = 'This is a search excerpt for ' . get_the_title();
// maybe add a read more link
// also, you can use global $post to access the current search result
}
return $content;
}
I see four possibilities:
Create a new (child) theme
Override the search template using filters (template_include)
Use client-side code to modify the appearance (CSS / JavaScript, poor workaround)
Hook the_content or the_excerpt
The easiest way might be to copy the search.php file of your installed theme and modify it to fulfill your needs. You can then hook it in using the first or second way. The first requires you to create a child theme, the second to create a plugin. The latter might be more complex so I would suggest to create a theme (take a look at template files of child themes for an explanation).

Resources