Skip to content

Instantly share code, notes, and snippets.

@gelanivishal
Created November 28, 2016 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gelanivishal/11024ea5c698d07909b3b9abb62c038f to your computer and use it in GitHub Desktop.
Save gelanivishal/11024ea5c698d07909b3b9abb62c038f to your computer and use it in GitHub Desktop.
Hook while save page and create new post and append function.php
<?php
add_action( 'save_post', 'my_save_post_function', 10, 3 );
function my_save_post_function( $post_ID, $post, $update ) {
$fields=$_POST['fields'];
if(isset($_POST['fields']) && $_POST['fields']!=""){
$fields= implode(array_values($fields));
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'my_save_post_function');
if(post_exists(wp_strip_all_tags($fields))){
$exit_post_id = get_page_by_title(wp_strip_all_tags($fields), "", 'post');
$my_post = array(
'ID' => $exit_post_id->ID,
'post_title' => wp_strip_all_tags($fields),
'post_content' => $_POST['post_content'],
);
wp_update_post( $my_post, true );
}
else{
$my_post = array(
'post_title' => wp_strip_all_tags($fields),
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ));
wp_insert_post( $my_post, true);
}
// re-hook this function
add_action('save_post', 'my_save_post_function');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment