thats a pretty neat question.
to be exact, keeping Custom Post Types aside, there is no direct WP hook to change title when aside is clicked.
working:
the first time you create a post-format post, say aside, a new term gets created in wp_terms
table and is mapped to -relations and -taxonomy further. but these dont store a separate ‘title’ field anywhere. ie, all post details go with post metadata only.
so when you enter the create new post page, the default post title is populated via html and is not related to post-formats. also, the toggle function of the radio buttons is handled by a JS file (post.js
) which doesnt have any hooks attached to it, and it is part of core, and we dont hack the core!
couple of workarounds:
before editing:
1. adding this in functions.php
add_filter( 'default_content', 'my_goofy_content' );
function my_goofy_content( $content ) {
$content = "Egestas? Turpis! Rhoncus urna sit sagittis, magna tincidunt montes rhoncus est. Scelerisque nascetur in, elementum! Natoque ridiculus adipiscing? Rhoncus? Ultrices et mattis, enim aliquet montes enim, porttitor montes nec et mid sociis, turpis! Vel non, tristique et rhoncus porttitor lectus cras a in pulvinar non? In scelerisque non? Elit dictumst, ridiculus? Tincidunt? Parturient sit lectus nisi odio sit ac dapibus.";
return $content;
}
add_filter( 'default_title', 'my_goofy_title' );
function my_goofy_title( $title ) {
$title = "Egestas? Turpis!";
return $title;
}
this is what you mentioned.
this will set and display title and content while on editing page.
2. what if you want only the label to change for the input element (if you click it disappears ie).
this you can do by introducing JS.
function boogy_enqueue( $hook ) {
wp_enqueue_script( 'my_custom_script', get_site_url( __FILE__ ) . '/wp-content/plugins/edit-label-hack.js' ); /* use plugin_dir_url if working */
}
add_action('admin_enqueue_scripts', 'boogy_enqueue');
and create edit-label-hack.js
in plugins folder and place this code:
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) { oldonload(); }
func();
}
}
}
addLoadEvent(function() {
document.getElementById('title-prompt-text').innerHTML = 'This is JS enabled label title for input';
});
3. set a default value (unseen) beforehand. this way, if you leave the title blank, it gets autofilled. this way, you can type titles in for others and leave blank for aside:
add_filter('pre_post_title', 'set_default_newpost_data');
add_filter('pre_post_content', 'set_default_newpost_data');
function set_default_newpost_data($goofyvalue)
{
if ( empty($goofyvalue) ) {
$time = current_time( 'c', 1 );
return 'Default | ' . $time;
}
return $goofyvalue;
}
during editing:
4. well like i said, there are no hooks here. our only option is with jquery.
function boogy_enqueue( $hook ) {
wp_enqueue_script( 'my_custom_script', get_site_url( __FILE__ ) . '/wp-content/plugins/myscript.js' ); /* use plugin_dir_url if working */
wp_enqueue_script( 'my_custom_script', 'https://code.jquery.com/jquery-2.1.0.js' ); /* use plugin_dir_url if working */
wp_enqueue_script( 'my_custom_script', get_site_url( __FILE__ ) . '/wp-content/plugins/post-format-click.js' ); /* use plugin_dir_url if working */
}
add_action('admin_enqueue_scripts', 'boogy_enqueue');
and again, create post-format-click.js in plugins dir with code:
$(document).ready(function(){
$('[name=post_format]').click(function() {
$('[name=post_title]').val('Title changed to '+$(this).val());
});
});
^ i tried this code in fiddle but gotto fixup on WP.
after editing:
5. rope in the action hook from post transition to work for you. in this case, lets work on publish_post
:
function aside_post_publish( $goofyID ) {
global $wpdb;
if ( empty( $wpdb ) ) {
return new WP_Error( 'invalid_post', __( 'Invalid post' ) );
}
if( has_post_format('aside',$goofyID) && !wp_is_post_revision($goofyID) ) {
$args = array();
$args['ID'] = $goofyID;
$args['post_title' ] = 'This is after post publish!';
remove_action('publish_post', 'aside_post_publish');
wp_update_post( $args );
add_action('publish_post', 'aside_post_publish', 10, 1 );
}
}
add_action('publish_post', 'aside_post_publish', 10, 1 );
although it says publish_post, we are pretty much late to the party and WP has already set stuff, hence using wp_update_post
.
and if all this doesnt work, like you mentioned, you can loop over wp_insert_post_data
and couple of other handy api’s and put timechecks, post status checks, etc to get it to work.
this took me an hour to think and type down. so would like to hear on how you implemented your code, even if diff from my comments. ??