Any way how to change plugins action to a custom one?

Hi, my client's using The Events Calendar and I need to modify small part of a plugin actions (originally in some function part) which will deal with multiple prices added in "cost" option. That is pretty easy to do, but I would like to solve it more elegantly, not "hard coded" to loose it after plugin update or so. Any way how to do that? Thanks a lot in advance for your help

  • Jude

    Hi again @Karolina

    That is pretty easy to do, but I would like to solve it more elegantly, not "hard coded" to loose it after plugin update or so. Any way how to do that?

    Once again congrats on asking such a question. WP needs more developers who think on these same lines.

    You have 2 options

    1) Site specific plugin ( I told you earlier as well)

    2) Mu Plugin

    Both can serve you well depending on how much control your client/end user should have

    Jude

  • Karolina

    Hi again @Jude thanks for your answer (I've rather divided both questions to two parts than keeping it in one because some part would be not answered for sure). But actually that's not what I wanted, I still need the possibility to update the plugin - we are thinking about buying the PRO version... I have following code somewhere withing the plugin files:

    /**
    	 * Get an event's cost
    	 *
    	 * @param null|int $postId             (optional)
    	 * @param bool     $withCurrencySymbol Include the currency symbol
    	 *
    	 * @return string Cost of the event.
    	 */
    	function tribe_get_cost( $postId = null, $withCurrencySymbol = false ) {
    		$tribe_ecp = TribeEvents::instance();
    		$postId    = TribeEvents::postIdHelper( $postId );
    
    		$cost = tribe_get_event_meta( $postId, '_EventCost', true );
    
    		if ( $cost === '' ) {
    			$cost = '';
    		} elseif ( $cost === '0' ) {
    			$cost = __( "Free", 'tribe-events-calendar' );
    		} else {
    			$cost = esc_html( $cost );
                  // MY CODE COMES HERE
    		}
    
    		// check if the currency symbol is desired, and it's just a number in the field
    		// be sure to account for european formats in decimals, and thousands separators
    		if ( $withCurrencySymbol && is_numeric( str_replace( array( ',', '.' ), '', $cost ) ) ) {
    			$cost = tribe_format_currency( $cost );
    		}
    
    		return apply_filters( 'tribe_get_cost', $cost, $postId, $withCurrencySymbol );
    	}

    and I've added following part to the "MY CODE COMES HERE"

    if (strpos($cost, '/') !== FALSE){
      $cs = tribe_get_event_meta( $postId, '_EventCurrencySymbol', true );
      $cost = str_replace("/",$cs.' / ',$cost).$cs;
    }

    So my question is if I can redefine somewhere (in site specific plugin) the function tribe_get_cost?

  • Jude

    Hi there Karolina ..

    Hope the weekend was good !!

    You don't need to redefine the function if this is what you want.

    Just use this in your site specific plugin.

    add_filter( 'tribe_get_cost' , 'custom_cost_filter' , 99 , 3) ; 
    
    function custom_cost_filter ( $cost, $postId, $withCurrencySymbol  ) {
    if (strpos($cost, '/') !== FALSE) {
      $cs = tribe_get_event_meta( $postId, '_EventCurrencySymbol', true );
      $cost = str_replace("/",$cs.' / ',$cost).$cs;
    }
    
    return $cost ;
    }

    Hope that helps

    Jude

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.