SparkPost / wordpress-sparkpost

WordPress plugin to use SparkPost email
https://wordpress.org/plugins/sparkpost/
20 stars 15 forks source link

Switch Templates #78

Closed interceptclients closed 8 years ago

interceptclients commented 8 years ago

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 commented 8 years ago

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

interceptclients commented 8 years ago

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

interceptclients commented 8 years ago

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 commented 8 years ago

@interceptclients

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

interceptclients commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

Cool. Glad to know that helped.

interceptclients commented 8 years ago

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 commented 8 years ago

just return the new template id from the function.

interceptclients commented 8 years ago

Thanx a bunch!

rajumsys commented 8 years ago

I updated the above example to make it clearer. Thanks

interceptclients commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

Fabulous, you're the BEST!