最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why does post-slug not get saved for drafts?

programmeradmin1浏览0评论

I am trying to get the value of the 'new-post-slug' (the bit that you can edit to customize your slug for a post, but bizarrely doesn't have a 'name' attribute as you'd expect a form field to have ... the update must be done in ajax in the background)

What I am stuck on is 2 things:

  • Why do posts not get their post_name (slug) updated on saving a draft? (not autosaving, I mean hitting the 'Save Draft' button)

  • If I was to try and do this manually (using the save_post hook), how can I get the value of the 'new-post-slug' (since this field is not available in the $_POST var). I guess I could try and do a pre-form-submit jQuery append-to-form type thing which gets this field .

When using the wp_handle_upload_prefilter to catch the filename before it gets uploaded, the post_name (the slug) is not set:

add_filter('wp_generate_attachment_metadata', 'check_images_file_to_slug', 10, 2);

public function check_images_file_to_slug($image_info)
{  
    // Get the parent post ID, if there is one
    if( isset($_GET['post_id']) ) {
        $post_id = $_GET['post_id'];
    } elseif( isset($_POST['post_id']) ) {
        $post_id = $_POST['post_id'];
    }

    // Only do this if we got the post ID--otherwise they're probably in
    //  the media section rather than uploading an image from a post.
    if(is_numeric($post_id)) {
        /* @var @post WP_Post */
        $post = get_post($post_id);

        // Here $post->post_name (the slug), is empty for post-new.php (Adding new post)
    }
}

I am trying to get the value of the 'new-post-slug' (the bit that you can edit to customize your slug for a post, but bizarrely doesn't have a 'name' attribute as you'd expect a form field to have ... the update must be done in ajax in the background)

What I am stuck on is 2 things:

  • Why do posts not get their post_name (slug) updated on saving a draft? (not autosaving, I mean hitting the 'Save Draft' button)

  • If I was to try and do this manually (using the save_post hook), how can I get the value of the 'new-post-slug' (since this field is not available in the $_POST var). I guess I could try and do a pre-form-submit jQuery append-to-form type thing which gets this field .

When using the wp_handle_upload_prefilter to catch the filename before it gets uploaded, the post_name (the slug) is not set:

add_filter('wp_generate_attachment_metadata', 'check_images_file_to_slug', 10, 2);

public function check_images_file_to_slug($image_info)
{  
    // Get the parent post ID, if there is one
    if( isset($_GET['post_id']) ) {
        $post_id = $_GET['post_id'];
    } elseif( isset($_POST['post_id']) ) {
        $post_id = $_POST['post_id'];
    }

    // Only do this if we got the post ID--otherwise they're probably in
    //  the media section rather than uploading an image from a post.
    if(is_numeric($post_id)) {
        /* @var @post WP_Post */
        $post = get_post($post_id);

        // Here $post->post_name (the slug), is empty for post-new.php (Adding new post)
    }
}
Share Improve this question edited Oct 22, 2015 at 6:08 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Apr 4, 2015 at 2:22 gvantogvanto 1431 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I run into a similar issue. I had to remove remove_meta_box('slugdiv', ['post', 'page'], 'normal'); from my code. When you remove the slugdiv meta box the slug edit box for WP does stops working.

When a new post is created (regardless of status) post_name which is the slug is saved to the WP_Post object.

You can fire off a wp_ajax_$youraction() in the backend to get the post object and return the slug.

发布评论

评论列表(0)

  1. 暂无评论