• Resolved Aimee

    (@greatdanemaniac)


    Hi!
    I need help with this. I’d like to have a default title based on the post formats status or aside. Aka if I select the post format “aside” in the post format metabox, the title should automatically change to i.e “Update + date”. If I select another post format, the title field should stay empty and I shall fill it in myself.

    I have some basic code and this works as long as I do not add the post format stuff to it. I get a default title but it’s stuck no matter what post format I choose. I only want this for the post formats I mentioned.

    add_filter( 'default_title', 'my_default_title', 10, 2 );
    
    function my_default_title( $post_title, $post ){
    
          if(has_post_format("aside"))
        {
    
    	$title = "Update |";
    
      // create your preferred title here
      $post_title = $title . date( 'F j Y' );
    
    }
      return $post_title;
    }

    The titles are not supposed to be shown on my blog. I only want this in order to have a unique permalink and title for each post, instead of some number if I forget or don’t fill in the title myself. I know I can use wp_insert_post_data for this as well, but I haven’t seen a single proper example of how to incorporate post formats into that code, and I’ve googled this problem for 3-4 days now and I’m getting nowhere.

    I’m a complete noob, so please be thourough with your explanation on how to make this work. Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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. ??

    Thread Starter Aimee

    (@greatdanemaniac)

    Thanks a bunch! I’ll try this and get back to you.

    Thread Starter Aimee

    (@greatdanemaniac)

    Thanks again a bunch! I’ve tried it now and the best and simple solution to my problem was to take your code below, change a few things and set a title as the post is published. This way I also get a proper permalink as well.

    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 = date('F j Y');
    		$post_id = get_post_format();
            return $post_id . '|' . $time;
        }
        return $goofyvalue;
    }
    
    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' ] = 'Aside |' .date('F j Y');
    		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 );
    
    function status_post_publish( $goofyID ) {
    	global $wpdb;
    	if ( empty( $wpdb ) ) {
    		return new WP_Error( 'invalid_post', __( 'Invalid post' ) );
    	}
    	if( has_post_format('status',$goofyID) && !wp_is_post_revision($goofyID) ) {
    		$args = array();
    		$args['ID'] = $goofyID;
    		$args['post_title' ] = 'Status update |' .date('F j Y');
    		remove_action('publish_post', 'status_post_publish');
    		wp_update_post( $args );
    		add_action('publish_post', 'status_post_publish', 10, 1 );
    	}
    }
    add_action('publish_post', 'status_post_publish', 10, 1 );

    I can’t thank you enough for this!

    Cool. Thanks for sharing code. Cheers..

    Thread Starter Aimee

    (@greatdanemaniac)

    Hello again. Even though this thread has been marked as resolved, I noticed that the code I used from you, @shadez created an unexpected problem with the menus. All menus received the same name.

    Finally, I found a solution and hopefully everything will be fine from now on. The main thing is that I’d like a default title to a specific post format, and it doesn’t matter how that happens, as long as I make it happen.

    Here’s my code:

    add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );
    
    function modify_post_title( $data , $postarr ) {
    
    if( has_post_format('status')) {
       if ($data['post_type'] == 'post') {
        $data['post_title'] = 'Status update | ' . date('j F Y');
      }
    }
      return $data;
    }

    Thanks again, though for all your help. I never thought this would work but it worked like a charm!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Create a default title based on post format status and or aside?’ is closed to new replies.