Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Templates #78

Closed
interceptclients opened this issue Oct 27, 2016 · 17 comments
Closed

Switch Templates #78

interceptclients opened this issue Oct 27, 2016 · 17 comments

Comments

@interceptclients
Copy link

Does anyone have any code samples for switching templates based on which blog in a multi-site implementation is sending transactional emails?

Thanx in advance for your thoughts ....... Rick

@rajumsys
Copy link
Contributor

you can use the wpsp_template_id hook to change template based on your logic.

@interceptclients
Copy link
Author

Thanx, found that but am too new to all this to get it to work. Hoping to have a working example to follow.

@interceptclients
Copy link
Author

interceptclients commented Oct 28, 2016

Will this work?

/* Set SparkPost Template */ 
function sp_assign_template($body) {
    $blog_id = get_current_blog_id();
    $this->settings = SparkPost::get_settings();
    $template_id = $this->settings['template'];
    if ( ( is_multisite() ) &&  ( $blog_id <> '1' ) ) {  
        $new_template_id = $template_id . '-' . $blog_id;
    $body['content']['template_id'] = $new_template_id;
    } else {
        $body['content']['template_id'] = $template_id;
    } 
    return $body;       
}
add_filter('wpsp_template_id', 'sp_assign_template');

If so, how can I confirm the new template was actually created in SparkPost?

...... Rick

@rajumsys
Copy link
Contributor

rajumsys commented Oct 28, 2016

@interceptclients

  • you don't need to modify settings, the way you did in
    $this->settings = SparkPost::get_settings();
  • you can't create template here. you need to create template using SparkPost UI or using API. Once you created the template, all you need here is to specify it's ID.

Rest looks good, though I'm not sure about particular logics to detect blog id.

@interceptclients
Copy link
Author

Trying to get the settings not modify them. How do I get the stored template value?

I don't want to create templates I want to validate they exist. How's that done?

@rajumsys
Copy link
Contributor

rajumsys commented Oct 28, 2016

wpsp_template_id hook provides you that.

function sp_assign_template($storedTemplateId) {
 // $storedTemplateId contains the template ID that's stored in settings

  return 'NEW_TEMPLATE_ID';
}
add_filter('wpsp_template_id', 'sp_assign_template');

And if you want to validate, if the template really exists in SparkPost, you have to use the API I've linked above. Remember, your API key must have permission to read templates for that.

@interceptclients
Copy link
Author

Thank you. How can I validate a template exists before trying to use it? If the new template was not created it should assign the stored template id.

@rajumsys
Copy link
Contributor

How can I validate a template exists before trying to use it?

And if you want to validate, if the template really exists in SparkPost, you have to use the API I've linked above. Remember, your API key must have permission to read templates for that.

@interceptclients
Copy link
Author

Missed that, thank you. Well over my head to create, need a php example to modify. Appreciate your prompt and thorough replies. Maybe I can find someone on fiverr willing to do it for me.

@rajumsys
Copy link
Contributor

Cool. Glad to know that helped.

@interceptclients
Copy link
Author

Seems I missed something previously. How is the modified $body with the new template id returned if the stored template id is passed in the function call?

@rajumsys
Copy link
Contributor

just return the new template id from the function.

@interceptclients
Copy link
Author

Thanx a bunch!

@rajumsys
Copy link
Contributor

I updated the above example to make it clearer. Thanks

@interceptclients
Copy link
Author

interceptclients commented Oct 28, 2016

Like this,

/* Set SparkPost Template */ 
function sp_assign_template($storedTemplateId) {
    $blog_id = get_current_blog_id();
    if ( ( is_multisite() ) &&  ( $blog_id <> '1' ) ) {  
        $newTemplateId = $storedTemplateId . '-' . $blog_id;
    } 
    return $newTemplateId;      
}

@rajumsys
Copy link
Contributor

Right. But your code now has logical error.

function sp_assign_template($storedTemplateId) {
    $blog_id = get_current_blog_id();
    if ( ( is_multisite() ) &&  ( $blog_id <> '1' ) ) {  
        return $storedTemplateId . '-' . $blog_id;
    }  else {
        return $storedTemplateId;
    }
}

@interceptclients
Copy link
Author

Fabulous, you're the BEST!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants