• Resolved signale

    (@signale)


    Hi!
    Could you please tell is there a way to output ( and ) as they are (currently they translate into %28 and %29). And the same for the %27(‘) – for me it’s like %2527 and I need only %27
    Thanks!

    • This topic was modified 6 years, 2 months ago by signale.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Maciej Bis

    (@mbis)

    Hi @signale,

    First of all you need to modify the function that sanitizes the special characters. To make sure that the brackets are not removed, please use this code snippet:

    function pm_keep_special_diactrics($regex) {
    	return str_replace('|+,', '|+,\'\’\(\)', $regex);
    }
    add_filter('permalink_manager_sanitize_regex', 'pm_keep_special_diactrics', 1);

    When it comes to URL decoding, I would recommend to keep them as they are. The special characters are encoded intentionally, so the browsers can safely parse them.

    Best regards,
    Maciej

    Thread Starter signale

    (@signale)

    Hello, Maciej.
    Thanks for the help.
    You’re right I’ve chosen the urldecode route and it lead nowhere.
    I’ve used the code and was able to make some progress. Thus I managed to display brackets (). But I noticed that it didn’t influence the way it outputs the url for the loop instances – on the Blog page brackets still are encoded. (I placed the code globally – not conditioned them just for the single post). Plus, what’s even worse – when I click to share – it shows the slug without the brackets at all – which means AddThis is not able to see brackets and perceives it as a different url from the original.
    The first post here:
    http://agency.smartleads.eu/
    it opens like http://agency.smartleads.eu/post/te%28st%29/
    Though it shows ok as http://agency.smartleads.eu/post/te(st)/ if you try to open it directly

    And another issue the ' symbol still refuses to be processed correctly. I need it as %27 and it just trims itself and gives http://agency.smartleads.eu/post/test%5C%272/ instead of http://agency.smartleads.eu/post/test%272/ (test2 post)

    If I use urldecode, still trying to share shows the url without special characters at all. And although the issue with the brackets on the loop resolve itself, the ' symbol shows as it is – not encoded %27

    So, why is it happening, why ' acts so inconsistently, why it can’t be just ordinary be encoded and gives spare codes? And how to make the url globally visible, so AddThis gets Permalink and sees the brackets version? Thanks a lot.

    Plugin Author Maciej Bis

    (@mbis)

    Hi again @signale,

    I am afraid that there is no simple solution here. The URLs are encoded to provide support for old browsers:
    http://xahlee.info/js/url_encoding_unicode.html

    The URLs are incorrect, because some 3rd party plugin or custom code snippet rewrites the URLs after they are rewritten by my plugin. You can see it in the HTML output:

    <link rel="canonical" href="http://agency.smartleads.eu/post/test" />

    Generally speaking, WordPress strips all the special characters because they are problematic. It is not a standard feature request. I wish I could help you, but I am afraid that I will not be able to provide you with a custom solution in this case.

    Thread Starter signale

    (@signale)

    Okay, I see, thanks anyways!

    Thread Starter signale

    (@signale)

    Hi again. This time I’m contributing and saving someone’s life in the future πŸ™‚

    So I actually managed to accomplish needed effect. And somehow it all fell in such way that even old browsers will be able to process it.

    So what I did was:
    1. Deactivate all Custom Permalinks plugins as for some reason they just don’t seem to influence on a native slug.
    2. Next I’ve just added

    if(strpos($permalink,'%28')!== false)
    			$permalink = urldecode($permalink);

    which checks if the permalink contains encoding and decodes it if it does.
    3. The same logic used for other symbols (like $)

    And this code must be implemented in the post_link filter.

    The entire code in a proper form for those in need

    function bis_post_permalinks($permalink, $post) {
    	if(!empty($post->post_type) && $post->post_type == 'post' && is_single()) {
    		$home_url = trim(get_home_url(), '/');
    
    		// Old format
    		if(strtotime($post->post_date) < strtotime('2020-02-15')) {
    			$permalink = add_query_arg('post', $post->ID, $home_url);
    		}
    		// New format
    		else {
    			$permalink = sprintf('%s/post/%s', $home_url, $post->post_name);
    			if(strpos($permalink,'%28')!== false)
    			$permalink = urldecode($permalink);
    		}
    	}
    	
    	return $permalink;
    }
    
    add_filter('post_link', 'bis_post_permalinks', 9999, 2);

    Remember it’s a custom case, all you need – the part with filter and strpos.

    This exact code won’t work, unless you set

    add_action('init', 'set_new_tag');
    function set_new_tag(){
    if(!empty($_GET['post']) && is_numeric($_GET['post'])) {
      $_GET['p'] = (int) $_GET['post'];
    }
    }

    in the beginning of functions.php

    In fact for some ordinary use you just do:

    function bis_post_permalinks($permalink, $post) {
    		if(strpos($permalink,'%28')!== false)
    			$permalink = urldecode($permalink);
    	
    	return $permalink;
    }
    
    add_filter('post_link', 'bis_post_permalinks', 9999, 2);
    Thread Starter signale

    (@signale)

    You can check here:
    http://agency.smartleads.eu/

    the test post works as default, then the test2 – decodes and the most important part, although it doesn’t redirect to the raw version (which is good for the old browsers), it still knows the urls, plus for some reason AddThis takes that native slug generated dynamically – isn’t that magic?

    Thread Starter signale

    (@signale)

    oh, forgot to mention, my native slug in WordPress looks like test%282%29 which later translates into test(2)

    • This reply was modified 6 years, 2 months ago by signale.
Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Literally () symbols and ‘ as they are’ is closed to new replies.