Created
November 28, 2016 06:59
-
-
Save gelanivishal/11024ea5c698d07909b3b9abb62c038f to your computer and use it in GitHub Desktop.
Hook while save page and create new post and append function.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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