[wp-trac] [WordPress Trac] #29847: get_default_post_to_edit() stomps on assigned post_name

WordPress Trac noreply at wordpress.org
Thu Oct 2 23:25:06 UTC 2014


#29847: get_default_post_to_edit() stomps on assigned post_name
-------------------------------+-----------------------
 Reporter:  danielbachhuber    |      Owner:
     Type:  defect (bug)       |     Status:  new
 Priority:  normal             |  Milestone:  4.1
Component:  Posts, Post Types  |    Version:
 Severity:  normal             |   Keywords:  has-patch
  Focuses:                     |
-------------------------------+-----------------------
 When creating a new post, I'm assigning a secure `post_name` via the
 `wp_insert_post_data` filter:

 {{{
 /*
  * Generate a secure hash for our responses
  */
 add_filter( 'wp_insert_post_data', function( $post_data ){

         if ( 'my_post_type' !== $post_data['post_type'] ) {
                 return $post_data;
         }

         if ( empty( $post_data['post_name'] ) ) {

                 $secure_hash_values = array();
                 foreach( array( 'AUTH_KEY', 'SECURE_AUTH_KEY',
 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT',
 'LOGGED_IN_SALT', 'NONCE_SALT' ) as $constant ) {
                         if ( defined( $constant ) ) {
                                 $secure_hash_values[] = constant(
 $constant );
                         }
                 }

                 $secure_hash_values[] = time();
                 shuffle( $secure_hash_values );
                 $post_data['post_name'] = hash( 'sha256', serialize(
 $secure_hash_values ) );
         }

         return $post_data;
 });
 }}}

 `get_default_post_to_edit()` stomps on my `post_name` in `post-new.php`
 context because it blindly resets the value in the database.

 This hack properly resets the post object from database:

 {{{
 /*
  * Hack to restore the post_name that get_default_post_to_edit() noops
  * Our assigned post_name via wp_insert_post_data can get fubar on post-
 new.php
  *
  * @see https://core.trac.wordpress.org/browser/trunk/src/wp-
 admin/includes/post.php#L588
  */
 add_filter( 'post_updated_messages', function( $messages ) {
         global $pagenow, $post;

         if ( 'post-new.php' == $pagenow && empty( $post->post_name ) ) {
                 $post = get_post( $post->ID );
         }
         return $messages;
 });
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29847>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list