在URL中使用加号“+”而不是破折号“-”

时间:2011-04-12 作者:Jaquis

是否可以使用加号而不是破折号作为url中的单词分隔符?如果是这样的话,具体怎么做?

例如。:www.example.com/some-title/ 成为www.example.com/some+title/

当然,理想情况下,它必须处理帖子、页面和自定义帖子。

2 个回复
SO网友:Chip Bennett

Mark Jaquith在他的尼斯搜索插件中使用“+”作为搜索查询字符串分隔符。Here\'s what he does:

function cws_nice_search_redirect() {
    if ( is_search() && strpos( $_SERVER[\'REQUEST_URI\'], \'/wp-admin/\' ) === false && strpos( $_SERVER[\'REQUEST_URI\'], \'/search/\' ) === false ) {
        wp_redirect( home_url( \'/search/\' . str_replace( array( \' \', \'%20\' ),  array( \'+\', \'+\' ), get_query_var( \'s\' ) ) ) );
        exit();
    }
}
也许这对我们有一些借鉴意义?

SO网友:T.Todua

不使用.HTACCESS 为了这个

它的疯狂,WP将与- 您的网站将在.htaccess... 相反,您需要修改url生成代码:

将此插入functions.php:

add_action( \'wp_ajax_sample-permalink\', \'MyajaxSamplePermalink\',1);
function MyajaxSamplePermalink($data) {
    // check that we\'re dealing with a product, and editing the slug
    $post_id = isset($_POST[\'post_id\']) ? intval($_POST[\'post_id\']) : 0;
    $new_title = isset($_POST[\'new_title\'])? $_POST[\'new_title\'] : null;
    $post_name = isset($_POST[\'new_slug\'])? $_POST[\'new_slug\'] : $new_title;
    //on first fire, there is not set the "new_slug"
    $_POST[\'new_slug\'] = ISSET($_POST[\'new_slug\']) ? $_POST[\'new_slug\'] : slug_modify($post_name); 
}
//disable slug beforehand Post Update  action (also, in navigation menus and etc...)
add_filter(\'name_save_pre\', \'MyfilterNameSavePre\');
function MyfilterNameSavePre($post_name) { 
    if (!empty($_POST[\'post_ID\']) || !empty($_POST[\'post_name\']) || !empty($_POST[\'post_title\']) ){
        // check that we\'re dealing with a product, and editing the slug
        $post_id = !empty($_POST[\'post_ID\']) ? intval($_POST[\'post_ID\']) : 0;
        $new_slug = !empty($_POST[\'post_name\']) ? $_POST[\'post_name\'] :  $_POST[\'post_title\'];
        //if got from new post
        if ($post_id && !empty($_POST[\'_wp_http_referer\']) ) {  if (stripos($_POST[\'_wp_http_referer\'],\'wp-admin/post-new.php\')!==false) { $post_name = slug_modify($new_slug); $_POST[\'post_name\']=$post_name;} } 
    }
    return $post_name;
}
//disable slug on any update
add_filter(\'wp_insert_post_data\', \'myappend_slug\', 3); function myappend_slug($dataaaaaa) { 
    if (!empty($_POST[\'_wp_http_referer\'])) {
        if (stripos($_POST[\'_wp_http_referer\'],\'wp-admin/post-new.php\')!==false) {
            $dataaaaaa[\'post_name\']=slug_modify(     (!empty($_POST[\'post_name\']) ? $_POST[\'post_name\'] :$dataaaaaa[\'post_title\'])      );
        } 
    }
    return $dataaaaaa; 
}
function slug_modify($slg) {return str_replace(\'-\',\'+\',$slg);}

结束

相关推荐