如何定制这种具有覆盖功能的自动段塞缩短器

时间:2010-09-17 作者:NetConstructor.com

我正在使用一段巧妙的小代码,我已经将其包含在我的函数文件中,它会自动获取输入到新帖子标题中的任何信息,并创建一个自动删除特定停止词的slug(代码包含在下面)。

问题是,有时在自动创建这个slug之后,我想单击slug(在标题下)并直接修改它。

我向社区提出的问题是。。。我需要在下面的代码中更改什么,以便当我单击slug(标题下)并修改它时,无论我在那里输入什么,都可以使用,而不需要下面的函数来阻止这种情况的发生。代码如下:

// AUTOMATICALLY SANITIZE PAGE/POST SEO SLUG FROM SHORT WORDS
// SOURCE: http://www.vretoolbar.com/news/seo-slugs-wordpress-plugin
   add_filter(\'name_save_pre\', \'seo_slugs\', 0);
   function seo_slugs($slug) {
    // We don\'t want to change an existing slug
 if ($slug) return $slug;
 global $wpdb;
 $seo_slug = strtolower(stripslashes($_POST[\'post_title\']));
 $seo_slug = preg_replace(\'/&.+?;/\', \'\', $seo_slug); // kill HTML entities
    // kill anything that is not a letter, digit, space or apostrophe
 $seo_slug = preg_replace ("/[^a-zA-Z0-9 \\\']/", "", $seo_slug);
    // Turn it to an array and strip common words by comparing against c.w. array
 $seo_slug_array = array_diff (split(" ", $seo_slug), seo_slugs_stop_words());
    // Turn the sanitized array into a string
 $seo_slug = join("-", $seo_slug_array);
 return $seo_slug;
   }
   function seo_slugs_stop_words () {
   return array ("a", "able", "about", "above", "abroad", "according", "accordingly", "across", "actually", "adj", "after", "afterwards", "again", "against", "ago", "ahead", "ain\'t", "all", "allow", "allows", "almost", "alone", "along", "alongside", "already", "also", "although", "always", "am", "amid", "amidst", "among", "amongst", "an", "and", "another", "any", "anybody", "anyhow", "anyone", "anything", "anyway", "anyways", "anywhere", "apart", "appear", "appreciate", "appropriate", "are", "aren\'t", "around", "as", "a\'s", "aside", "ask", "asking", "associated", "at", "available", "away", "awfully", "b", "back", "backward", "backwards", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "begin", "behind", "being", "believe", "below", "beside", "besides", "best", "better", "between", "beyond", "both", "brief", "but", "by", "c", "came", "can", "cannot", "cant", "can\'t", "caption", "cause", "causes", "certain", "certainly", "changes", "clearly", "c\'mon", "co", "co.", "com", "come", "comes", "concerning", "consequently", "consider", "considering", "contain", "containing", "contains", "corresponding", "could", "couldn\'t", "course", "c\'s", "currently", "d", "dare", "daren\'t", "definitely", "described", "despite", "did", "didn\'t", "different", "directly", "do", "does", "doesn\'t", "doing", "done", "don\'t", "down", "downwards", "during", "e", "each", "edu", "eg", "eight", "eighty", "either", "else", "elsewhere", "end", "ending", "enough", "entirely", "especially", "et", "etc", "even", "ever", "evermore", "every", "everybody", "everyone", "everything", "everywhere", "ex", "exactly", "example", "except", "f", "fairly", "far", "farther", "few", "fewer", "fifth", "first", "five", "followed", "following", "follows", "for", "forever", "former", "formerly", "forth", "forward", "found", "four", "from", "further", "furthermore", "g", "get", "gets", "getting", "given", "gives", "go", "goes", "going", "gone", "got", "gotten", "greetings", "h", "had", "hadn\'t", "half", "happens", "hardly", "has", "hasn\'t", "have", "haven\'t", "having", "he", "he\'d", "he\'ll", "hello", "help", "hence", "her", "here", "hereafter", "hereby", "herein", "here\'s", "hereupon", "hers", "herself", "he\'s", "hi", "him", "himself", "his", "hither", "hopefully", "how", "howbeit", "however", "hundred", "i", "i\'d", "ie", "if", "ignored", "i\'ll", "i\'m", "immediate", "in", "inasmuch", "inc", "inc.", "indeed", "indicate", "indicated", "indicates", "inner", "inside", "insofar", "instead", "into", "inward", "is", "isn\'t", "it", "it\'d", "it\'ll", "its", "it\'s", "itself", "i\'ve", "j", "just", "k", "keep", "keeps", "kept", "know", "known", "knows", "l", "last", "lately", "later", "latter", "latterly", "least", "less", "lest", "let", "let\'s", "like", "liked", "likely", "likewise", "little", "look", "looking", "looks", "low", "lower", "ltd", "m", "made", "mainly", "make", "makes", "many", "may", "maybe", "mayn\'t", "me", "mean", "meantime", "meanwhile", "merely", "might", "mightn\'t", "mine", "minus", "miss", "more", "moreover", "most", "mostly", "mr", "mrs", "much", "must", "mustn\'t", "my", "myself", "n", "name", "namely", "nd", "near", "nearly", "necessary", "need", "needn\'t", "needs", "neither", "never", "neverf", "neverless", "nevertheless", "new", "next", "nine", "ninety", "no", "nobody", "non", "none", "nonetheless", "noone", "no-one", "nor", "normally", "not", "nothing", "notwithstanding", "novel", "now", "nowhere", "o", "obviously", "of", "off", "often", "oh", "ok", "okay", "old", "on", "once", "one", "ones", "one\'s", "only", "onto", "opposite", "or", "other", "others", "otherwise", "ought", "oughtn\'t", "our", "ours", "ourselves", "out", "outside", "over", "overall", "own", "p", "particular", "particularly", "past", "per", "perhaps", "placed", "please", "plus", "possible", "presumably", "probably", "provided", "provides", "q", "que", "quite", "qv", "r", "rather", "rd", "re", "really", "reasonably", "recent", "recently", "regarding", "regardless", "regards", "relatively", "respectively", "right", "round", "s", "said", "same", "saw", "say", "saying", "says", "second", "secondly", "see", "seeing", "seem", "seemed", "seeming", "seems", "seen", "self", "selves", "sensible", "sent", "serious", "seriously", "seven", "several", "shall", "shan\'t", "she", "she\'d", "she\'ll", "she\'s", "should", "shouldn\'t", "since", "six", "so", "some", "somebody", "someday", "somehow", "someone", "something", "sometime", "sometimes", "somewhat", "somewhere", "soon", "sorry", "specified", "specify", "specifying", "still", "sub", "such", "sup", "sure", "t", "take", "taken", "taking", "tell", "tends", "th", "than", "thank", "thanks", "thanx", "that", "that\'ll", "thats", "that\'s", "that\'ve", "the", "their", "theirs", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "there\'d", "therefore", "therein", "there\'ll", "there\'re", "theres", "there\'s", "thereupon", "there\'ve", "these", "they", "they\'d", "they\'ll", "they\'re", "they\'ve", "thing", "things", "think", "third", "thirty", "this", "thorough", "thoroughly", "those", "though", "three", "through", "throughout", "thru", "thus", "till", "to", "together", "too", "took", "toward", "towards", "tried", "tries", "truly", "try", "trying", "t\'s", "twice", "two", "u", "un", "under", "underneath", "undoing", "unfortunately", "unless", "unlike", "unlikely", "until", "unto", "up", "upon", "upwards", "us", "use", "used", "useful", "uses", "using", "usually", "v", "value", "various", "versus", "very", "via", "viz", "vs", "w", "want", "wants", "was", "wasn\'t", "way", "we", "we\'d", "welcome", "well", "we\'ll", "went", "were", "we\'re", "weren\'t", "we\'ve", "what", "whatever", "what\'ll", "what\'s", "what\'ve", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "where\'s", "whereupon", "wherever", "whether", "which", "whichever", "while", "whilst", "whither", "who", "who\'d", "whoever", "whole", "who\'ll", "whom", "whomever", "who\'s", "whose", "why", "will", "willing", "wish", "with", "within", "without", "wonder", "won\'t", "would", "wouldn\'t", "x", "y", "yes", "yet", "you", "you\'d", "you\'ll", "your", "you\'re", "yours", "yourself", "yourselves", "you\'ve", "z", "zero");
   }

3 个回复
最合适的回答,由SO网友:NetConstructor.com 整理而成

最后,我在函数中使用了以下代码。获取所需功能的php文件:

// AUTOMATICALLY SANITIZE PAGE/POST SEO SLUG FROM SHORT WORDS
// SOURCE: http://www.vretoolbar.com/news/seo-slugs-wordpress-plugin
   add_filter(\'name_save_pre\', \'seo_slugs\', 0);
   function seo_slugs($slug) {
    // We don\'t want to change an existing slug
    if ($slug) return $slug;
    global $wpdb;
    $seo_slug = strtolower(stripslashes($_POST[\'post_title\']));
    $seo_slug = preg_replace(\'/&.+?;/\', \'\', $seo_slug); // kill HTML entities
    // kill anything that is not a letter, digit, space or apostrophe
    $seo_slug = preg_replace ("/[^a-zA-Z0-9 \\\']/", "", $seo_slug);
    // Turn it to an array and strip common words by comparing against c.w. array
    $seo_slug_array = array_diff (explode(" ", $seo_slug), seo_slugs_stop_words());
    // Turn the sanitized array into a string
    $seo_slug = join("-", $seo_slug_array);
    return $seo_slug;
   }
   function seo_slugs_stop_words () {
   return array ("a", "able", "about", "above", "abroad", "according", "accordingly", "across", "actually", "adj", "after", "afterwards", "again", "against", "ago", "ahead", "ain\'t", "all", "allow", "allows", "almost", "alone", "along", "alongside", "already", "also", "although", "always", "am", "amid", "amidst", "among", "amongst", "an", "and", "another", "any", "anybody", "anyhow", "anyone", "anything", "anyway", "anyways", "anywhere", "apart", "appear", "appreciate", "appropriate", "are", "aren\'t", "around", "as", "a\'s", "aside", "ask", "asking", "associated", "at", "available", "away", "awfully", "b", "back", "backward", "backwards", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "begin", "behind", "being", "believe", "below", "beside", "besides", "best", "better", "between", "beyond", "both", "brief", "but", "by", "c", "came", "can", "cannot", "cant", "can\'t", "caption", "cause", "causes", "certain", "certainly", "changes", "clearly", "c\'mon", "co", "co.", "com", "come", "comes", "concerning", "consequently", "consider", "considering", "contain", "containing", "contains", "corresponding", "could", "couldn\'t", "course", "c\'s", "currently", "d", "dare", "daren\'t", "definitely", "described", "despite", "did", "didn\'t", "different", "directly", "do", "does", "doesn\'t", "doing", "done", "don\'t", "down", "downwards", "during", "e", "each", "edu", "eg", "eight", "eighty", "either", "else", "elsewhere", "end", "ending", "enough", "entirely", "especially", "et", "etc", "even", "ever", "evermore", "every", "everybody", "everyone", "everything", "everywhere", "ex", "exactly", "example", "except", "f", "fairly", "far", "farther", "few", "fewer", "fifth", "first", "five", "followed", "following", "follows", "for", "forever", "former", "formerly", "forth", "forward", "found", "four", "from", "further", "furthermore", "g", "get", "gets", "getting", "given", "gives", "go", "goes", "going", "gone", "got", "gotten", "greetings", "h", "had", "hadn\'t", "half", "happens", "hardly", "has", "hasn\'t", "have", "haven\'t", "having", "he", "he\'d", "he\'ll", "hello", "help", "hence", "her", "here", "hereafter", "hereby", "herein", "here\'s", "hereupon", "hers", "herself", "he\'s", "hi", "him", "himself", "his", "hither", "hopefully", "how", "howbeit", "however", "hundred", "i", "i\'d", "ie", "if", "ignored", "i\'ll", "i\'m", "immediate", "in", "inasmuch", "inc", "inc.", "indeed", "indicate", "indicated", "indicates", "inner", "inside", "insofar", "instead", "into", "inward", "is", "isn\'t", "it", "it\'d", "it\'ll", "its", "it\'s", "itself", "i\'ve", "j", "just", "k", "keep", "keeps", "kept", "know", "known", "knows", "l", "last", "lately", "later", "latter", "latterly", "least", "less", "lest", "let", "let\'s", "like", "liked", "likely", "likewise", "little", "look", "looking", "looks", "low", "lower", "ltd", "m", "made", "mainly", "make", "makes", "many", "may", "maybe", "mayn\'t", "me", "mean", "meantime", "meanwhile", "merely", "might", "mightn\'t", "mine", "minus", "miss", "more", "moreover", "most", "mostly", "mr", "mrs", "much", "must", "mustn\'t", "my", "myself", "n", "name", "namely", "nd", "near", "nearly", "necessary", "need", "needn\'t", "needs", "neither", "never", "neverf", "neverless", "nevertheless", "new", "next", "nine", "ninety", "no", "nobody", "non", "none", "nonetheless", "noone", "no-one", "nor", "normally", "not", "nothing", "notwithstanding", "novel", "now", "nowhere", "o", "obviously", "of", "off", "often", "oh", "ok", "okay", "old", "on", "once", "one", "ones", "one\'s", "only", "onto", "opposite", "or", "other", "others", "otherwise", "ought", "oughtn\'t", "our", "ours", "ourselves", "out", "outside", "over", "overall", "own", "p", "particular", "particularly", "past", "per", "perhaps", "placed", "please", "plus", "possible", "presumably", "probably", "provided", "provides", "q", "que", "quite", "qv", "r", "rather", "rd", "re", "really", "reasonably", "recent", "recently", "regarding", "regardless", "regards", "relatively", "respectively", "right", "round", "s", "said", "same", "saw", "say", "saying", "says", "second", "secondly", "see", "seeing", "seem", "seemed", "seeming", "seems", "seen", "self", "selves", "sensible", "sent", "serious", "seriously", "seven", "several", "shall", "shan\'t", "she", "she\'d", "she\'ll", "she\'s", "should", "shouldn\'t", "since", "six", "so", "some", "somebody", "someday", "somehow", "someone", "something", "sometime", "sometimes", "somewhat", "somewhere", "soon", "sorry", "specified", "specify", "specifying", "still", "sub", "such", "sup", "sure", "t", "take", "taken", "taking", "tell", "tends", "th", "than", "thank", "thanks", "thanx", "that", "that\'ll", "thats", "that\'s", "that\'ve", "the", "their", "theirs", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "there\'d", "therefore", "therein", "there\'ll", "there\'re", "theres", "there\'s", "thereupon", "there\'ve", "these", "they", "they\'d", "they\'ll", "they\'re", "they\'ve", "thing", "things", "think", "third", "thirty", "this", "thorough", "thoroughly", "those", "though", "three", "through", "throughout", "thru", "thus", "till", "to", "together", "too", "took", "toward", "towards", "tried", "tries", "truly", "try", "trying", "t\'s", "twice", "two", "u", "un", "under", "underneath", "undoing", "unfortunately", "unless", "unlike", "unlikely", "until", "unto", "up", "upon", "upwards", "us", "use", "used", "useful", "uses", "using", "usually", "v", "value", "various", "versus", "very", "via", "viz", "vs", "w", "want", "wants", "was", "wasn\'t", "way", "we", "we\'d", "welcome", "well", "we\'ll", "went", "were", "we\'re", "weren\'t", "we\'ve", "what", "whatever", "what\'ll", "what\'s", "what\'ve", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "where\'s", "whereupon", "wherever", "whether", "which", "whichever", "while", "whilst", "whither", "who", "who\'d", "whoever", "whole", "who\'ll", "whom", "whomever", "who\'s", "whose", "why", "will", "willing", "wish", "with", "within", "without", "wonder", "won\'t", "would", "wouldn\'t", "x", "y", "yes", "yet", "you", "you\'d", "you\'ll", "your", "you\'re", "yours", "yourself", "yourselves", "you\'ve", "z", "zero");
   }
还请注意,您需要启用SLUG元数据库才能使其工作。由于我不想显示屏幕选项或实际的元框,我在管理样式表中添加了以下内容以删除它们:

[for="slugdiv-hide"] { 
    display: none; 
    }

#slugdiv { 
    display: none; 
    }

SO网友:hakre

这只是第一个想法:检查它是否是AJAX请求。

if ( defined( \'DOING_AJAX\' ) && DOING_AJAX ) 
    return $slug;
或:

if (
       isset( $_POST[\'action\'] ) 
       && \'sample-permalink\' === $_POST[\'action\']
   ) return $slug;
取决于所需的详细程度。

如果你把它放在函数的开头(可能在第一次测试下面if ($slug) return $slug; 然后函数应该退出。

如果还想过滤输入,那么对于AJAX请求,需要从$_POST[\'new_title\'] 而不是$_POST[\'post_title\']. 这就是它通过AJAX编辑删除输入的原因。

SO网友:T.Todua

您应该使用wp_ajax_sample-permalink actionname_save_pre filter.

add_action( \'wp_ajax_sample-permalink\', \'MyajaxSamplePermalink\',0);     function MyajaxSamplePermalink($data) {
    $post_id = isset($_POST[\'post_id\']) ? intval($_POST[\'post_id\']) : 0;
    $post_name = isset($_POST[\'new_slug\'])? $_POST[\'new_slug\'] : null;
    $_POST[\'new_slug\'] = YOUT_SLUG_MODIFY_FUNCTION($post_name);
}

add_filter(\'name_save_pre\', \'filterNameSavePre\');       function filterNameSavePre($post_name) {
    $post_id = isset($_POST[\'post_ID\']) ? intval($_POST[\'post_ID\']) : 0;
    $new_slug = isset($_POST[\'post_name\']) ? $_POST[\'post_name\'] : 0;
    $post_name = YOUT_SLUG_MODIFY_FUNCTION($new_slug);
    return $post_name;
}
并定义所需的功能

function YOUT_SLUG_MODIFY_FUNCTION($input){
      //........your codes.......
      return $input;
}

结束

相关推荐

WP-ADMIN似乎正在重定向

我的w-admin登录有一个奇怪的问题。这是从我升级到3.0以后才开始的,当我转到wp admin时,登录表单显示正常,但当我输入用户名并通过时,每次都会再次显示登录表单。使用密码恢复功能会导致电子邮件未找到错误。我知道用户名密码和电子邮件是正确的,b/c我可以访问mysql数据库,我可以看到值(至少用户名和电子邮件) 有人知道会出什么问题吗