skip to Main Content

I am trying to modify a part of code inside: themes / flatsome / inc / shortcodes / ux_products.php

I want to change this:

<a href="<?php echo get_the_permalink(); ?>">

For this

<a href="<?php echo get_the_permalink(); ?>" aria-label="<?php echo get_the_title(); ?>">

I have the same path and file inside the folder of my child theme but I can’t annul the main theme file.

I realized that there is another file that refers to the file I want to cancel: themes / flatsome / inc / init.php:

if (is_woocommerce_activated()) {
  require get_template_directory() . '/inc/shortcodes/ux_products.php'; //File to annul
  require get_template_directory() . '/inc/shortcodes/ux_products_list.php';
  require get_template_directory() . '/inc/shortcodes/product_flip.php';
  require get_template_directory() . '/inc/shortcodes/product_categories.php';
  if(get_theme_mod('product_layout') == 'custom') {
    require get_template_directory() . '/inc/shortcodes/custom-product.php';
  }
}

How can I do this without having to modify the main theme (this works but it is not correct).

2

Answers


  1. Can you try adding this to your child theme’s functions.php:

    add_filter( 'template_directory', 'search_child_template_directory' );
    function search_child_template_directory( $template_dir ) {
        if (is_woocommerce_activated()) {
            return get_theme_file_path();
        }
        return $template_dir;
    }
    
    Login or Signup to reply.
  2. First you need copy the shortcode you need modify to your child theme.

    'your_child_theme/inc/shortcodes/ux_products.php'
    

    Then you need to modify the child shortcode, function name :

    function ux_products($atts, $content = null, $tag)
    

    to

    function child_ux_products($atts, $content = null, $tag)
    

    Modify what you need:

    <a href="<?php echo get_the_permalink(); ?>">
    

    to

    <a href="<?php echo get_the_permalink(); ?>" aria-label="<?php echo get_the_title(); ?>">
    

    And at the end of your child shortcode file, override all (or only needed) declared shortcodes, with your own function :

    add_shortcode("ux_products", "ux_products");
    

    to

    add_shortcode("ux_products", "child_ux_products");
    

    To override all shortcodes :

    add_shortcode("ux_bestseller_products", "child_ux_products");
    add_shortcode("ux_featured_products", "child_ux_products");
    add_shortcode("ux_sale_products", "child_ux_products");
    add_shortcode("ux_latest_products", "child_ux_products");
    add_shortcode("ux_custom_products", "child_ux_products");
    add_shortcode("product_lookbook", "child_ux_products");
    add_shortcode("products_pinterest_style", "child_ux_products");
    add_shortcode("ux_products", "child_ux_products");
    

    Finally in your function.php or ‘/inc/init.php’, you need require overrided shortcodes when wp_loaded :

    function override_shortcodes(){
      if(is_woocommerce_activated()){
        require get_stylesheet_directory() . '/inc/shortcodes/ux_products.php';
      }
    }
    add_action('wp_loaded', 'override_shortcodes', 10);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search