I would like to develop wooCommerce custom plugin. For the project requirement product title hyperlink will be disable.For this simple task i define the following script.Please give me the solution How to fix this.I want to use it WordPress custom plugin.
class Custom_WooCommerce_Disable_Title_Link {
public function __construct() {
add_action( 'init', array( $this, 'remove_title_hyperlink' ) );
}
public function remove_title_hyperlink() {
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
}
}
new Custom_WooCommerce_Disable_Title_Link();
I tried about above script. this is doesn’t work
Here’s an updated version of your code:
<?php
/*
Plugin Name: Disable WooCommerce Product Title Link
Description: A simple plugin to disable the hyperlink on WooCommerce product titles.
Version: 1.0
Author: Your Name
*/
class Custom_WooCommerce_Disable_Title_Link {
public function __construct() {
// Hook into WooCommerce initialization
add_action( 'woocommerce_init', array( $this, 'remove_title_hyperlink' ) );
}
public function remove_title_hyperlink() {
// Remove the link opening and closing actions
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
// Optionally, you can also modify the title output directly
add_filter( 'the_title', array( $this, 'disable_title_link' ), 10, 2 );
}
public function disable_title_link( $title, $id ) {
if ( 'product' === get_post_type( $id ) ) {
return $title; // Return the title without link
}
return $title; // Return unchanged title for other post types
}
}
new Custom_WooCommerce_Disable_Title_Link();
Explanation:
- Hook Adjustment: The
woocommerce_init
hook ensures that WooCommerce functions are available when your plugin runs.
- Title Filter: The additional filter for
the_title
is optional. It prevents the title from being wrapped in a link by returning the title as-is when the post type is ‘product’.