Skip to content

Instantly share code, notes, and snippets.

@AnnaMariaEriksson
Last active June 21, 2016 14:33
Show Gist options
  • Save AnnaMariaEriksson/cc2a6cdbdda5e20dc35d to your computer and use it in GitHub Desktop.
Save AnnaMariaEriksson/cc2a6cdbdda5e20dc35d to your computer and use it in GitHub Desktop.
Grab first sentence of from Instagram and make it a post title. Available for multiple categories with personal excerpts and create single WordPress posts without a plugin
add_action( 'save_post', 'save_post_wpse_87921', 10, 2 );
function strposa($haystack, $needles=array(), $offset=0) {
$chr = array();
foreach($needles as $needle) {
$res = strpos($haystack, $needle, $offset);
if ($res !== false) {
$chr[$needle] = $res;
}
}
if(empty($chr)) {
return false;
}
return min($chr);
}
function save_post_wpse_87921( $post_id, $post_object ){
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
if(has_excerpt($post_id)){
return;
}
$title_delimiters = array('.', '!', '?');
$new_title = substr($post_object->post_title, 0, strposa($post_object->post_title, $title_delimiters)+1);
remove_action( 'save_post', 'save_post_wpse_87921' );
// Call wp_update_post update, which calls save_post again.
if ( in_category( 'category-1', $post_id )&& has_tag('instagram', $post_id)) {
// Update post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => '',
'post_excerpt' => 'This is the personal excerpt for Category 1. Change this into whatever you'd like.' );
// Update the post into the database
wp_update_post( $my_post );
// set_post_format($post_id, 'image');
}
if ( in_category( 'category-2', $post_id )&& has_tag('instagram', $post_id)) {
// Update post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => '',
'post_excerpt' => 'This is the custom excerpt for the second category. You can change this into whatever you'd like.' );
// Update the post into the database
wp_update_post( $my_post );
// set_post_format($post_id, 'image');
}
if ( in_category( 'category-3', $post_id )&& has_tag('instagram', $post_id)) {
// Update post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => '',
'post_excerpt' => 'This is the custom excerpt for the third category. Change this into whatever you'd like as an excerpt.' );
// Update the post into the database
wp_update_post( $my_post );
// set_post_format($post_id, 'image');
}
add_action( 'save_post', 'save_post_wpse_87921', 10, 2 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment