Support

Account

Home Forums General Issues Fields not saving – Slug

Solving

Fields not saving – Slug

  • Lost almost 2 days on it. So I feel better to record it here in forum in case somebody else need it.

    It could be detected by deactivating plugins very quickly, but problem is wrong ACF Group slug is already in databae, and there is a devil. Deactivating plugins wont detect anything.

    Anyway, it is about one Group, two Visual editor fields not saving values in database.

    At the end it shows problem was my custom snippet I use on all websites to force owners to use same Title and slugs if they change Title.

    Problem is this snippet change ACF Group slug too to match Group Title. And for some to me unknown reasons Visual editor fields are not saving values then.

    Anyway here is the snippet, you should adapt somehow to avoid ACF Group slugs, or to be aware if you use it why it mess with fields saving:

    function myplugin_update_slug( $data, $postarr ) {
    if ( ! 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 );

    Or you can use snippet, but dont touch Save/Update button for ACF Group later.

  • What you need to do is first test $data or $postarr to make sure you’re modifying only the post types you want to modify. What you have here will modify the slug of every post type, which would include page, post, attachment, revision, nav_menu_item, and any custom post types added to your site.

  • 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 );
  • Only slightly shorter

    
    if (in_array($postarr['post_type'], array('post', 'page')) &&
        !in_array($data['post_status'], array('draft', 'pending', 'auto-draft'))) {
      $data['post_name'] = sanitize_title( $data['post_title'] );
    }
    
  • Just one note regarding this snippet. Gives huge headache because it allows Posts with same title/slugs. It doesnt add number on the end of slug if Posts have same Title.

    Example: post-title-2.

    If someone knows better snippet just write it here.

  • You’re going to need to add some more information that I don’t see in any of your code, but I found a function in WP that will do this https://codex.wordpress.org/Function_Reference/wp_unique_post_slug

    
    $data['post_name'] = wp_unique_post_slug(sanitize_title( $data['post_title'] ), $post_id, $post_status, $post_type, $post_parent);
    

    It appears that all of the arguments for this function are required and there’s not much information on using it.

  • Thank you. Seems as it works well:

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


    https://omelsoft.wordpress.com/2016/05/29/auto-generate-wordpress-slug-upon-saving-post/

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Fields not saving – Slug’ is closed to new replies.