Support

Home Forums Event Espresso Premium Replace Text on Event Page Script Not Working

Replace Text on Event Page Script Not Working

Posted: June 4, 2018 at 11:49 am


mdeyerinkc

June 4, 2018 at 11:49 am

Hi,

I’m using Tony’s script from : https://gist.github.com/Pebblo/a68e8a151bd1df0288c45ddff585c300 to try and change some of the default EE text, but it isn’t working as I would have expected. It changed the text on the simple event list register button from “Sold Out” to “Class Full”, but on a “members only” event page it is not replacing “The [ticket] is available to members only.” with “The [ticket] is restricted.”

Will this script not work for this text (no-tkt-slctr-ticket-content-dv)?

Ideally, I’d like the restricted ticket text to say “This course is restricted to [capability],” (since I imagine that variable can be pulled), and then I’d use the filter/replace script to switch that text out as well; e.g. “level_4” => “Level 4 employees.”


Josh

  • Support Staff

June 4, 2018 at 12:22 pm

The script will work if you have an exact match of the original string. Do you have this set as the original string?

The %1$s%2$s%3$s%4$s is available to members only. %5$s


mdeyerinkc

June 4, 2018 at 12:38 pm

I did not, but I tried it and it didn’t change anything. Below is the germane part…

    // This is an array of original strings
    // and what they should be replaced with
    $strings = array(
        'On Sale' => 'Open Now',
		'Sold Out' => 'Class Full',
        'The %1$s%2$s%3$s%4$s is available to members only. %5$s' => 'restricted',
        // Add some more strings here
    );


mdeyerinkc

June 4, 2018 at 1:13 pm

I copied and pasted that portion of text from Tony’s original post, and it’s working now. Is there a hook (?) for the restricted capability, e.g. ‘The %1$s%2$s%3$s%4$s is available to members only. %5$s’ => ‘The %1$s%2$s%3$s%4$s is restricted to [level_4]’,


Josh

  • Support Staff

June 4, 2018 at 1:31 pm

There is a filter hook that makes the capability available, but it works differently than how you’re using the gettext filter. Here’s an example of how to use the other filter hook to output the ticket capability at the end of the message:

function add_cap_to_access_message( 
    $original_text, 
    $ticket, 
    $ticket_price, 
    $ticket_status 
) {
    $cap_required = '';
    $cap_required = $ticket->get_extra_meta('ee_ticket_cap_required', true);
    return $original_text . ' ' . $cap_required;  
}

add_filter(
    'FHEE__EED_WP_Users_Ticket_Selector__maybe_restrict_ticket_option_by_cap__no_access_msg', 
    'add_cap_to_access_message', 
    10, 
    4
);


mdeyerinkc

June 4, 2018 at 2:02 pm

That works – but it still displays the format restricted version of the capability (e.g. level_4 vs. Level 4). I tried adding ‘level_4’ => ‘Level 4’ to the other script, but that didn’t work. That’s okay, fixing the main string replace was my primary goal. Thanks Josh!


Josh

  • Support Staff

June 4, 2018 at 2:12 pm

You can reformat the way the capability is displayed by adding a bit more PHP code. For example, make the changes to the above code:

$cap_required = $ticket->get_extra_meta('ee_ticket_cap_required', true);
if($cap_required == 'level_4') {
  $cap_required = 'Level 4';
} // or add a switch statement if you have many different required caps
return $original_text . ' ' . $cap_required;


mdeyerinkc

June 4, 2018 at 2:53 pm

So this is how I interpreted that change:

function add_cap_to_access_message( 
    $original_text, 
    $ticket, 
    $ticket_price, 
    $ticket_status 
) {
	$cap_required = '';
	$cap_required = $ticket->get_extra_meta('ee_ticket_cap_required', true);
	if($cap_required == 'metro_transit') {
  	$cap_required = 'Metro Transit';
	} // or add a switch statement if you have many different required caps
	return $original_text . ' ' . $cap_required;

add_filter(
    'FHEE__EED_WP_Users_Ticket_Selector__maybe_restrict_ticket_option_by_cap__no_access_msg', 
    'add_cap_to_access_message', 
    10, 
    4
);

But it didn’t do anything, so I missed something. Also, I had no idea what a switch statement was, but I looked it up on w3schools and took a stab:

function add_cap_to_access_message( 
    $original_text, 
    $ticket, 
    $ticket_price, 
    $ticket_status 
) {
	$cap_required = '';
	$cap_required = $ticket->get_extra_meta('ee_ticket_cap_required', true);
	if($cap_required == 'metro_transit') {
  	$cap_required = 'Metro Transit';

$cap_required = "metro_transit";

switch ($cap_required) {
    case "metro_transit":
        echo "Metro Transit";
        break;
    case "public_health":
        echo "Public Health";
        break;
    case "law_enforcement":
        echo "Law Enforcement";
        break;
    default:
        echo "designated personnel";
}
	}
	return $original_text . ' ' . $cap_required;

add_filter(
    'FHEE__EED_WP_Users_Ticket_Selector__maybe_restrict_ticket_option_by_cap__no_access_msg', 
    'add_cap_to_access_message', 
    10, 
    4
);

Am I close?


Josh

  • Support Staff

June 4, 2018 at 3:30 pm

That’s a good crack at it. One important thing to remember is you generally do not echo when the filter function is returning some text.

Before we tackle the switch statement, is the WordPress capability that’s required for the ticket level_4 or is it metro_transit? Put another way, what’s displayed when the filter function returns only the following:

$cap_required = $ticket->get_extra_meta('ee_ticket_cap_required', true);
    return $original_text . ' ' . $cap_required;


mdeyerinkc

June 5, 2018 at 8:55 am

metro_transit is the capability (to be displayed as Metro Transit). I used “level_4” just as a generic placeholder.


Josh

  • Support Staff

June 5, 2018 at 9:20 am

Maybe what you could do is add a little temporary debugging code to your custom code like this:

$cap_required = $ticket->get_extra_meta('ee_ticket_cap_required', true);
print_r($cap_required);

then, you can copy and paste the debug output and use that in your conditional statement.


mdeyerinkc

June 5, 2018 at 9:49 am

I think I misinterpreted your previous question what is currently displaying. Here’s the full code I have active now, to include the debugging line you just gave me. (Also worth mentioning, I really don’t know php; I’m just marginally decent at adapting existing code for my purposes).

function tw_custom_filter_gettext( $translated, $original, $domain ) {
 
    // This is an array of original strings
    // and what they should be replaced with
    $strings = array(
       	'Sold Out' => 'Class Full',
		'The %1$s%2$s%3$s%4$s  is available to members only. %5$s' => 'The %1$s%2$s%3$s%4$s is restricted to designated personnel.',
        // Add some more strings here
    );
 
    // See if the current string is in the $strings array
    // If so, replace its translation
    if ( isset( $strings[$original] ) ) {
        // This accomplishes the same thing as __()
        // but without running it through the filter again
        $translations = get_translations_for_domain( $domain );
        $translated = $translations->translate( $strings[$original] );
    }
 
    return $translated;
}
 
add_filter( 'gettext', 'tw_custom_filter_gettext', 10, 3 );

function add_cap_to_access_message( 
    $original_text, 
    $ticket, 
    $ticket_price, 
    $ticket_status 
) {
	
	$cap_required = '';
	$cap_required = $ticket->get_extra_meta('ee_ticket_cap_required', true);
print_r($cap_required);
	} // or add a switch statement if you have many different required caps
	return $original_text . ' ' . $cap_required;

add_filter(
    'FHEE__EED_WP_Users_Ticket_Selector__maybe_restrict_ticket_option_by_cap__no_access_msg', 
    'add_cap_to_access_message', 
    10, 
    4
);


Josh

  • Support Staff

June 5, 2018 at 9:51 am

Is anything being output onto the screen by print_r($cap_required);?


mdeyerinkc

June 5, 2018 at 10:09 am

Nothing different than before. It says “The [ticket name] is restricted to designated personnel.”


Josh

  • Support Staff

June 5, 2018 at 10:54 am

OK on a closer look there’s a missing closing bracket in one place and an extra closing bracket in another. In any case, here’s an altered version of with some suggested fixes:

https://gist.github.com/joshfeck/bb977b3cc27a5ffa619761430a65ff14


mdeyerinkc

June 5, 2018 at 11:07 am

That’s a perfect fix. The underscore replace is a much more svelt solution than switch. Thanks for your help Josh!


mdeyerinkc

June 5, 2018 at 11:29 am

…for the benefit of others that might need a similar solution in the future, I added the second line, which capitalizes the hooked capability [placed after $cap_required_display_name = str_replace( $search, $replace, $cap_required);]

$cap_required_display_name = ucwords($cap_required_display_name);

The support post ‘Replace Text on Event Page Script Not Working’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso