最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Encode text string being appended as query to URL

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I have a shortcode in use at present that embeds a button on selected product pages. Upon clicking this button, the user is brought to a contact form on another page and the title of the Product from where the button was clicked is appended to the end of the contact form URL as a query string. This product title is then gleaned from the query string and auto-filled into a field labeled "products" on the contact form.

Code as follows:

add_shortcode( 'dynamic_contact_button', 'button_product_page' );

function button_product_page() {
global $product;
return "href='/contact-form/?products=Product:%20" .$product->get_title(). "&#contact_form'";
}

This creates a URL such as:

/?products=Product: Brown Paint - 1L&#contact_form

It works quite well... until a product title includes an "unsafe" character which can't be appended to the query string. For example, the apostrophe character ' to denote measurement in feet in a product title - the query string gets cut where one of these appear meaning only part of the product title is carried over to the form. I'm sure there are many other unsafe characters which would cause similar issues.

Is there a something I can add to the code here to encode the Product title text being appended... so space becomes %20, an apostrophe becomes %27, etc. I see there is an urlencode option in PHP but my understanding is lacking on how I might implement it

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I have a shortcode in use at present that embeds a button on selected product pages. Upon clicking this button, the user is brought to a contact form on another page and the title of the Product from where the button was clicked is appended to the end of the contact form URL as a query string. This product title is then gleaned from the query string and auto-filled into a field labeled "products" on the contact form.

Code as follows:

add_shortcode( 'dynamic_contact_button', 'button_product_page' );

function button_product_page() {
global $product;
return "href='/contact-form/?products=Product:%20" .$product->get_title(). "&#contact_form'";
}

This creates a URL such as:

https://www.example/contact-form/?products=Product: Brown Paint - 1L&#contact_form

It works quite well... until a product title includes an "unsafe" character which can't be appended to the query string. For example, the apostrophe character ' to denote measurement in feet in a product title - the query string gets cut where one of these appear meaning only part of the product title is carried over to the form. I'm sure there are many other unsafe characters which would cause similar issues.

Is there a something I can add to the code here to encode the Product title text being appended... so space becomes %20, an apostrophe becomes %27, etc. I see there is an urlencode option in PHP but my understanding is lacking on how I might implement it

Share Improve this question edited Apr 26, 2018 at 10:21 Mostafa Soufi 8057 silver badges19 bronze badges asked Apr 26, 2018 at 0:03 ConMConM 731 gold badge1 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

To encode the URL, you could use the PHP urlencode( $url ) function or use the WordPress urlencode_deep( $array | $str ); function.

add_shortcode( 'dynamic_contact_button',    'button_product_page' );

function button_product_page() {
    global $product;
    return urlencode( "/contact-form/?products=Product:%20" .$product->get_title(). "&#contact_form" );
}

links:

WordPress - urlencode_deep

urlencode

The urlencode() function is your friend, it's a standard PHP function that encodes strings into URL valid format

发布评论

评论列表(0)

  1. 暂无评论