0

I am trying to use a simple publish_post / publish_page hook to get the URL of the post/page when it is either published or updated so I can later turn it into a static page.

Is it possible to add this hook outside of the theme functions.php file, because this is far more preferable to me? I also am unsure how to grab the file location / URL from the result? Any help is very much appreciated.

0

A simple solution for you - this hook will send you a full URL to the post that is being published. Don't forget to update your@email.com

function post_published_notification( $ID, $post ) {
    $permalink = get_permalink( $ID ); // GETTING THE PERMALINK
    $to[] = 'your@email.com'; // UPDATE THIS

    $title = $post->post_title;
    $subject = sprintf( 'Published: %s', $title );
    $message = sprintf( 'View: %s', $permalink );
    wp_mail( $to, $subject, $message, [] );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );

If you want it to work on post update as well, just add this action to the bottom of it:

add_action( 'save_post', 'post_published_notification', 10, 2 );

You can add this hooks to the bottom of your functions.php or in your plugin.

0

Yeah, you totally can add your code to some plugin/drop-in, instead functions.php in the theme. And you can use get_permalink() function to do that.

  • Do I need any sort of includes, though? – Autumn Rowan Apr 23 '18 at 12:18
  • You can read about WordPress Plugins at their codex page. Or You can just make a script with code and include where you want. – Oleg Butuzov Apr 23 '18 at 13:03

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.