Currently on my D6 site registered as well as anonymous users can post articles. A custom text field has to be filled by unregistered users while creating an article. This text field's value is shown as Node author for anonymous posters.

Now I am all set to upgrade this site to D7. I tried using the following snippet to display custom text field value as the 'node author' if posted by Anonymous user, But it has some issues...

  1. menu_get_object() works only for full node view and not for teasers etc.
  2. It also changes $name every where on the page eg: for unregistered comments too.
function mytheme_username_alter(&$name, $account) {
  $node = menu_get_object();
    if($account->uid == "0") {
      $name = $node->field_custom_author['und'][0]['value'];

Question: How can I tweak this function to display custom text field's value as 'Node Author' if the node is created by an anonymous user, without changing $name elsewhere on the page?

If there's a better function/solution than this, i am all open to try that too.

1 Answer 1


You have two viable options:

  1. In theme system, use either node.tpl.php file to do it, or probably better template_preprocess_node(&$variables). I'll discuss function as that's what I prefer. When using function, copy it to your own template.php, renaming to mytheme_preprocess_node(&$variables). As a source, use parent theme if it implements it. If not, or if there is no parent theme, use link I provided above.

    Then find a line like this:

    $variables['node'] = $variables['elements']['#node'];

    and after it modify $variables['node'] as you see fit.

  2. Use Computed Field to create a field that shows author's name, and in case of anonymous authors displays arbitrary text. Turn off displaying author and use this field instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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