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?
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:
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:
Lots of caveats :
Based on your statement that
that code completely hijacks the
title. If you want you append or prepend the display name to some
other title that won’t work.
john-smith
,john-smith-2
,john-smith-3
, etcget_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.