I'm workin on a plugin and I need some help.
Basically, I list some companies on the website and if you click on a company name, you go to the company details page.
I'm not using post types. I have created a custom table into the database.
On the listing page, I have a link that looks like this:
Read more
$value->company_name // I take this from the database
Everything works just fine, except the details page URL.
Now I get something like this:
http://www.website.com/companies/details/?companyname=My%20New%20Company
I use $_GET['companyname'] on the details page and I list the related company details, from the database.
But, I would like the URL to look like this:
http://www.website.com/companies/details/my-new-company
And still be able to get the company name from the URL and retrieve the details from the database.
I hope you can help.
Thank you.
Regards,
AG
Add a new column named slug in the table and remove spaces of that title.
$string = "my new company";
$string = strtolower($string);
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
$string = preg_replace("/[\s-]+/", " ", $string);
$string = preg_replace("/[\s_]/", "-", $string);
echo $string;
Output: my-new-company
OR
You can follow this link
https://developer.wordpress.org/reference/functions/sanitize_title/
I am using sanitize_title now and added a new column into the database table, called slug.
Now the links on the companies listing pahe look like this:
http://www.website.com/companies/details/?companyname=my-new-company
Better than
http://www.website.com/companies/details/?companyname=My%20New%20Company
But, if I create the links without a query, I get a 404 page. That is actually normal since those pages don't actually exist.
http://www.website.com/companies/details/my-new-company/
The slug was a good idea but how can I make such URLs work?
Once these kind of URLs work, I can parse the URL, extract the company slug and retrieve the related data from the database.
I guess I have somehow to transform the query URL into a permalink and on the details page I have to extract the query from the URL.
Related
I need help with this: I need that the url of the downloadable products link with an external url. I´m trying to add a custom field and replace the filters woocommerce_product_file and woocommerce_product_file_download_path but it didn't worked, I think because I can´t call to the product data from the order. Someone did this? Thank you for your help!
The code doesn't works. Now is something like:
*function mostrar_campo_personalizado_en_order($file_id){
global $wpdb, $woocommerce;
echo get_post_meta($file_id, 'downloadable_url', true);
}
add_filter( 'woocommerce_product_file', 'mostrar_campo_personalizado_en_order', 10, 1 );
add_filter( 'woocommerce_product_file_download_path', 'mostrar_campo_personalizado_en_order', 10, 1 );*
I don´t know if I´m working in the correct way, I need to change the url of the downloadable products because woocommerce internally routed. Because I couldn't modify him, I intente removal and display a custom post field. The page is in a test server because the site is on line. The url of the tester server ishttp://lab.jus.raffles.com.ar/my-account/ but you need credential to come in:
user: paula
pass: 5VPeGgAHWzYX1T
I need to edit a template's header.php file and I want to check if it has a taxonomy named "store". If so, then I need to extract it's tag_ID.
When I edit the page (it's not a WordPress Page type) I can see these values in the admin url:
taxonomy=store&tag_ID=720
So I know there is a way but I'm having trouble getting any good results. I tried the
method detailed here to extract at least the tag_ID but I'm getting NULL:
$tag_id=get_query_var('tag_ID');
echo $tag_id; //NULL
Edit:
To be clear in regards to the tag_ID because it may be confusing, all I really want is to get the unique id of the requested page so I figure seeing first if it has a taxonomy named "store" and then getting the right one using the tag_id.
AS far as i understand your question, You should try this:-
$data = get_queried_object();
With this, you can get what page/post/taxonomy is being called.
Just print-out/var_dump this $data variable, you will get full object of page/post/taxonomy.
Hope this may help you.
Hard to define the Title of this Question....
I want to create a nice readable permalink structure for my 2 custom post types (CPT).
My first CPT "family" has the following rewrite-slug "family/%postname%" (all works fine)
The second CPT "childs" has a metabox where I can select the parent_id-field by choosing a CPT "family" where the child-CPT belongs to. That also works great.
Now I set the rewrite-slug for "childs" to "%parent_post_url%/child/%postname%" so that I can get the following URL "family/the-griffons/child/peter" . But when I call this URL wordpress displays a not-found-page. The crazy thing is that if I set the rewrite-slug hard to "family/the-griffons/child/%postname%" I can call the URL (both URLs are the same!!!)
So why toes WP throws an error when I try to get the URL dynamically but not when I hardcode the URL??
The child-parent relationship you think you have is not quite there. Based on what you've told us so far, it seems that all you have is a custom field denoting the "pseudo-parent" id. So what your question should really read is
How do I rewrite the first part of the cpt url, based on the cpt custom field value ?
because, as far as wordpress is concerned in your case, that's all that "parent id" really is- a custom field value.
you can try following the last part(Part 3.) of this tutorial, keeping in mind, that you'll want the actual path of the url of the "parent id" and not the cf "parent id" value itself, you'll have to implement something along the lines of:
$parent_id = get_post_meta($post_id, "your_cf_key_for_parent_id", true);
$full_parent_post_url = parse_url( get_permalink( $parent_id ) );
$parent_post_url = $full_parent_post_url['path'];
if(!$parent_post_url) { $parent_post_url = "default-fallback" }
$permalink = str_replace(‘%parent_post_url%’, $parent_post_url, $permalink);
return $permalink;
another relevant stackexchange answer:
using-custom-fields-in-custom-post-type-url
BUT as #c0ns0l3 mentioned using custom taxonomies IS the proper way to go about this.
I want a structure such as www.training.com/courses/course/location where course and location change to a course name and a geographical location to dynamically translate to www.training.com/course/?course=course&location=location
The reason is I have a plugin to display course information based on course name and location that takes get parameters.
P.s. I don't want to manually add links into a .htaccess file to achieve this.
Are the courses and locations associated with each other? If so...
I'd recommend a custom post type for "courses," with a custom, non-hierarchical taxonomy attached, and then use a custom field in that post type to attach the location information to each course.
The Types plugin would handle all of these things relatively easily (but you'd need to create a template to display that information, of course).
If the URL structure is going to be predictable, you could try:
// $url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$url = "www.training.com/course/?course=course&location=location";
$array = explode('/',$url);
$rev_array = array_reverse($array);
$location = $rev_array[0];
$course = $rev_array[1];
Then edit the plugin code slightly to read these variables rather than get variables.
I was wondering which is the best approach to get the catgeory ID when listing the posts within a particular category. Normally, the urls look something like this : www.example.com/?cat=4 and it is pretty easy to get the id. However, I really need the urls to be routed like this www.example.com/categories/hotels . wordpress provides an easy way to do the "pretty" routing, however all of the GET paramater information is lost this way. In this case, the $_GET variable is assigned nothing. I need to be able to say $category = $_GET["cat"] or something like that
What is the easiest approach ?
Can you use the Wordpress get_the_category function to grab the ID (from member variable cat_ID) once you're in the template?
See http://codex.wordpress.org/Function_Reference/get_the_category
e.g.
foreach((get_the_category()) as $category) {
$id = $category->cat_ID;
// do something with $id
}
The thing is that the guy might not really want to associate categories with post ids. In this case, there's the global $wp->query_vars array that contains all the data coming from the GET request even when the routing has been "prettified"
What about the category base setting in the backend? "Configuration" > "Permalinks" and there the last paragraph. See here for docs.