Advertisement
krzxsiek

wordpress slug checker

Mar 14th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. function disallow_posts_with_same_name($messages) {
  2.     global $post;
  3.     global $wpdb;
  4.     $name = $post->post_name;
  5.     $post_id = $post->ID;
  6.     $wtquery_post = "SELECT post_name FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_name = '{$name}' AND ID != {$post_id}";
  7.     $wtquery_page = "SELECT post_name FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'page' AND post_name = '{$name}' AND ID != {$post_id}";
  8.  
  9.     $wresults_post = $wpdb->get_results($wtquery_post);
  10.     $wresults_page = $wpdb->get_results($wtquery_page);
  11.  
  12.     if ( $wresults_post || $wresults_page ) {
  13.         $error_message = 'Bezpośredni odnośnik o tej samej nazwie już istnieje. Zmień go aby uniknąć problemów z duplikowaniem się adresów.';
  14.         add_settings_error('post_has_links', '', $error_message, 'error');
  15.         settings_errors( 'post_has_links' );
  16.         $post->post_status = 'draft';
  17.         wp_update_post($post);
  18.         return;
  19.     }
  20.     return $messages;
  21.  
  22. }
  23. add_action('post_updated_messages', 'disallow_posts_with_same_name');
  24.  
  25. function disallow_posts_with_same_name_js() {
  26.     echo '"<script>
  27.            (function($) {
  28.                $(document).ready(function(){
  29.                    if ( $( "#setting-error-sname" ).length ) {
  30.                        $( "#setting-error-sname, #editable-post-name" ).animate({backgroundColor:"#dc3232",color:"#fff",padding:"2px 4px"},\'slow\');
  31.                    }
  32.                });
  33.            })( jQuery );
  34.           </script>"';
  35. }
  36. add_action('admin_footer', 'disallow_posts_with_same_name_js');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement