How to set the post slug (post name) when creating or editing a post

Created by Phil Kurth, Modified on Fri, 6 Sep at 2:28 PM by Phil Kurth

<?php
add_filter( 'af/form/editing/post_data', function ( $post_data, $form, $args ) {

  // Check if this is the correct form. If not, return the post data as is.
  if ( $form['key'] !== '{YOUR_FORM_KEY}' ) {
    return $post_data;
  }

  // Set the post excerpt
  $post_data['post_name'] = sanitize_title( $post_data['post_title'] );

  return $post_data;
}, 10, 3 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article