[PRO] Share via email => Subject is not urlencoded

When I have any diacritical mark onto subject, how to URL encode the strings using UTF-8?

i.e. => What I get today, below:

href="mailto:?subject=O%20que%20é%20a%20Síndrome%20de%20Savant?&body=Ei,%20pensei%20que%20pudesse%20gostar%20disso!%20Veja%20este%20artigo%20assim%20que%20puder:%20https://neurosaber.com.br/o-que-e-a-sindrome-de-savant/"

source => https://neurosaber.com.br/o-que-e-a-sindrome-de-savant/

Hi There,

Please add the following code under functions.php file locates in your child theme:

add_action('init', 'fix_mail_share', 999999999999);
 
 function fix_mail_share () {
  remove_shortcode( 'x_share' );
remove_shortcode( 'cs_share' );
remove_shortcode( 'share' );

 add_shortcode( 'x_share', 'x_shortcode_share_v2' );
 add_shortcode( 'cs_share', 'x_shortcode_share_v2' );
  add_shortcode( 'share', 'x_shortcode_share_v2' );
 }
 
 function x_shortcode_share_v2( $atts ) {
  extract( shortcode_atts( array(
    'id'          => '',
    'class'       => '',
    'style'       => '',
    'title'       => '',
    'share_title' => '',
    'facebook'    => '',
    'twitter'     => '',
    'google_plus' => '',
    'linkedin'    => '',
    'pinterest'   => '',
    'reddit'      => '',
    'email'       => '',
    'email_subject' => ''
  ), $atts, 'x_share' ) );

  $share_url        = urlencode( get_permalink() );

  if ( is_singular() ) {
    $share_url = urlencode( get_permalink() );
  } else {
    global $wp;
    $share_url = urlencode( home_url( ($wp->request) ? $wp->request : '' ) );
  }

  if ( is_singular() ) {
    $share_title = ( $share_title    != '' ) ? esc_attr( $share_title ) : urlencode( get_the_title() );
  } else {
    $share_title = ( $share_title    != '' ) ? esc_attr( $share_title ) : urlencode( apply_filters( 'the_title', get_page( get_option( 'page_for_posts' ) )->post_title) );
  }

  $share_source     = urlencode( get_bloginfo( 'name' ) );
  $share_image_info = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
  $share_image      = ( function_exists( 'x_get_featured_image_with_fallback_url' ) ) ? urlencode( x_get_featured_image_with_fallback_url() ) : urlencode( $share_image_info[0] );

  if ( $linkedin    == 'true' ) {
    $share_content    = urlencode( cs_get_excerpt_for_social() );
  }

  $tooltip_attr = cs_generate_data_attributes_extra( 'tooltip', 'hover', 'bottom' );

  $id          = ( $id          != ''     ) ? 'id="' . esc_attr( $id ) . '"' : '';
  $class       = ( $class       != ''     ) ? 'x-entry-share ' . esc_attr( $class ) : 'x-entry-share';
  $style       = ( $style       != ''     ) ? 'style="' . $style . '"' : '';
  $title       = ( $title       != ''     ) ? $title : csi18n('shortcodes.share-title');
  $facebook    = ( $facebook    == 'true' ) ? "<a href=\"#share\" {$tooltip_attr} class=\"x-share\" title=\"" . csi18n('shortcodes.share-facebook') . "\" onclick=\"window.open('http://www.facebook.com/sharer.php?u={$share_url}&amp;t={$share_title}', 'popupFacebook', 'width=650, height=270, resizable=0, toolbar=0, menubar=0, status=0, location=0, scrollbars=0'); return false;\"><i class=\"x-icon-facebook-square\" data-x-icon=\"&#xf082;\"></i></a>" : '';
  $twitter     = ( $twitter     == 'true' ) ? "<a href=\"#share\" {$tooltip_attr} class=\"x-share\" title=\"" . csi18n('shortcodes.share-twitter') . "\" onclick=\"window.open('https://twitter.com/intent/tweet?text={$share_title}&amp;url={$share_url}', 'popupTwitter', 'width=500, height=370, resizable=0, toolbar=0, menubar=0, status=0, location=0, scrollbars=0'); return false;\"><i class=\"x-icon-twitter-square\" data-x-icon=\"&#xf081;\"></i></a>" : '';
  $google_plus = ( $google_plus == 'true' ) ? "<a href=\"#share\" {$tooltip_attr} class=\"x-share\" title=\"" . csi18n('shortcodes.share-google-plus') . "\" onclick=\"window.open('https://plus.google.com/share?url={$share_url}', 'popupGooglePlus', 'width=650, height=226, resizable=0, toolbar=0, menubar=0, status=0, location=0, scrollbars=0'); return false;\"><i class=\"x-icon-google-plus-square\" data-x-icon=\"&#xf0d4;\"></i></a>" : '';
  $linkedin    = ( $linkedin    == 'true' ) ? "<a href=\"#share\" {$tooltip_attr} class=\"x-share\" title=\"" . csi18n('shortcodes.share-linkedin') . "\" onclick=\"window.open('http://www.linkedin.com/shareArticle?mini=true&amp;url={$share_url}&amp;title={$share_title}&amp;summary={$share_content}&amp;source={$share_source}', 'popupLinkedIn', 'width=610, height=480, resizable=0, toolbar=0, menubar=0, status=0, location=0, scrollbars=0'); return false;\"><i class=\"x-icon-linkedin-square\" data-x-icon=\"&#xf08c;\"></i></a>" : '';
  $pinterest   = ( $pinterest   == 'true' ) ? "<a href=\"#share\" {$tooltip_attr} class=\"x-share\" title=\"" . csi18n('shortcodes.share-pinterest') . "\" onclick=\"window.open('http://pinterest.com/pin/create/button/?url={$share_url}&amp;media={$share_image}&amp;description={$share_title}', 'popupPinterest', 'width=750, height=265, resizable=0, toolbar=0, menubar=0, status=0, location=0, scrollbars=0'); return false;\"><i class=\"x-icon-pinterest-square\" data-x-icon=\"&#xf0d3;\"></i></a>" : '';
  $reddit      = ( $reddit      == 'true' ) ? "<a href=\"#share\" {$tooltip_attr} class=\"x-share\" title=\"" . csi18n('shortcodes.share-reddit') . "\" onclick=\"window.open('http://www.reddit.com/submit?url={$share_url}', 'popupReddit', 'width=875, height=450, resizable=0, toolbar=0, menubar=0, status=0, location=0, scrollbars=0'); return false;\"><i class=\"x-icon-reddit-square\" data-x-icon=\"&#xf1a2;\"></i></a>" : '';
  $email_subject = ( $email_subject != '' ) ? esc_attr( $email_subject ) : csi18n('shortcodes.share-email-subject');
  $mail_to     =  "mailto:?subject=" . get_the_title() . "&body=" . $email_subject . " " . get_permalink() . "" ;
  $email       = ( $email       == 'true' ) ? "<a href=\"{$mail_to}\" {$tooltip_attr} class=\"x-share email\" title=\"" . csi18n('shortcodes.share-email') . "\"><span><i class=\"x-icon-envelope-square\" data-x-icon=\"&#xf199;\"></i></span></a>" : '';


  $output = "<div {$id} class=\"{$class}\" {$style}>"
            . '<!--TEST--><p>' . $title . '</p>'
            . '<div class="x-share-options">'
              . $facebook . $twitter . $google_plus . $linkedin . $pinterest . $reddit . $email
            . '</div>'
          . '</div>';

  return $output;
}

March 2021 Update: The above code example has been adjusted to account for a change to a function name.

Unfortunately, doesn’t work.
What I’m doing wrong?
The Cloudflare cache have been cleared properly as well.
I have been insert in functions.php from my child theme, as first function.

The last function is:

// Add Social Sharing after the single post content
// =============================================================================

function x_add_social_sharing ( $content ) {
  if ( is_singular('post') ) {
    echo do_shortcode('[share title="Compartilhe este artigo" facebook="true" twitter="true" google_plus="true" linkedin="true" pinterest="true" email="true"]');
  }
}
add_action('x_before_the_content_end', 'x_add_social_sharing');

Checkout at => https://neurosaber.com.br/cat/artigos - at the end of any post.

Hi There,

Could you please post a screenshot with your issue ? as the email share is working on my end (https://snag.gy/UPJ7hX.jpg).

Thanks!

Hello,
Since initial post, the problem described, clearly, is: non utf-8 urlencoded character shown - on onpage, mouseover or inside message subject after click. Nothing about email link working or not, moreover, the mailto: is universal inasmuch protocol.

Hi there,

Yes, but it’s the limit of PHP’s urlencode() function. Could you try rawurlencode() instead? Simply edit the above code and replace all existence of urlencode(). Another thing (if this doesn’t work), there is a good snippet here http://php.net/manual/en/function.rawurlencode.php#100313 that you may able to use.

Example, add this code to your child theme’s functions.php

function mb_rawurlencode($url){
$encoded='';
$length=mb_strlen($url);
for($i=0;$i<$length;$i++){
$encoded.='%'.wordwrap(bin2hex(mb_substr($url,$i,1)),2,'%',true);
}
return $encoded;
}

Then replace all existence of urlencode() with mb_rawurlencode()

Thanks.

@Rad @thai @mldarshana

Thank you for your reply.

As a practical example, please, follow me @ https://neurosaber.com.br/disturbios-psicomotores-como-sintoma-de-transtorno-emocional/

We have the follow href into <a class="x-share email">

href="mailto:?subject=Distúrbios Psicomotores como sintoma de Transtorno Emocional&body=Ei, pensei que pudesse gostar disso! Veja este artigo assim que puder: https://neurosaber.com.br/disturbios-psicomotores-como-sintoma-de-transtorno-emocional/"

Unfortunately, using the code above, doesn’t work properly - pro-child functions.php. The result, I guess, would be Dist%C3%BArbios instead Distúrbios.

Note: By default, my WordPress database set, entirely, is: utf8mb4 and utf8mb4_unicode_ci.

So, we still have the word Distúrbios. Naturally, this word is Distúrbios - in portuguese. For HTML5 if would be Dist&uacute;rbios - I don’t know, but would be it better… suggestions? Anyway, a simple urlencode would give us Dist%C3%BArbios.

About &body= , the correct would be &amp;body= , no?

How can you help me about these things? What the best way to solve it?

Hi there,

Would you mind providing your admin login credentials on a secure note? I think the issue is, it’s already like that when pulled from the database. Hence, even with PHP process, it will be always like that. Example, when I directly changed it to Distúrbios instead of Distúrbios, it works perfectly. Hence, the issue is already there even before it’s rendered.

I like to create new post for further checking.

Thanks!

@Rad, I added a secure note in the message above and below. Thank you very much.

Hi There,

Thank you from the credentials. I have check and yes I can confirm @Rad conclusion that it has been like that from the database. See this: https://screencast-o-matic.com/watch/cbXDrH2rhL
Can you confirm that your database charset is the same with the charset set on wp-config.php file on the root folder of your installation?

This article might also help: https://www.maketecheasier.com/fix-foreign-characters-in-wordpress-issue/

@Lely and @Rad

Thank you very much for reply.
Please, check these credentials into secure note below.

Hi there,

I made some changes in your code and seems it’s okay now, could you try it too?

I applied the rawurlencode() as suggested :slight_smile:

Thanks!

1 Like

@Rad, thank you for write in.

Yes, it’s working fine now.
Which changes you have been made? I’d like to know where and how it was made… to learn more.

Thank you very much.

Hi there,

It’s this line

$mail_to = "mailto:?subject=" . rawurlencode( get_the_title() ) . "&body=" . $email_subject . " " . get_permalink() . "" ;

Cheers!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.