On my Wordpress website I have users fill out a form which ultimately generates a product. One of the form fields is a "Title" for the product. I'm trying to change the redirect for where the user is sent after they submit the form.
The URL for products are based on the "Title" of the product (e.g. http://example.com/product/My-Great-Product-Title/)
The issue with my current code is that I get a returned URL of... http://example.com/product/My%Great%Product%Title/
This returns a page not found 404 error.
Naturally a product title with no spaces in between the words works just fine but for obvious reasons every title can't be limited to one word.
My current code for redirecting which results with the 404 error...
$redirect = "http://example.com/product/$title";
How can I get my URL to be "My-Great-Product-Title" instead of "My%Great%Product%Title" which doesn't seem to work.
Assuming that the URL is generated by WordPress whenever the Product is created and now created by some other method you should be able to use sanitize_title() which is what WordPress uses to create their friendly permalinks.
The drawback to this is if there's a duplicate, it won't detect this but WordPress will append a -2 to the url. A better solution would be to get the ID after the product has been created and use get_permalink() which will be more accurate.
Related
I have a front-end editing form, where users can update specific post data (ACF custom fields). The problem is, that the users only know the post slug, not the ID.
So I need a page before, where I can convert the slug to post ID, then pass it as a url parameter, and redirect to the update page above.
I know the url_to_postid(site_url("slug")) function, but I don't know how can I pass the slug (from a form or a textbox) to this function, then redirect to a url with the parameter.
Let me know on which of the following steps you want me to explain further.
create a page template in your theme, code below
create a page that uses that template. now you have url for ajax from your front-end.
if it's on same domain you can use ajax fetch. if it's on another domain you can use jsonp which is same idea..
sample code for template file (not tested):
<?php
/* Template Name: My Service */
$slug = $_REQUEST['slug'];
$id = url_to_postid(site_url($slug))
echo $id;
exit();
// or
echo json_encode(['id'=>$id]);
// or
// redirect to url + id
Update - Non ajax solutions
First 2 steps as before. Then make a <form> around that button and <input name="slug"> that submits the to the address of the page you prepared in step 2.
I'm trying to get domain.com/employee?display_county=sd&dept=sales&id=23 to display in the URL as domain.com/firstnamelastname while retaining the URL variables.
And also if someone goes directly to domain.com/firstnamelastname it would still have the original URL variables.
How would one accomplish this?
Edit: OK, so I'm starting to rethink this...
Let's say I have a page that displays a list of users.
Each user has a clickable link that directs to domain.com/employee?display_county=XX&dept=XXXXX&id=XX
How could I get the URL to display domain.com/firstnamelastname when a user is clicked and still bring up the correct user?
I was thinking maybe using a POST method to pass the id to the script that displays the user and then rewriting the URL to display the firstnamelastname related to the id, but then browsing directly to domain.com/firstnamelastname would leave the id variable empty...
This is the situation:
I made a portfolio page for photographies. The gallery is already on the front page, so the first thing the visitor sees when calling www.homepage.com (example)
This loads all the images into the gallery, despite their category. Each image does have one or more categories assigned (one of which is 'all', which all pictures have, obviously).
The filter works as following: the navigation buttons for choosing a category are actually submit-buttons, that print the according category name into the URI, like so
www.homepage.com/?all=
The PHP reloads the page and puts this '/?all=' into a variable, strips it of all special characters and leaves 'all', which then filters the images as wanted and shows only those.
I hope it makes sense so far.
The problem is, that I want the first thing for the visitor to see be this:
www.homepage.com/?all
so he only does see all the images with that category assigned to it (I mentioned that all images have that, but that's not entirely true, my client wishes to exclude certain categories that are less important, but can be found by choosing this particular category in the menu. Achieved by simply not assigning the category 'all' to them)
So far I tried to change the standard URI of the page to this link via my hosting settings, which didn't work (too many redirects error).
I also tried a wordpress function to redirect the page with the added info
<?php wp_redirect( home_url( '/?all' ) ); exit; ?>
but again, the redirect-error strikes.
And since I didn't use wordpress in quite a while I'm already at my wits' end.
Is there any way to solve this elegantly, without reprogramming the whole filter system?
Thanks in advance :)
-edit-
I might want to add, that this should only be done the first time someone visits this particular page (so it doesn't negate the filtering process upon reloading this very page) and it obviously shouldn't affect any other page ( About, Contact etc.)
I solved this in another way (which was actually dead simple...)
Before I fired the WP function that put all the relevant post-data into the $args array I added an if-statement right in front of it that checked whether the $category Variable was empty or not. If so (and this is the case when the function prior to that cannot pick data from the URL) it will assign the variable "all" to it by default.
The Code then looks like this:
if(!$cat){
$cat = 'all';}
and that's basically it.
I'm using wordpress with a custom crm for a 2 part form.
When I send the first form data through from the form on the homepage it returns a customer ID in the URL to the second form page. However, the &recordid=xx makes wordpress returna 404 page.
How do I make the page not through a 404?
Original form second page: http://mysites.com/get-personalized-quote
Original form second page with variable: http://mysite.com/get-personalized-quote&recordid=2763
You should use ?recordid=2763 instead of &recordid=2763
note the ?
I've built a webpage that, in its products page has this prototype for its url
http://www.example.com/products/show/some-really-nice-product/512
The last bit of the url is the product id, which serves the sole purpose of searching the db for the product.
But, is it possible, using routing, to be able to hide that part and still be possible to search the product by its id?
Like
http://www.example.com/products/show/some-really-nice-product/ ??
Thank you
If you don't want to put a number in your URL, you can't later search by a number/id :)
So you could add a VARCHAR field to the table, say "urlized", and use "some-really-nice-product" as its content for the given ID (you'll need to 'urlize' all your product names and make them unique). Don't forget to index your new field...
Then, you could access the product page using this URL which is very SEO friendly (ends with ".html"):
http://www.example.com/products/show/some-really-nice-product.html
...by adding a route to CodeIgniter, somehow like this:
$route['^products/show/(.*).html'] = 'products/show/$1';
In your controller/model, search the database for the string that is passed to "show" method, instead of the ID
if you hide id you can't retrieve that from the url anymore
if you want to hide that anyway just do:
$route['products/show/(:any)'] = "products/show/$1/$2";