Created
August 18, 2020 16:40
-
-
Save nickyreinert/1f23d4afee14ad8ce065ce2d165517ac to your computer and use it in GitHub Desktop.
Set post tags when post is saved based on post title and regular expression
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 | |
// functions.php is part of your child theme | |
function add_tags_to_post_on_save( $post_id ) { | |
// do not trigger if this is revision only | |
if ( wp_is_post_revision( $post_id ) ) {return;} | |
// get the current post title | |
$post_title = get_the_title($post_id); | |
// apply regular expression to the title | |
preg_match('/(.*)/', $post_title, $matches); | |
// take the first matching group and use it as a tag, FALSE means -> do not append to actual tags | |
wp_set_post_tags($post_id, $matches[1], FALSE); | |
} | |
add_action( 'save_post', 'add_tags_to_post_on_save' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment