0

I want to create a script for when I add a title and an image to a post, and then when i publish, i want it to auto create a description

Something like . . . . .

You are viewing [title of post]! These are great hairstyle designs in our [category] section. Hope you try this look and feel on your hair today. Ever considered changing it up, you might like this [random post title and link]

This would be the body content of the post, In the last part it will select a random post and place his title and link (you might like this [random post title and link])

How can i achieve this using a php script and calling it from the publish button?

1
0

You need to use save_post_post hook and use $wpdb->update() method. The code -

add_action( 'my_save_post_post', 'save_post_post' );
function my_save_post_($post_ID, $post )
{
    global $wpdb;

    $post_content = 'You are viewing '. $post->post_title .'! These are great hairstyle designs in our '. get_the_category_list( ', ', '', $post_ID ) .' section. Hope you try this look and feel on your hair today. Ever considered changing it up, you might like this';

    $random = get_posts( array(
        'post__not_in'    => $post_ID,
        'posts_per_page'  => '1',
        'post_type'       => 'post',
        'post_status'     => 'publish',
        'orderby'         => 'rand'
    ));

    if( $random )
    {
        $rpost = array_shift( $random );
        if( isset($rpost->ID) )
        {
            $post_content .= sprintf( 
                ' <a href="%s">%s</a>', 
                $rpost->post_title, 
                get_permalink( $rpost->ID )
            );
        }
    }

    $wpdb->update( 
        $wpdb->posts, 
        array( 'post_content' => $post_content ), 
        array( 'ID' => $post_ID ) 
    );
}
1
  • This seems really good, in which file this must be hardcoded?
    – elvismdev
    Jan 10 '14 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.