Support

Account

Home Forums General Issues Fields not saving – Slug Reply To: Fields not saving – Slug

  • Thanks for reply.
    This one seems to work. If someone wants to make it shorter or better please be free to do it.

    // Force slug auto generate
    function myplugin_update_slug( $data, $postarr ) {
    if ( 'page' === $postarr['post_type'] || 'post' === $postarr['post_type'] || 'attachment' === $postarr['post_type'] && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
    $data['post_name'] = sanitize_title( $data['post_title'] );
    }
    return $data;
    }
    add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );