Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a custom post type called "teacher" and I want to use that person's display name as the title of their post.

Details: 1. The "teacher" custom post type supports title. 2. Only logged in teachers can post. 3. I'm using frontend posting by Rev Voodoo

I've come up with this based on various snippets I've found around here but can't get it to work:

function wpa65253_teacher_title( $title ) {
    global $post;
    if ( isset( $post->ID ) ) :
        if ( empty( $_POST['post_title'] ) && in_the_loop() && 'teacher' == get_post_type( $post->ID ) )
            $title = get_user_meta($user->ID, 'display_name', true);
    endif;
    return $title;
}
add_action( 'submitpost_box', 'wpa65253_teacher_title' );

I've seen various questions about creating a title based on custom fields but I want to pull them from the user's name in his profile.

This code by t31os also no longer works:

add_action( 'submitpost_box', 'hidden_type_title' );

function hidden_type_title() {
    global $current_user, $post, $post_type;

    // If the current type supports the title, nothing to done, return
    if( post_type_supports( $post_type, 'title' ) )
        return;

    ?>
    <input type="hidden" name="post_title"value="<?php echo esc_attr( htmlspecialchars( get_the_author_meta( 'display_name', $current_user->data->ID ) ) ); ?>" id="title" />
    <?php
}

What am I doing wrong?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

If you are using that code by Rev Voodoo, you will need to edit that code, not try to hook into WordPress core code. The form in that link submits to itself, and handles its own post processing thus bypassing many core hooks.

Some of the hooks you are trying to use aren't going to work. I won't swear to it without investigating more thoroughly but I don't think I would ever use, or try to use, the submitpost_box hook for this purpose (It runs while the edit form is being displayed, not when it is submitted). It certainly won't work in this case because Rev Voodoo's code never loads the page that uses that hook-- wp-admin/edit-form-advanced.php.

It looks to me like you need to edit the following:

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
} else {
    echo 'Please enter the wine name';
}

I am not sure where you expect to get your teacher name. I am not very comfortable with this but you state that "Only logged in teachers can post". If you have locked the form adequately then you may be able to do something like this instead of that code:

// Do some minor form validation to make sure there is content
global $currentuser;
get_currentuserinfo();
if (isset($current_user->display_name)) {
    $title = $current_user->display_name;
} else {
    echo "No user data";
}

Lots of caveats :

  • Completely untested
  • Based on your statement that

    I want to use that person's display name as the title of their post

    that code completely hijacks the title. If you want you append or prepend the display name to some other title that won't work.

  • You stand a chance of getting post slugs like john-smith, john-smith-2, john-smith-3, etc
  • I am concerned about using get_currentinfo because I don't know how access to your form is controlled. If access to the form is inadequate then any user can post as a teacher.
share|improve this answer
    
It works great! Thank you for explaining it to me. I've limited access to the teacher form using Justin Tadlock's Members plugin and added a custom capability for just that role. I'm also ok with the numerated slugs issue - my target audience is limited enough that there will be small chance of people with the same name. –  annabwashere Mar 17 '13 at 18:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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