Problem with special characters in menus.

Special characters in menu widgets are showing weird additional characters, example:

This is only happening for menu items and in content it works fine.

It's also specific when using different dash, for example this will not work:
Living Water Smart – BC's Water Plan
While this does work fine:
Living Water Smart - BC's Water Plan

It doesn't look like theme issue because Predrag tested the theme on his site and there both of the examples work fine.

wp-config has UTF8 set and DB charset is utf_general_ci.

  • JP

    The odd characters are caused by the Menu Section Titles plugin, disabling it solves the problem.

    This is the function from this plugin:

    // wp recommended security precaution
    defined('ABSPATH') or die('No script kiddies please!');
    
    add_filter( 'wp_nav_menu_items', 'zw_menu_section_titles', 10, 2 );
    
    /**
     *
     */
    function zw_menu_section_titles ( $items, $args ) {
    
    	$dom = new DOMDocument();
    
    	$dom->loadHTML( $items );
    
    	$tags = $dom->getElementsByTagName('a');
    
    	// loop through all a tags
    	foreach($tags as $tag) {
    
    		// set the href to a var
    		$tag_href = $tag->getAttribute('href');
    
    		// get the hash
    		$tag_base = explode("#", $tag_href);
    
    		if( count($tag_base) > 1 ) {
    			// set it to what we care about
    			$tag_base = $tag_base[1];
    
    			// get string between parenthsis
    			preg_match('#\((.*?)\)#', $tag_base, $match);
    
    			// check to see if there were parenthesis
    			if( isset($match[1]) && !empty($match[1]) ) {
    				// create the new element
    				$new_element = $dom->createElement($match[1], $tag->nodeValue);
    			}
    		}
    
    		// make sure we have a new element and it isnt empty
    		if( isset($new_element) && $new_element != '' ) {
    			// replace the tag
    			$tag->parentNode->replaceChild($new_element, $tag);
    
    			// reset the var so it doesnt trigger by accident
    			$new_element = '';
    		}
    	}
    
    	// return the string
    	return $dom->saveHTML();
    }

    Anyone knows how to alter the script so it doesn't cause odd characters?

    What I need at the moment are h2 titles that don't get wrapped in an < a > link, this is the only plugin that seem to be able to do this. Note: this is only happening in the WordPress menus.

    Found this resource but it doesn't seem to work (and breaks all menus): https://wordpress.org/support/topic/support-for-utf8-and-non-htmlbody-tags-in-output/

  • James Morris

    Hello JP,

    I hope you are well today. I'll be following up with you regarding your chat.

    I've been looking into this issue extensively and I believe the source of the problem to be in the database. Essentially, there are a large number of left/right curly quotes and emdashes on the site that are not being interpreted correctly. This could be a character set issue on the DB or something related to the theme.

    However, since this is a live site, it's a bit difficult to fully test these scenarios out. I see in the chat transcript you have a staging server where we could be a bit more aggressive with our tests. Would you please sync the production site over to your staging site and provide me with access to the staging site so I can dig into this further for you?

    Please visit the Contact page and complete the form with the following information:

    Subject: "Attn: James Morris"

    In the Message box, please provide the following:

    - link back to this thread for reference
    - any other relevant urls

    - Admin login:
    Admin username
    Admin password
    Login url

    - Hosting Control Panel Login
    Admin username
    Admin password
    Login url

    ~OR~

    - FTP credentials
    host
    username
    password
    (and port if required)

    Best regards,

    James Morris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.