Jump to content

Hooks and-or email merge fields for custom urls


turbosatan

Recommended Posts

i am using a whmcs wordpress integration. all of the urls are different than normal

 

the whmcs installation is at /client/ but the integration is at /client-area/

 

I would like to be able to modify the urls sent out in emails to reflect this as they currently send people to the /client folder.

 

I initially tried to add the url into the main config file as i know this works for some functions but apparently this doesnt work for fields available to emails.

 

after trying various other options i am down to trying to use a hook to amke the custom variables available within the email templates and i have two questions pertaining to this.

 

Firstly i cant even seem to access a basic variable returned by the hook function.

 

if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function hook_set_custom_urls() {


$customurl = "My Custom Var2";
return $customurl;

}

add_hook("EmailPreSend",1,"hook_set_custom_urls");

 

i then included {$customurl} into the email template and that has done nothing with it.

 

could you explain what i am doign incorrectly here as im at my wits end

 

Secondly id like to just have new merge fields available to use. there are going to be about 20 or so to deal with all the fields i need to have in the emails.

 

is there a simple way to just define new merge fields for use in emails without having to use custom fields for every client as this is a global setting.

 

configemailtemplates.php is ion cubed so i cant see anything in there to figure this out myself

Link to comment
Share on other sites

Hi,

 

coming at this from another angle, and apologies if this in one of the options you have already tried, but did you try modifying the {$whmcs_link} merge field in the email template(s) to change your URL ?

 

because the email templates use Smarty, you can change the value of {$whmcs_link} using the Smarty replace command...

 

{$whmcs_link|replace:'client/':'client-area/'}

other email templates might use different link merge fields, but the above principle should work on all of them.

 

and to answer your question about the hook - did you read the documentation page on EmailPreSend Hook - http://docs.whmcs.com/Hooks:EmailPreSend ?

 

that has the basis of some working code... which i've simplified and posted below - if you add it to your hooks folder, it will pass the two merge fields to your templates.

 

<?php

if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function hook_emailvars($vars) {

$merge_fields = array();
$merge_fields['my_custom_var'] = "My Custom Var";
$merge_fields['my_custom_var2'] = "My Custom Var2";
return $merge_fields;

}

add_hook("EmailPreSend",1,"hook_emailvars");

?>

Edited by brian!
Link to comment
Share on other sites

Hi,

 

coming at this from another angle, and apologies if this in one of the options you have already tried, but did you try modifying the {$whmcs_link} merge field in the email template(s) to change your URL ?

 

because the email templates use Smarty, you can change the value of {$whmcs_link} using the Smarty replace command...

 

{$whmcs_link|replace:'client/':'client-area/'}

other email templates might use different link merge fields, but the above principle should work on all of them.

 

 

Hi. yeah i did think about doing a general replacement with both smarty and also looked at htaccess to just redirect the urls

 

one issue i have is that i need to replace other parts of the url also. so the url of /client/knowledgebase.php needs to actually be /client-area/?ccce=knowledge

 

which is a bit of a pain.

 

Really i just want to have some general tutorial on how to make a new merge field available to emails as the security around it while good isnt very clear on how to make any changes.

 

 

and to answer your question about the hook - did you read the documentation page on EmailPreSend Hook - http://docs.whmcs.com/Hooks:EmailPreSend ?

 

 

Yes thats what my example is based on. as i understand it my code should work. i am hooked into the EmailPreSend hook position to amke the variable available and i then insert this ref into the code so it should read it and put that forward.

 

 

that has the basis of some working code... which i've simplified and posted below - if you add it to your hooks folder, it will pass the two merge fields to your templates.

 

<?php

if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function hook_emailvars($vars) {

$merge_fields = array();
$merge_fields['my_custom_var'] = "My Custom Var";
$merge_fields['my_custom_var2'] = "My Custom Var2";
return $merge_fields;

}

add_hook("EmailPreSend",1,"hook_emailvars");

?>

 

Yes this is the same code i used to make my Hook. i just made a change to the Vars passed across as i didnt want any to create my return and i am reasonably sure i named everythign correctly but as i said that didnt work. Could do with an actual working copy of some code somewhere that doesnt have a non standard usage.

 

for example the code example in the docs assumes there will be some vars passed across and then you can include the forum signup code. and while this is quite likely a good solution to have for someone with a forum it doesnt actually provide anything that affects the page in question via the hook.

 

Also more to the point. adding a merge field shouldnt really be this difficult. why cant i ust add my merge field to a db table somewhere?

Link to comment
Share on other sites

Hi. yeah i did think about doing a general replacement with both smarty and also looked at htaccess to just redirect the urls

 

one issue i have is that i need to replace other parts of the url also. so the url of /client/knowledgebase.php needs to actually be /client-area/?ccce=knowledge

 

which is a bit of a pain.

if we're talking about WHMCS Bridge, then the Pro version of the plugin deals with that redirection issue...

 

but thinking purely of the email links, then you should be able to do them using Smarty replace.

 

Really i just want to have some general tutorial on how to make a new merge field available to emails as the security around it while good isnt very clear on how to make any changes.

 

Yes thats what my example is based on. as i understand it my code should work. i am hooked into the EmailPreSend hook position to amke the variable available and i then insert this ref into the code so it should read it and put that forward.

 

Could do with an actual working copy of some code somewhere that doesnt have a non standard usage.

 

Also more to the point. adding a merge field shouldnt really be this difficult. why cant i ust add my merge field to a db table somewhere?

the code I posted above makes the two additional merge fields available to the email templates - I did test it before posting! :idea:

 

in the email templates, you can just call them as you would any other merge field...

 

{$my_custom_var}
{$my_custom_var2}

I tried it on the invoice payment reminder email template, but I assume it would work in any of them...

Link to comment
Share on other sites

if we're talking about WHMCS Bridge, then the Pro version of the plugin deals with that redirection issue...

 

ill look at that now assuming that it does deal with the redirection itself...

 

 

but thinking purely of the email links, then you should be able to do them using Smarty replace.

 

would this bit in that case not be app;icable? if the bridge is working and redirecting to the correct place then the actual email links no longer matter? or am i missing something there?

 

the code I posted above makes the two additional merge fields available to the email templates - I did test it before posting! :idea:

 

in the email templates, you can just call them as you would any other merge field...

 

{$my_custom_var}
{$my_custom_var2}

I tried it on the invoice payment reminder email template, but I assume it would work in any of them...

 

 

 

Ill test this again now. Could you tell me where i went wrong with my code in that case as i had expected mine to work? the code seemed to all be in order with correctly named functions etc yet the returned value was not available?

Im still very much a learner with php so would love to know where i went wrong for the future.

 

Thanks very much for your help with this.

Link to comment
Share on other sites

ill look at that now assuming that it does deal with the redirection itself...

that's what their site says...

 

Pretty Permalinks

 

Display links like http://www.mysite.com/clientarea/ rather than http://www.mysite.com/?ccce=clientarea. Also supports knowledgebase, announcement and download links.

though it would probably be better to test their trial version first before spending $55 every year on it! :idea:

 

would this bit in that case not be applicable? if the bridge is working and redirecting to the correct place then the actual email links no longer matter? or am i missing something there?

no, you're absolutely right - I think if the Pro version redirects in the way it suggests, then there's no need for the previous Smarty replace solution to be used... in fairness, you didn't mention site redirection in your original question - only tweaking the email links ! :)

 

Ill test this again now. Could you tell me where i went wrong with my code in that case as i had expected mine to work? the code seemed to all be in order with correctly named functions etc yet the returned value was not available? Im still very much a learner with php so would love to know where i went wrong for the future.

it's how you declared your merge field variable(s) - so instead of using...

 

$customurl = "My Custom Var2";

it needs to be in the format...

 

$customurl['url_brian'] = "My Custom Var2";
$customurl['url_satan'] = "WHMCS Bridge Over Troubled Water";

that will give you two merge fields to use in your email templates - {$url_brian} and {$url_satan}.

 

as you want to use 20+ merge fields, it might be safer to use my previous code as that is closer to the format specified by the documentation, e.g first declaring an array - though the above two lines does work without any such declaration.

Link to comment
Share on other sites

Ah i see. i need to define the variables as if they were in an array even without it having to be one.

 

and i just checked as i was not sure if they deal with the auto redirect as well as permalinks but it seems they do. they mention pretty permalinks (change ?cccee=client to /clientarea/ but they also deal with redirecting from whmcs to the proper whmcs bridge urls

 

That may well fix all the issues in one go as the urls in the emails would then be handled by the redirect plugin to the correct final destination.

 

And hopefully anyone with queries about the other (free) ways to do this will be able to use this thread to manage hooks properly.

 

One last question.

 

Do you know of any way to have these new merge fields actually show up within the merge fields display at the bottom of the edit email template section?

 

Assuming there may possibly need to be two hooks to add data to that array to display in there plus additionally in the presend so that it provides the same variables with the email send?

Link to comment
Share on other sites

quick update. the bridge pro plugin does indeed work.

 

all of the urls are rewritten correctly minus the admin area for whmcs so i can login correctly.

Couple of bits in case anyone gets stuck with it not working

 

when you are logged in as admin the redirects dont work. spent ages looking at the plugins and trying this or that turned on or off.

 

logged out and it worked straight away. dur

 

Thanks for your help brian really appreciated and hope this thread can help someone else also.

Link to comment
Share on other sites

Thanks for your help brian really appreciated and hope this thread can help someone else also.

i'm glad to hear switching to the pro plugin solved your redirection issues! :)

 

One last question.

 

Do you know of any way to have these new merge fields actually show up within the merge fields display at the bottom of the edit email template section?

 

Assuming there may possibly need to be two hooks to add data to that array to display in there plus additionally in the presend so that it provides the same variables with the email send?

there's a clever way to do it using Language Overrides, but the simpler way is the way you imagined by using two hooks - the hook below will add your merge fields to the "Available Merge Fields" table on the email templates page...

 

function hook_mergefieldsvars($vars) {

$merge_return = array();
$merge_return['my_custom_var'] = "My Custom Var";
$merge_return['my_custom_var2'] = "My Custom Var2";
return $merge_return;

}

add_hook("EmailTplMergeFields",1,"hook_mergefieldsvars");

you'll still need the previous posted hook to ensure the merge fields values display in the emails.

Link to comment
Share on other sites

i'm glad to hear switching to the pro plugin solved your redirection issues! :)

 

yep i had read through about it before but it seemed to ahve been updated since my first enquiry

 

there's a clever way to do it using Language Overrides, but the simpler way is the way you imagined by using two hooks - the hook below will add your merge fields to the "Available Merge Fields" table on the email templates page...

 

function hook_mergefieldsvars($vars) {

$merge_return = array();
$merge_return['my_custom_var'] = "My Custom Var";
$merge_return['my_custom_var2'] = "My Custom Var2";
return $merge_return;

}

add_hook("EmailTplMergeFields",1,"hook_mergefieldsvars");

you'll still need the previous posted hook to ensure the merge fields values display in the emails.

 

Thats perfect. I was thinking this would be the best way to do as i assume i can read in a list from elsewhere and do a foreach within the array to add all the items i want to be in there?

 

that way the hooks can be created and then just a single file will determine the additional email merge fields as it will be read in by both hook files.

 

What is the language override way? just for completeness?

Link to comment
Share on other sites

Thats perfect. I was thinking this would be the best way to do as i assume i can read in a list from elsewhere and do a foreach within the array to add all the items i want to be in there?

 

that way the hooks can be created and then just a single file will determine the additional email merge fields as it will be read in by both hook files.

possibly the easier way to do it is just have one file - declare the merge fields once within it and then have the two hooks within that one file...

 

<?php

if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function hook_emailvars($vars) {

$merge_fields = array();
$merge_fields['my_custom_var'] = "My Custom Var";
$merge_fields['my_custom_var2'] = "My Custom Var2";
return $merge_fields;

}

add_hook("EmailPreSend",1,"hook_emailvars");
add_hook("EmailTplMergeFields",1,"hook_emailvars");
?>

I think it's better to keep the number of files down and not have files referencing other files unless necessary - if for no other reason, it should make debugging easier!

 

when you say language overrides do you mean, for example, overriding the actual whmcs_url merge field here? as that would be ideal. i could then just create the overrides for the email merge fields and this would be a one stop fix to everything.

that's not what I had in mind - language overrides are just tweaking the labels for variables, rather than the content of them.

 

the option I had in mind was to modify an existing string variable, "Available Merge Fields", from the Email Template page and edit it to add a table with the new merge fields in...

 

so add an "overrides" folder within the admin/lang folder, create a file in there called "english.php" (or any other admin language) and add the following code...

 

<?php 

if (!defined("WHMCS")) die("This file cannot be accessed directly"); 

# Merge Fields 
$_ADMINLANG['mergefields']['url_brian'] = "My Custom Var2"; 
$_ADMINLANG['mergefields']['url_satan'] = "WHMCS Bridge"; 
$_ADMINLANG['mergefields']['title'] = "Available Merge  Fields".$my_custom_var."<br><br><div id=\"mergefields\"  style=\"border:1px solid  #8FBCE9;background:#ffffff;color:#000000;padding:5px;height:100px;overflow:auto;\"> 
<table> 
<b>Custom Merge Fields</b><br> 
<tr><td width=\"150\"><a href=\"#\"  onclick=\"insertMergeField('url_brian');return  false\">".$_ADMINLANG['mergefields']['url_brian']."</td><td  width=\"150\">{\$url_brian}</td></tr> 
<tr><td width=\"150\"><a href=\"#\"  onclick=\"insertMergeField('url_satan');return  false\">".$_ADMINLANG['mergefields']['url_satan']."</td><td  width=\"150\">{\$url_satan}</td></tr> 
</table> 
</div>";

what this does is create a similar table to the existing merge fields table and places it between "Available Merge Fields" and the existing table.

 

from a cosmetic point of view, using the hook is far cleaner - but one possible use of this Language Override solution would be if you wanted to give longer explanations of what each merge field is for...

 

you could even define your merge field values in the language override file and then call them via references in the hook - it would work, but I don't think it's really advisable in this case - it would just make the solution far more complex than it needs to be - just declare them in the hook and you're good to go. :idea:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated