Advertisement
websupporter

save_post action hook

Jun 5th, 2015
11,933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. <?php
  2. //Diese URLs aus der Metabox müssen noch abgespeichert werden
  3. add_action( 'save_post', 'us_save_post' );
  4. function us_save_post( $post_id ){
  5.     //Wir prüfen zunächst, ob wir unsere URL
  6.     //abspeichern wollen und können
  7.     if(
  8.         ! isset( $_POST['us_nonce'] ) ||
  9.         ! wp_verify_nonce( $_POST['us_nonce'], 'save-url' ) ||
  10.         ! current_user_can( 'edit_post', $post_id ) ||
  11.         ! isset( $_POST['url'] )  ||
  12.         wp_is_post_revision( $post_id )
  13.     )
  14.         return;
  15.  
  16.     //Wir reorganisieren unsere URL, um sie URL-konform zu machen.
  17.     //So werden wir beispielsweise Anführungszeichen in %22 umwandeln.
  18.     $url = parse_url( urldecode( stripslashes( $_POST['url'] ) ) );
  19.  
  20.     if( ! isset( $url['scheme'] ) || ! isset( $url['host'] ) )
  21.         return;
  22.    
  23.     $url_string = $url['scheme'] . '://';
  24.     if( isset( $url['user'] ) && isset( $url['password'] ) )
  25.         $url_string .= $url['user'] . ':' . $url['password'] . '.';
  26.  
  27.     $url_string .= $url['host'];
  28.     if( isset( $url['port'] ) )
  29.         $url_string .= ':' . $url['port'];
  30.     if( isset( $url['path'] ) )
  31.         $url_string .= $url['path'];
  32.  
  33.     if( isset( $url['query'] ) ){
  34.         $queries = explode( '&', $url['query'] );
  35.         foreach( $queries as $key => $val ){
  36.             $query = explode( '=', $val );
  37.             $query_key = $query[0];
  38.             unset( $query[0] );
  39.             $query_value = urlencode( implode( '=', $query ) );
  40.             $queries[ $key ] = $query_key . '=' . $query_value;
  41.         }
  42.         $url_string .= '?' . implode( '&', $queries );
  43.     }
  44.  
  45.     if( isset( $url['fragment'] ) )
  46.         $url_string .= '#' . urlencode( $url['fragment'] ) ;
  47.  
  48.  
  49.     //Wir speichern unsere URL
  50.     update_post_meta( $post_id, 'url', $url_string );
  51. }
  52. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement