Extend existing wordpress plugin - wordpress

I was wondering if I could extend a existing wordpress plugin. For example adding some new features but without touching the original plugin files. Is this possible?
EDIT
I found the solution. For example in woocommerce plugin, I need to add some html to the sigle product so I wrote in function.php:
add_action( 'woocommerce_single_product_summary', 'woocommerce_single_product_my_custom_function', 15 );
function woocommerce_single_product_my_custom_function(){
echo '<p>this is my html code</p>';
}

unless the original defines hooks to extend, I am afraid this will not be possible. I suggest the way you would do that is copy the plugin directory and give it a different name in its definition

Related

Add Thumbnail Support in Wordpress Theme

I am building a theme where I need to add Thumbnails for the Category
I have tried this in the theme function.php
function spencer_cat_support(){
add_theme_support('category-thumbnails');
}
add_action('after_setup_theme', 'spencer_cat_support');
I don't know what I am doing wrong here.
I don't think that there is any support in wordpress to add thumbnail to taxonomies like this it is only available for post types and custom post types. You can either write some code to add image support to category or use some external plugin based on which you prefer.
If you prefer writing your own code check this: https://wordpress.stackexchange.com/questions/8736/add-custom-field-to-category
If you prefer external plugin check this:https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

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.

Using Jetpack Portfolio Project in WordPress child theme does not call archive custom template

I have a child theme that uses the new Jetpack Portfolio Project custom post type and wish to modify archive.php to display custom results.
I'm using: WordPress v3.9.2; Theme: Child of Point, Jetpack is installed with Custom Content Types enabled, and Portfolio Projects selected in the Settings. (No other plugins that implement portfolio functionality are installed.)
According to the Codex:
Template Files
In the same way single posts and their archives can be displayed using
the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
and if you don't have this post type archive page you can pass BLOG_URL?post_type={post_type}
where {post_type} is the $post_type argument of the
register_post_type() function.
My understanding is that if you create files called single-jetpack-portfolio.php and archive-jetpack-portfolio.php within the child theme, WordPress will automatically use those files in place of single.php and archive.php respectively.
However, my child theme successfully calls single-jetpack-portfolio.php, but completely ignores archive-jetpack-portfolio.php, instead calling archive.php in the child.
I am stuck for a solution.
From the codex above, adding to the URL "?post_type=jetpack-portfolio" does cause the child theme to correctly use archive-jetpack-portfolio.php, but should I need to be manually modifying every single URL to explicitly specify this? Should WordPress not automatically be detecting this, as it does for the single-jetpack-portfolio.php file? How can I solve this?
I have tried:
Resetting the permalinks in case it was related to that (changing the option in Settings and saving and back again)
Adding an archive.php file to the child in addition to archive-jetpack-portfolio.php (I initially didn't have an archive.php in the child, so it used the parent's archive.php)
Publishing a new Jetpack portfolio project and updating an existing page (I read somewhere that publishing something might trigger Wordpress to see the changes)
Thanks in advance for any help.
I had the same problem described by the OP. When I visited mydomain.com/portfolio it would use the custom archive template. When I tried to view a project type it defaulted to the regular archive.php. I'm wondering if OP was viewing a project type page without realizing it.
My solution was to create a taxonomy template file. After playing around with it I figured out that
taxonomy.php
taxonomy-jetpack-portfolio-type.php
taxonomy-jetpack-portfolio-type-{name-of-project-type}.php
all worked correctly, depending on how specific you wanted to get.
There's more info at the wordpress codex: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display
Hope this helps someone.
I will be working on this the next days.
You should try this in the child archive.php first lines:
<?php
if( is_post_type_archive('jetpack-portfolio') )
get_template_part('content', 'jetpack-portfolio');
elseif( is_tax('jetpack-portfolio-type') || is_tax('jetpack-portfolio-tag') )
get_template_part('archive', 'jetpack-portfolio');
else continue;
?>

How to make a wordpress theme woocommerce compatible?

How can I make a wordpress theme woocommerce compatible ? I want to make cart page, my account page, product loop page, product single page,checkout page design into my wordpress theme.
We Can make WordPress theme compatible with woocommerce here is how you can do that
There are two ways to resolve this:
1] Using woocommerce_content() -
This solution allows you to create a new template page within your theme that will be used for all WooCommerce taxonomy and post type displays.
To set up this template page, perform the following steps:
Duplicate page.php-
Duplicate your theme’s page.php file, and name it woocommerce.php. This file should be found like this: wp-content/themes/YOURTHEME/woocommerce.php.
Edit your page (woocommerce.php)-
Open up your newly created woocommerce.php in a text editor, or the editor of your choice.
Replace the loop-
In woocommerce.php, replace the Loop with woocommerce_content();
i.e., instead of if(have_posts)… endif; should be replaced by
woocommerce_content()
This will ensure that the WooCommerce templates are picked up for the product and taxonomy pages.
2] Using WooCommerce Hooks-
The hook method is more involved that using woocommerce_content, but is more flexible. This is similar to the method we use when creating our themes. By inserting a few lines in your theme’s functions.php file, First unhook the WooCommerce wrappers;
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
Then hook in your own functions to display the wrappers your theme requires:
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10); function my_theme_wrapper_start() {
echo '<section id="main">';} function my_theme_wrapper_end() {
echo '</section>';}
3] Declare WooCommerce support -
Now that you have made the changes, the final thing you have to do, is specify that your theme now supports WooCommerce. You need to add the following in functions.php of your theme.
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}
To make it more practical for you this is the video for you, which you
can follow too- How To Make WordPress Theme Compatible With WooCommerce Plugin
You need to install WooC and look at the all the style tags that come accross with it then you can style up the pages and add all of that to your style sheet.
Also you can use hooks but Im not 100% sure how you would check if WooC is active off the top of my head so that hooks in your code only come up when the plugin is active.

Changes on archive-product.php doesn't work

I'm trying to customize the standard woocommerce theme and so far that has worked well. I copied all template files from /plugins/woocommerce/templates to /mytheme/woocommerce and customized the files.
But when i'm change something in archive-product.php nothing happens? I even tried to customize this in the core files (/plugins/woocommerce/templates/archive-product.php) but i doesn't work.
I want to change the class of the h1 heading: <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>.
So i looked up all woocommerce template files, the class page-title occurs only in this one file (to prevent editing the wrong file).
Edit:
In detail, i want to customize the theme used in this path: http://example.com/product-category/mycategory
I tried all the above solutions, to no avail. No matter what I did, the archive-product.php was not being used at all. Instead, most woocommerce pages were using page.php from my theme.
The solution for me was to add theme support... Which, it's been a while since I used woocommerce, so I completely forgot about that. But after adding the following line to my functions.php file, archive-product.php is now being used (/mytheme/woocommerce/archive-product.php) and I can update it, as I should be able to.
add_theme_support('woocommerce');
Seems this is STILL an issue in Woocommerce. For anyone landing here, the following solution was working for me as of version 2.1.6.
Apparently the problem is due to the function woocommerce_content() outputting the wrong page for archive content.
I used the following to get around it:
replace woocommerce_content() in woocommerce.php with:
if ( is_singular( 'product' ) ) {
woocommerce_content();
}else{
//For ANY product archive.
//Product taxonomy, product search or /shop landing
woocommerce_get_template( 'archive-product.php' );
}
Credit: found the solution here
Here's how I fixed mine:
Delete woocommerce.php in your theme folder.
Copy TEMPLATE folder in woocommerce plugin dir, paste it in your THEME folder, and rename it to woocommerce.
Open the folder you just renamed, go to shop folder, and edit wrapper-start.php and wrapper-end.php.
If you use the woocommerce.php method you cannot customize archive-product. You must use the hooks method
http://docs.woothemes.com/document/third-party-custom-theme-compatibility/
Please note: when creating woocommerce.php in your theme’s folder, you won’t be able to override the woocommerce/archive-product.php custom template as woocommerce.php has the priority over archive-product.php. This is intended to prevent display issues.
For others searching here, doublecheck the path. It is for example not /mytheme/woocommerce/templates/archive-product.php but only /mytheme/woocommerce/archive-product.php. I didn't have to apply #Talk nerdy to me's patch or any other to make it work.
you need to edit the file "taxonomy-product_cat.php" and add a conditional is_product_category( 'mycategory' ).
open your theme folder and add a new subfolder named "woocommerce" to it.
copy the files "archive-product.php" and "taxonomy-product_cat.php" from /plugins/woocommerce/templates to the woocommerce subfolder in your theme.
rename "archive-product.php" to "archive-mycategory.php" (or whatever you like, this will be the template file to the category).
open "taxonomy-product_cat.php" and wrap the wc_get_template( 'archive-product.php' ); with:
if (is_product_category( 'mycategory' )){
wc_get_template( 'archive-mycategory.php' );
} else {
wc_get_template( 'archive-product.php' );
}

Resources