最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - How to direct WordPress to load custom template for custom post type

programmeradmin4浏览0评论

I create a plugin that create a new product-type called exploded. For this product type I want create a whole new product page if the product is equal on my product-type.

I tried the {product-type}-add-to-cart filter but this is only for the button section.

Can I replace the content-single-product file is the product-type is equal?

I create a plugin that create a new product-type called exploded. For this product type I want create a whole new product page if the product is equal on my product-type.

I tried the {product-type}-add-to-cart filter but this is only for the button section.

Can I replace the content-single-product file is the product-type is equal?

Share Improve this question edited Apr 14, 2020 at 19:03 西門 正 Code Guy - JingCodeGuy 1,5911 gold badge13 silver badges15 bronze badges asked Apr 9, 2020 at 23:12 JopJop 1279 bronze badges 3
  • 1 I think this is a similar question that I have answered before. I have made an example in this question for creating custom product type template. Please refer to Woocommerce frontend template custom product type – 西門 正 Code Guy - JingCodeGuy Commented Apr 9, 2020 at 23:44
  • But this is only the add_to_cart section I want if is product-type is.... load my own template for the whole page. – Jop Commented Apr 10, 2020 at 19:33
  • I have added the suggested method and I recommend you to change the title to how to add product template for custom product type for better understanding to different audience. – 西門 正 Code Guy - JingCodeGuy Commented Apr 11, 2020 at 3:44
Add a comment  | 

1 Answer 1

Reset to default 0

For future audience sake, this notice is added.

The following answer assumed anyone have the following knowledge

  • php and basic debug knowledge: how to var_dump() or print_r()
  • WordPress Core Filter and Action known as Hooks, when and how to use them correctly
  • feel comfortable modifying theme file functions.php
  • for plugin-related development, please refer to Plugin Handbook
  • The examples are working for both putting in functions.php or in your plugin's file. Additional logic for your own plugin required to be added by yourself since each conditions are different.

Method 1 (inside theme)

You may need to create your own single-product.php and place in your-theme/woocommerce/single-product.php;

  • prepare 2 more files in the same folder, content-single-exploded.php and content-single-other.php and place in the same folder of single-product.php

If you want to control the logic in template file single-product.php Inside your template file, you may do the following, this will help to choose different template files for inclusion.

Do the following in your single-product.php, here is a simplified version to illustrate the concept.

// ...
<?php

if( $product->get_type() === 'exploded' ) {
wc_get_template_part( 'content', 'single-exploded' );
} else {
wc_get_template_part( 'content', 'single-other' );
}
// ...
?>

Method 2 (for both theme or plugin)

If in case you want to control template in plugin, you can use the filter template_include and do the checking you need.

You may put the following code into theme functions.php or your plugin and try to load a single product page.

* It is NOT necessary to add the following code inside any action.

// you can use this filter to control the template path and use the plugin template
add_filter( 'template_include', 'q363767_tweak_template' );
function q363767_tweak_template( $template ) {
    if( preg_match( '/single-product.php/', $template ) ) {
        // do the tweaking and update the path to single-product-explode.php with plugin directory
        // you may do var_dump here to see what is $template eg.
        // var_dump( $template );
        // $template = 'your-template-path';
    }

    return $template;
}

As the answer is for template file, this way will not work as expected:

function your_function() {
// not necessary to put the code here 
// XXX
// copy the above code here and run -> will NOT work
// XXX
}
add_action('woocommerce_before_single_product','your_function'); 

Because woocommerce_before_single_product is not for controlling the whole template and it is not necessary.

发布评论

评论列表(0)

  1. 暂无评论