Activate or deactivate rules programmatically

This is how you can activate or deactivate rules by its machine readable name programmatically.

<?php
function ticket_rules_activate($rule_name, $active = TRUE) {
   
$proxy = rule_load($rule_name);
   
$rule = $proxy->get_rule();
   
$rule['#active'] = $active;
   
rules_item_save('rules', $rule_name, $rule);
   
watchdog('ticket', t('Rules %rule_name active (%active)', array('%rule_name' => $rule_name, '%active' => ($active ? 'true' : 'false'))), '');
}
?>
Knowledge keywords: