How to use filter in this situation, can not modify the structure using filter

So you’re trying to call the hide_empty_fields() and maybe_add_disclaimer_and_courtesy() methods in the Register_Impress_Shortcodes class, from within a global function, and it’s not impossible to call class methods from within such global functions, but you cannot use $this in your function because $this is only available from within a class method which is called from within an object context. More details in the PHP manual

As for the proper way to call those two class methods from within your function, I’m not sure (unless maybe, if you provide the full class code and the information on how/when/where it’s being instantiated), but see below for some options that you can try, without modifying the core plugin files:

  • If those methods are public and there’s an instance of the class in the global scope, e.g. global $Register_Impress_Shortcodes; $Register_Impress_Shortcodes = new Register_Impress_Shortcodes();, then just add global $Register_Impress_Shortcodes; to your function (at the top), then replace those $this with $Register_Impress_Shortcodes.

  • If such global instance is not available, but those methods are public and there is a “static instance getter” like the DBConn::getConn() here, then in your function, just replace the $this with Register_Impress_Shortcodes::instance() (or replace instance with the correct method name).

    If such method doesn’t exist, but/or the plugin in question provides a global function like register_impress_shortcodes_instance(), which returns the class instance, then replace those $this with register_impress_shortcodes_instance().

  • If those methods are not public or there’s no global class instance, and neither an instance getter method or function, and that the Register_Impress_Shortcodes class can be extended, and also that those methods are not private, then you can do something like this:

    <?php
    // Extend the class.
    class My_Register_Impress_Shortcodes extends Register_Impress_Shortcodes {
        public function __construct() {
            // Add your filter.
            add_filter( 'impress_showcase_property_html', array( $this, 'impress_showcase_property_html_callback' ), 10, 7 );
        }
    
        // Now this is your filter callback which is a method in this class. So
        // you can now use $this and access the methods hide_empty_fields() and
        // maybe_add_disclaimer_and_courtesy() defined in the parent class.
        public function impress_showcase_property_html_callback( $value, $prop, $instance, $url, $prop_image_url, $column_class, $target ) {
            $value = sprintf(
                '... your code here',
                price_selector( $prop ),
                $prop['propStatus'],
                $url,
                $prop_image_url,
                htmlspecialchars( $prop['remarksConcat'] ),
                $prop['streetNumber'],
                $prop['streetDirection'],
                $prop['streetName'],
                $prop['unitNumber'],
                $prop['cityName'],
                $prop['state'],
                parent::hide_empty_fields( 'beds', 'Beds', ( empty( $prop['bedrooms'] ) ? '' : '<span class="numbers">'.$prop['bedrooms'].'</span>' ) ),
                parent::hide_empty_fields( 'baths', 'Baths', ( empty( $prop['totalBaths'] ) ? '' : '<span class="numbers">'.$prop['totalBaths'].'</span>' ) ),
                parent::hide_empty_fields( 'sqft', 'SqFt', ( empty( $prop['sqFt'] ) ? '' : '<span class="numbers">'.$prop['sqFt'].'</span>' ) ),
                parent::hide_empty_fields( 'acres', 'Acres', ( empty( $prop['acres'] ) ? '' : '<span class="numbers">'.$prop['acres'].'</span>' ) ),
                parent::maybe_add_disclaimer_and_courtesy( $prop ),
                $column_class,
                $target
            );
    
            return $value;
        }
    }
    
    function init_my_register_impress_shortcodes() {
        new My_Register_Impress_Shortcodes();
    }
    add_action( 'init', 'init_my_register_impress_shortcodes' );
    

    Note: Be sure to remove the add_filter('impress_showcase_property_html', 'impress_showcase_property_html_callback', 10, 8); in your code.

  • If the class cannot be extended or you’re having trouble calling those methods from within your class, then I guess you would want to unregister/remove the shortcode added by that class/plugin, then copy that very class, rename it and re-add the shortcode you have removed.

    That way, you wouldn’t need to hook on impress_showcase_property_html and simply modify the shortcode output in the property_showcase_shortcode() method…

    But the problem with this (and the previous option), is that when the plugin is updated, you would need to revise your class/methods so that it’s in sync with the latest code in the updated plugin.

Another option that you can try, is if possible, create 2 global functions (e.g. my_hide_empty_fields() and my_maybe_add_disclaimer_and_courtesy()) which does what those methods do, respectively, i.e. basically copy the code in those methods… and edit it accordingly, e.g. remove any $this calls. Then just use your functions from within your impress_showcase_property_html_callback() function.