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'm wondering if is possible to change the text "Enter title here" based on the post format that you selected? For example: If you select the post format Quote than your text will be "Enter author's quote". Is that possible?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You need to do the following;

jQuery(document).ready(function( $ ) {

    $('input.post-format').click(function(){

    $('#title-prompt-text').val( $(this).next('label').text());

    })

});

This will replace the title with whichever post format you select.

Save the above into a file called custom-post-title.js and you will need to place this file into your theme folder. Either in the root or within a folder named /js/ - but just note that if you put it within a sub-folder make sure that you reflect this within the next snippet I give you.

Then in your theme functions.php file you need to enqueue your script for use in the admin area and in this case we will enqueue it on the post/page edit screens only (so it doesn't unnecessarily load elsewhere and create overhead.

function custom_post_title($hook) {
    if( 'edit.php' != $hook )
        return;
    wp_enqueue_script( 'custom_post_title', get_bloginfo('template_directory') . '/custom-post-title.js', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'custom_post_title' );

Usuing admin_enqueue_scripts filter means we are only injecting our Javascript file on the backend (admin area) not on the front end, after all its useless to your users.

If you want to add a little further customization and change the default post title then use (also within your functions.php file);

function custom_title( $title ){

    $screen = get_current_screen();

    if ( 'post' == $screen->post_type ) {

    $title = 'Choose your post format';

    }

    return $title;

}

add_filter( 'enter_title_here', 'custom_title' );

This is something similar to what Rutwick posted above previously; although this does not address your immediate question, it does extend the level of customization so you can have,

  • A default post title upon entering the edit screen
  • The post title dynamically updated upon the selection of a post format.

Good luck!

Notes:

I've made an important update to the jQuery, by modifying;

$('#title-prompt-text').text( $(this).val());

In favor of,

$('#title-prompt-text').val( $(this).next('label').text());

The former was grabbing its property from the "value" attribute of the input field and while this is perfectly fine to do in most cases, WP assigns the "Standard" post_format a value=0 instead of value=Standard - for all other post_formats the value is equal to that of its name.

The reason for this is because WP returns false when for example using the API function get_post_format(); thus this is how it decides to determine its default standard post. Which is a bit silly really, so the latter adjustment takes care of that by grabbing the value of the input fields adjacent <label> element instead which contains the name of our post format needed.

share|improve this answer

Go for jQuery! You can do this via php, but only when you are editing a post, for which you've already set the post format. But for 'Add New', you cannot, because the post format is not set.

share|improve this answer
    
No, it's not post type. I'm need the function for the post format. –  Daniel May 19 '12 at 12:50
    
Oops... missed that part... wait let me check... –  Rutwick Gangurde May 19 '12 at 12:52
    
You have to do some jQuery for it! Because my function works on $post object, which does not have the post format. –  Rutwick Gangurde May 19 '12 at 12:56
    
So when either of the radios is checked, change the place holder text. –  Rutwick Gangurde May 19 '12 at 12:59
    
Exactly! That's is the idea. –  Daniel May 19 '12 at 13:01

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.