简体   繁体   English

Wordpress PHP | get_permalink() 剥离斜线。 不能添加额外的斜线

[英]Wordpress PHP | get_permalink() stripping slashes. Cant add extra slash

I have social media buttons to share a URL.我有社交媒体按钮来分享 URL。 My code looks something like我的代码看起来像

$socialURL = get_permalink();

//trying to add extra slash here
$socialURL = str_replace('https://', 'https:///', $socialURL);

$facebookURL = 'https://facebook.com/sharer/sharer.php?u=' . $socialURL;

My expected URL: https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fpillartopost.online My expected URL: https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fpillartopost.online

where %2F are the 2 slashes其中 %2F 是两个斜杠

Result URL: https://www.facebook.com/sharer/sharer.php?u=https%3A%2Fpillartopost.online%2Fnews%2Fwater-heating-systems%2F Result URL: https://www.facebook.com/sharer/sharer.php?u=https%3A%2Fpillartopost.online%2Fnews%2Fwater-heating-systems%2F

where there is only one %2F slash.其中只有一个 %2F 斜杠。

For some reason my str_replace method is not doing anything.出于某种原因,我的 str_replace 方法没有做任何事情。 I have looked through wordpress docs for other solutions and nothing was working, and this definitely seems like the most simple solution.我查看了 wordpress 文档以获取其他解决方案,但没有任何效果,这绝对是最简单的解决方案。 Why would that not add an extra slash?为什么不添加额外的斜线?

Probably, using trailingslashit with可能,使用trailingslashit

php query builder function could solve the problem: php 查询生成器 function 可以解决问题:

$social_url        = trailingslashit( get_permalink() );
$facebook_base_url = 'https://facebook.com/sharer/sharer.php';
$facebook_query    = http_build_query(
    [
        'u' => $social_url
    ]
);
$facebook_url      = "$facebook_base_url?$facebook_query";

According to trailingslashit docs:根据 trailingslashit 文档:

Appends a trailing slash.附加一个斜杠。 Will remove trailing forward and backslashes if it exists already before adding a trailing forward slash.如果在添加尾部正斜杠之前已经存在,将删除尾部正斜杠和反斜杠。 This prevents double slashing a string or path...这可以防止双斜线字符串或路径...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM