Add Simplenews category programmatically

Public

Here is a little snippet that shows you how to add new Simplenews category e.g. from your module:

Get raw version
php
  1. /**
  2.  * Save Simplenews category
  3.  * @param int $simplenews_taxonomy_id
  4.  * @param $category_name Simplenews category name.
  5.  */
  6. function mymodule_save_newsletter_category($simplenews_taxonomy_id = 6, $category_name = 'New category') {
  7.  
  8. //ID of taxonomy related to Simplenews
  9. $simplenews_taxonomy_id = 6;
  10.  
  11. //Create default Simplenews category object
  12. $category = new stdClass();
  13. $category->tid = '';
  14. $category->format = 'plain';
  15. $category->priority = 0;
  16. $category->receipt = 0;
  17. $category->from_name = variable_get('site_name', 'Asist Local');
  18. $category->email_subject = ' [[simplenews-category:name]] [node:title]';
  19. $category->from_address = variable_get('site_mail', '');
  20. $category->hyperlinks = 1;
  21. $category->new_account = 'none';
  22. $category->opt_inout = 'double';
  23. $category->block = 1;
  24. $category->name = '';
  25. $category->description = '';
  26. $category->$simplenews_taxonomy_id = $simplenews_taxonomy_id;
  27.  
  28. //Create taxonomy term related to Simplenews category
  29. $term = new stdClass();
  30. //Automatically get term ID
  31. $term->tid = '';
  32. $term->vid = $simplenews_taxonomy_id;
  33. $term->name = $category_name;
  34. $term->description = "My first category I've created programmatically!";
  35. $term->weight = 0;
  36.  
  37. taxonomy_term_save($term);
  38. $category->tid = $term->tid;
  39.  
  40. //Finally we can save category!
  41. simplenews_category_save($category);
  42. }