Plugin messes with social sharing buttons
-
Hello and thank you for this awesome plugin! I have a problem when using Auto Show as it appears that Secondary Title is messing with the social sharing buttons I use on my website.
More specifically, whenever Secondary Title is enabled the URL gets replaced with part of the title format, namely “brspan class=’secondary-title’ style=’color:” (that’s what appears instead of the page URL when trying to share the article on social media using the buttons on the website).
The code for one of the share buttons is:
// Get current page URL $bm_current_url = urlencode( get_permalink() ); // Construct sharing URL without using any script $twitter_url = 'https://twitter.com/intent/tweet?via=9to5linux&hashtags=Linux&text=' . $bm_post_title . '&url=' . $bm_current_url;I can share more code if necessary!
I’m using the Bam Pro theme. Is there a way to fix this issue? If I disable Secondary Title everything works normally, so I guess it injects some code that messes up existing code, but I have no idea where to look.
Thank you!
-
This topic was modified 3 years, 5 months ago by
nestormarius.
The page I need help with: [log in to see the link]
-
This topic was modified 3 years, 5 months ago by
-
Hi @nestormarius,
Well, then, let’s fix it together 🙂
I need two things:
1. The Site Health logs as .txt (Tools -> Site Health)
2. detailed instructions on how to reproduce the error in a local environmentThat’d probably be all.
Thank you for your quick reply. I’ve enabled the plugin to let you reproduce the bug, but I just discovered that if the secondary title field is not filled there’s no bug. If I write something in the secondary title field in the Gutenberg editor, the bug appears. So…
The plugin is now enabled on the site. Go to the article linked in my first post and click on the Tweet button to share it on Twitter. In the pop-up window, you’ll see the error (
brspan class='secondary-title' style='color:instead of the page URL).Go to this article (https://9to5linux.com/9to5linux-weekly-roundup-december-11th-2022) that doesn’t have a secondary title to see how the Tweet button should actually work even if Secondary Title is enabled.
I’ll post the Site Health logs shortly…
Sorry, but I don’t see any logs on the Site Health page. Only the site info is available to copy… maybe I’m missing something? It looks like WP_DEBUG_LOG is set to false, could that be the issue?
BTW, Secondary Title also messes with the RSS feeds. If a new post is published with a secondary title, the secondary title also appears next to the post’s title in the RSS feed. Any idea how to fix this?
-
This reply was modified 3 years, 5 months ago by
nestormarius.
Okay, this looks like an encoding problem. Do you have access to a child theme (bad method, but it’d fix the problem) or the plugin itself? What’s the name of the plugin and theme? I might be able to include them in the supported list for the future version.
Is there a way you can send me the theme via [email protected] so I can see how to fix it most easily?
Since I can’t entirely reproduce your WP installation, here’s an untested snippet that strips all HTML tags if you visit a single blog post. If you want to have it only on the Twitter sharing button, I need the theme/plugin that does that.
Frontpage, HTML enabled:
https://cln.sh/ernTavSingular page, HTML disabled:
https://cln.sh/vYb8hhI’ve commented on every line for better understanding. Place the following in the
functions.phpof your theme or child theme (better), and please let me know how it behaves./** * This filter will remove all manually set HTML tags from * the secondary title so it doesn't collude with features * from other plugins, i.e. Twitter sharing. * * @param string $secondary_title * @param int $post_id * * @since 2.1.0 * @test PHP >= 7.1 * * @more https://ww.wp.xz.cn/support/topic/plugin-messes-with-social-sharing-buttons/ */ add_filter( 'get_secondary_title', function ( string $secondary_title, int $post_id ) { // TODO: Check if theme/plugin is actually activated with /** We need some data from the post*/ $post = get_post( $post_id ); /** Check if the secondary title exists and the page is a singular post blog page (adjust if necessary) */ if ( $secondary_title && $post && is_singular( 'post' ) ) { /** Decode the HTML */ $secondary_title_html_entity = html_entity_decode( $secondary_title ); /** Strip all tags */ $secondary_title_without_tags = wp_strip_all_tags( $secondary_title_html_entity ); /** Polish the variable names ;) */ $secondary_title = $secondary_title_without_tags; } return (string) $secondary_title; }, 10, 2 );BTW: You were right regarding WP_DEBUG. Try this:
define("WP_DEBUG", true); define("WP_DEBUG_LOG", true); define("WP_DEBUG_DISPLAY", true); define("SCRIPT_DEBUG", true); @ini_set("display_errors", 1);You can download the log under
wp-admin/site-health.php?tab=debugand then hit the button “Copy site info to clipboard.”To the mods: How much longer must my replies, meant to support my own plugin and make it work for users, has to be manually published? I’m slowly losing interest in this community.
A more precise approach would be adding
global $postand use$post->IDas the first parameter ofget_secondary_title()to the, and then strip the tags in the code wherever you construct the Twitter share URL.-
This reply was modified 3 years, 5 months ago by
thaikolja.
There’s no plugin for the social buttons, they are implemented in the theme. The theme is called Bam Pro and it’s a commercial theme. You can check out a demo version on the official website (https://themezhut.com/demo/bam-pro/be-happy-for-this-moment-this-moment-is-your-life/). I’ll paste below the entire code for the social sharing buttons, but they are adjusted to my needs.
BTW, all the social sharing buttons are affected by this bug, not only the Twitter button. More specifically, the $bm_current_url variable is affected in some way when the page has a secondary title.
RSS issue I believe it’s similar.
/** * Social Sharing Icons. */ if ( ! function_exists( 'bam_social_sharing_buttons' ) ) { function bam_social_sharing_buttons( $container_class = "" ) { if ( ! is_singular() ) { return; } global $post; // Get current page URL $bm_current_url = urlencode( get_permalink() ); // Get current page title $bm_post_title = str_replace( ' ', '%20', get_the_title() ); $post_thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); $bm_share_thumbnail = ( $post_thumbnail ) ? $post_thumbnail[0] : ''; // Construct sharing URL without using any script $twitter_url = 'https://twitter.com/intent/tweet?via=9to5linux&hashtags=Linux&text=' . $bm_post_title . '&url=' . $bm_current_url; $facebook_url = 'https://www.facebook.com/sharer/sharer.php?u=' . $bm_current_url; $reddit_url = 'https://reddit.com/submit?url='. $bm_current_url .'&title='. $bm_post_title; $mastodon_url = 'https://toot.kytta.dev/?text='.$bm_post_title . '%20' . $bm_current_url; $linkedin_url = 'https://www.linkedin.com/shareArticle?mini=true&url='. $bm_current_url .'&title='. $bm_post_title; //$whatsapp_url = 'whatsapp://send?text='.$bm_post_title . ' ' . $bm_current_url; // Add sharing button at the end of page/page content $content = '<div class="bm-social-sharing '. esc_attr( $container_class ) .'">'; $content .= '<a href="'. esc_url( $twitter_url ) .'" target="_blank" rel="noopener"><i class="fab fa-twitter"></i>' . __( 'Tweet', 'bam-pro' ) . '</a>'; $content .= '<a href="'. esc_url( $mastodon_url ) .'" target="_blank" rel="noopener"><i class="fa fa-mastodon" style="padding: 0 5px 0 0;"></i>' . __( 'Toot', 'bam-pro' ) . '</a>'; $content .= '<a href="'. esc_url( $facebook_url ) .'" target="_blank" rel="noopener"><i class="fab fa-facebook-f" aria-hidden="true" style="padding: 0 0 0 5px;"></i>' . __( '', 'bam-pro' ) . '</a>'; $content .= '<a href="'. esc_url( $reddit_url ) .'" target="_blank" rel="noopener"><i class="fab fa-reddit-alien" aria-hidden="true" style="padding: 0 0 0 5px;"></i>' . __( '', 'bam-pro' ) . '</a>'; $content .= '<a href="'. esc_url( $linkedin_url ) .'" target="_blank" rel="noopener"><i class="fab fa-linkedin" aria-hidden="true" style="padding: 0 0 0 5px;"></i>' . __( '', 'bam-pro' ) . '</a>'; //$content .= '<a href="'. esc_url( $whatsapp_url ).'" target="_blank" rel="noopener"></a>'; $content .= '</div>'; echo $content; } }-
This reply was modified 3 years, 5 months ago by
nestormarius.
-
This reply was modified 3 years, 5 months ago by
nestormarius.
-
This reply was modified 3 years, 5 months ago by
nestormarius.
I’ve added the filter you send me to functions.php but it doesn’t work. The error persists. For other social buttons, the output is a bit different. For example, on the Toot (Mastodon) button the URL is replaced only with “brspan class”.
The Reddit sharing button on the other hand gets the URL, but the “brspan class” thing appears next to the title.
The Facebook and Linkdin share buttons work as expected (they’re not affected).
I did some more digging. If I remove the
$bm_post_titlevariable from the Twitter share button code ($twitter_url), the URL appears in the pop-up window, but not the post’s title (of course, cause we removed it).So my guess is that it’s not about the URL, it’s about the title, more specifically this code
$bm_post_title = str_replace( ' ', '%20', get_the_title() );. Can we edit this line somehow so that it doesn’t interact with theget_secondary_title()function?Why? Because the Facebook URL code doesn’t include the
$bm_post_titlevariable.Guess what? If I replace
get_the_title()withget_secondary_title()in the above line it works wonderfully, in the sense that the secondary title is read correctly by the Twitter URL code.-
This reply was modified 3 years, 5 months ago by
nestormarius.
-
This reply was modified 3 years, 5 months ago by
nestormarius.
I fixed the issue by implementing the secondary title manually in the theme. Thank you for your time and for this awesome and unique plugin!
Hi @nestormarius,
I’m having a look at it tomorrow or the day after, it’s way past midnight over here. But we’ll get it solved, don’t worry.
Is there no way I can get a temporary copy of the theme? That’d make things a lot easier since I’m basically supporting blindly.
-
This reply was modified 3 years, 5 months ago by
The topic ‘Plugin messes with social sharing buttons’ is closed to new replies.