0

I currently have nodes set to unpublished by default because I want to approve user submitted content before publishing it live. However, admins should have their nodes published by default. How can I accomplish this?

3

Assuming Drupal 6.

Define a hook_form_alter function in your custom module and set the '#default_value' of the status element to TRUE. This will only run if the user has the correct permission. This way, nodes can also be unpublished by unticking the 'Published' checkbox.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
    if (user_access('administer nodes')) { // or other permission
        $form['options']['status']['#default_value'] = TRUE;
    }
}

Now your admin's nodes will be automatically published.

Thanks to andy for his comments.

  • 1
    Careful - this effectively prevents the admin from being able to have unpublished content (whether they've ticked the checkbox or not): it would be better to simply change the default value of the published checkbox to be published. Also, rather than checking against roles, I think it's a bit nicer to check against a permission. And you're not gonna be checking against a single form ID, as each content type has its own node form. – Andy Aug 26 '11 at 8:32
  • You've raised some good points. However, even if #default_value was defaulted to published, wouldn't the problem of never being able to unpublish still exist? Perhaps we should check if the node has already been created. Also, do you mind explaining your last point some more? – Jukebox Aug 26 '11 at 14:42
  • Just kidding, #default_value works. Changing my answer. – Jukebox Aug 26 '11 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.