Get custom token value in PHP

Hello Guys!

I’m new here, started with MainWP just 2 weeks ago :slight_smile: Could you help me please, how can I get the value of a custom token in PHP?

I created a custom Pro Report template, and I can “echo” the content of a custom token using the [my_custom_token] syntax, but how can I get the value and pass it to a variable? I cannot figure it out.

I’d like to write some if statements in the custom report template, and my conditions based on the custom token string’s value and length.

Thank you

Hey @senepro

Welcome to the MainWP community.

I’ll check with the development team and update you via this thread as soon as I can.

1 Like

@senepro

The values of tokens are not available when the template is loaded. It will be available in the $data array in the hook mainwp_pro_reports_addition_custom_tokens like in example below.

So all statements and conditions will be available in the hook and will be able to show the custom value by using custom tokens in the template, like the token [custom.update.total] in the example.

add_filter( 'mainwp_pro_reports_addition_custom_tokens', 'mycustom_mainwp_pro_reports_addition_custom_tokens', 10, 3 );
function mycustom_mainwp_pro_reports_addition_custom_tokens( $tokens, $site_id, $data ) {
    if(is_array($data) && isset($data[$site_id])){
         $total = 0;
         $total += isset($data[$site_id]['other_tokens_data']['body']['[plugin.updated.count]']) ? intval( $data[$site_id]['other_tokens_data']['body']['[plugin.updated.count]'] ) : 0;
         $total += isset($data[$site_id]['other_tokens_data']['body']['[theme.updated.count]']) ? intval( $data[$site_id]['other_tokens_data']['body']['[theme.updated.count]'] ) : 0;
         $total += isset($data[$site_id]['other_tokens_data']['body']['[wordpress.updated.count]']) ? intval( $data[$site_id]['other_tokens_data']['body']['[wordpress.updated.count]'] ) : 0;
         
         $tokens['[custom.update.total]'] = $total;
    }
    
    return $tokens;
}

Let me know if that works.

Hi Bojan!

Thank you, I’ll check it, and give a feedback.

1 Like

Hi Bojan!

This hook not worked for me, but the mainwp_pro_reports_custom_tokens hook was fine, I can made the customizations for my custom token.

Thank you

1 Like