WordPress - Ovveride plugin template in theme - php

I have plugin that create new post type. Also plugin set single template for it's single page.
add_filter( 'single_template', array( &$this, 'register_ipa_product_post_type_single_template' ) );
function register_ipa_product_post_type_single_template( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = IPA_PRODUCT_POST_TYPE_TEMPLATE_FOLDER . 'single-product.php';
}
return $single_template;
}
How i can override single-product.php in my theme.
I don't found any solutions for my question at this site.

just filter it a little later than the current function (ps if doing this within a class you need to reference it using array(&$this, 'function'). I left it out as i assume you are using the functions.php or function override....etc
add_filter( 'single_template', 'register_ipa_product_post_type_single_template', 100 );
function change_temp( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = 'path to your template file';
}
return $single_template;
};

Related

I can't purchase my custom product type in WordPress

I'm developing a plugin for custom product type. Here's my class that is being registered on plugins_loaded hook:
class WC_Product_Subscription extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'subscription';
$this->purchasable = true;
$this->downloadable = false;
$this->virtual = true;
$this->sold_individually = true;
$this->manage_stock = false;
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
}
public function is_purchasable() {
return true;
}
}
The problem is that I cannot see "Add to Cart" button on the product page which means my product cannot be purchased. I tried adding
public function add_to_cart_url() {
return apply_filters( 'woocommerce_product_add_to_cart_url', get_permalink( $this->get_id() ), $this );
}
public function add_to_cart_text() {
$text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add to cart', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
}
to the class but without success. I'm stuck.
It appears there are some missing steps to make your custom product type work.
Try the steps below:
#1. Make sure that your plugin is active.
#2. Make sure the product is in stock and has a price set. WooCommerce checks both of these conditions before displaying the Add to Cart button.
#3. Check if the custom product type is registered correctly. Use the following code to check:
add_action( 'init', 'check_registered_product_types' );
function check_registered_product_types() {
$product_types = wc_get_product_types();
var_dump( $product_types );
}
#4. Make sure that the WooCommerce product type is supported. Use the following code to check:
add_filter( 'product_type_selector', 'custom_product_type_selector' );
function custom_product_type_selector( $product_types ) {
var_dump( $product_types );
return $product_types;
}
#5. Make sure that the product class is correctly loaded. Use the following code to check:
add_action( 'plugins_loaded', 'check_product_class' );
function check_product_class() {
$product_class = 'WC_Product_Subscription';
var_dump( class_exists( $product_class ) );
}
#6. Ensure you have a product template for your custom product type in your theme's WooCommerce folder (e.g. single-product-subscription.php).
#7. If everything else seems to be working, you might have to override the WooCommerce templates to display the Add to Cart button.
Edit:
You can create a template in your plugin directory by using the following code:
add_filter( 'woocommerce_locate_template', 'wc_subscription_template', 10, 3 );
function wc_subscription_template( $template, $template_name, $template_path ) {
if ( 'single-product-subscription.php' === $template_name ) {
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/single-product-subscription.php';
}
return $template;
}
This will tell WooCommerce to use your custom template in the templates folder within your plugin directory. Make sure you put the code in a file that is included in your plugin, so it will run when the plugin is activated.

Can I load a post single page from my plugin directory?

As a wp plugin developer, I want to load a post single page from my plugin directory. Not a custom post type I want to load from the default wp post type.function.php template.php
You can use this hook to check if this is a single page and the post type is 'post'
add_action( 'single_template', [$this, 'event_single_page'] );
function event_single_page( $single ) {
global $post;
if ( $post->post_type == 'post' && is_singular( 'post' ) ) {
$plugin_template = 'post-single-page.php';
if ( file_exists( $plugin_template ) ) {
$single = $plugin_template ;
}
}
return $single;
}

How to add multiple categories in function WordPress

Wordpress - functions.php
I'm using this function to automatically add custom structure in url for posts in a specific categorie.
But now I would like to know how I could add multiple categories without having to copy this function over and over.
Other solution could be to have this function work for the parent post category and all child categories.
// url structure
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "b" ) {
$cat_name = strtolower($category[0]->cat_name);
$permalink = trailingslashit( home_url('/questions/' . $post->post_name .'/' ) );
}
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'b/([^/]+)(?:/([0-9]+))?/?$',
'index.php?category_name=b&name=$matches[1]&page=$matches[2]',
'top' // The rule position; either 'top' or 'bottom' (default).
);
}

Woocommerce conditional is_product() from functions.php

I want to check if is single product page, but this has no effect on single product page:
if (! is_admin() && is_product() ) {
var_dump('is product'); // this has no effect in single product page
}
Is there a limitation to use is_product() in functions.php? How can I achieve this?
EDIT:
In order to avoid var_dump issues, this is the final code I'm trying. It try to add product type (simple/variable) to body class:
add_action( 'woocommerce_after_single_product', function () {
if (! is_admin() && is_product() ) {
add_filter( 'body_class', function( $classes ) {
global $post;
$product = wc_get_product( $post->ID );
$tipo = $product->get_type();
return array_merge( $classes, array( $tipo ) );
});
}
}, 100 );
You can not use this function directly without a hook since your functions.php file is included before every other file - mostly. To make this thing work you need to work with hooks every time like this:
add_action( 'template_redirect', 'template_redirect_action' );
function template_redirect_action() {
if ( ! is_admin() && is_product() ) {
add_filter( 'body_class', function ( $classes ) {
global $post;
$product = wc_get_product( $post->ID );
$tipo = $product->get_type();
return array_merge( $classes, array( $tipo ) );
} );
}
}
Visit a product page and check your body classes.
By using the template_redirect hook you can be sure it's executed on every page. Otherwise your check would not make any sense when using a product hook which only gets executed on product pages.
Tested an works.

Wordpress: Include new template for a page using query var with rewrite endpoint

I am trying to load a new page template when a query var is appended at the end of my page url:
Original url: example.com/testpage/
with variable added to the end: example.com/testpage/amp
Then it would load up a custom php template.
This seems like a straight forward operation, but I cannot get it to work.
The url loads with the /amp variable at the end, but the template does not load. If I remove the condition "get_query_var('amp')" then it loads up the template no problem. What am I missing? Thanks :)
Here is my working code:
add_filter( 'query_vars', 'register_query_var' );
function register_query_var( $vars ) {
$vars[] = 'amp';
return $vars;
}
add_rewrite_endpoint( 'amp', EP_PAGES );
add_filter( 'template_include', 'use_amp_template', 99 );
function use_amp_template( $template ) {
global $wp_query;
if ( get_query_var( 'amp' ) && is_page() ) {
$new_template = locate_template( array( 'amptemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
Found a good fix on my own. Here is the code if it would help anyone.
Adding 'amp' after a page or post will load up different templates for amp versions of the page.
example.com/samplepage/amp
or
example.com/samplepost/amp
add_filter( 'query_vars', 'register_query_var' );
function register_query_var( $vars ) {
$vars[] = 'amp';
return $vars;
}
add_rewrite_endpoint( 'amp', EP_PAGES | EP_PERMALINK );
add_filter( 'template_include', 'use_amp_template', 99 );
function use_amp_template( $template ) {
global $wp_query;
if(isset( $wp_query->query['amp'] ) && is_page()){
$new_template = locate_template( array( 'amppagetemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
if(isset( $wp_query->query['amp'] ) && is_single()){
$new_template = locate_template( array( 'ampposttemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}

Categories