How to Override Post Titles

Hey Everyone,

I am using a plugin that creates a new post each time a photo is uploaded. However, what that does is use the photo's file name as the post title.

This isn't ideal and doesn't look good on the site. I was looking for a plugin or way to override any existing post title with a default post title of my choice. I am sure this wouldn't be able to change past posts but was hoping there has to be a way to do this for all newly created posts.

Thanks for any help you can provide!

  • Tom Eagles
    • Syntax Hero

    @ThinkTaiwan

    Hi Mike, this is the section of code that needs to be changed,

    * @param $post_id int The post ID of the attachment that was just added from an image upload.
    	 * @return mixed Only used when breaking from the script
    	 */
    	public function create_post_from_image( $post_id ) {
    
    		if( ! wp_attachment_is_image( $post_id ) )
    			return;
    
    		$new_post_category = array();
    
    		/* If this image is being added through an existing post, we want to make sure it inherits
    		 * the category setting from its parent. */
    		if ( $parent_post_id = get_post( $post_id )->post_parent ) {
    			if ( $parent_post_categories = get_the_category( $parent_post_id ) ) {
    				foreach( $parent_post_categories as $post_cat )
    					$new_post_category[] = $post_cat->cat_ID;
    			}
    		}
    
    		$afip_options = get_option( 'afip_options' );
    		$current_user = wp_get_current_user();
    
    		/* Allow other functions or themes to change the post date before creation. */
    		$new_post_date = apply_filters( 'afip_new_post_date', current_time( 'mysql' ), $post_id );
    
    		/* Allow other functions or themes to change the post title before creation. */
    		$new_post_title = apply_filters( 'afip_new_post_title', get_the_title( $post_id ), $post_id );
    
    		/* Allow other functions or themes to change the post categories before creation. */
    		$new_post_category = apply_filters( 'afip_new_post_category', $new_post_category, $post_id );
    
    		/* Allow other functions or themes to change the post content before creation. */
    		$new_post_content = apply_filters( 'afip_new_post_content', '', $post_id );
    
    		$new_post_id = wp_insert_post( array(
    			'post_title' => $new_post_title,
    			'post_content' => $new_post_content,
    			'post_status' => $afip_options['default_post_status'],
    			'post_author' => $current_user->ID,
    			'post_date' => $new_post_date,
    			'post_category' => $new_post_category,
    			'post_type' => $afip_options['default_post_type'],
    		));
    
    		if ( isset( $afip_options['default_post_format'] ) && 'standard' != $afip_options['default_post_format'] )
    			set_post_format( $new_post_id, $afip_options['default_post_format'] );
    
    		update_post_meta( $new_post_id, '_thumbnail_id', $post_id );
    		wp_update_post( array( 'ID' => $post_id, 'post_parent' => $new_post_id, 'post_status' => 'inherit' ) );
    	}
    }

    there is a section here you could work with via a function.

    /* Allow other functions or themes to change the post title before creation. */
    		$new_post_title = apply_filters( 'afip_new_post_title', get_the_title( $post_id ), $post_id );

    Cheers

    Tom

  • Mike Price
    • The Incredible Code Injector

    Hmm thanks so much Tom. I wish I knew how to code in PHP im really a novice when it comes to development which is why I rely heavily on awesome plugins. but maybe with some research I can figure this out. There might even be a youtube video out there somewhere haha. Thanks again!

    Mike

  • phillcoxon
    • The Crimson Coder

    Well the very simplest solution - hardcoding a standard title every time - would be to change this:

    /* Allow other functions or themes to change the post title before creation. */
    		$new_post_title = apply_filters( 'afip_new_post_title', get_the_title( $post_id ), $post_id );

    to something like:

    $new_post_title = "My Post Title";

    Of course that means every image will be uploaded with the exact same title.

    A better question might be - what do you want to appear as the title instead of the image filename? The same text every time or something that can be inserted automatically like the time, name of the user who uploaded etc.

  • Mike Price
    • The Incredible Code Injector

    You are a godsend thank you!..The answer to your question is I was originally just going to place the title as "Comment" so people know to click on it to open the post page since featured image on my theme isn't clickable. However, it would be even more awesome if you knew the code to insert the "username" of the person who uploaded it as the post title instead.

    Thank you so much for your help on this!

  • phillcoxon
    • The Crimson Coder

    These should do the trick. Just off the top of my head and I'm very tired but should work just fine:

    "Uploaded by username":

    $new_post_title = "Uploaded by " . $current_user->user_login;

    "Uploaded by Firstname Lastname":

    $new_post_title = "Uploaded by " . $current_user->user_firstname . " " . $current_user->user_lastname;

  • phillcoxon
    • The Crimson Coder

    You're welcome :slight_smile:

    One final thing to remember... be very careful of any updates to that plugin as it will probably overwrite the custom code you've just inserted. So either don't upgrade that plugin or keep a note of the custom code so you can reinsert it.

    If you want to be extra clever you can edit the plugin to change the name so that it will be treated as a new plugin and therefore won't try to upgrade.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.