Custom WordPress URL Structure - php

I'm trying to ensure the WordPress site I'm working on maintains consistent URLs. The problem I'm having is with child pages and custom post types.
Essentially the setup is as follows.
/main-page/sub-page/
Main Page
-- Child Page
This is fine, however I have a custom post type (research-papers) I'd like to list on the Child Page. I've got this working fine. The problem I'm having is getting the research papers post type to use the following URL structure:
/main-page/sub-page/research-paper-title/
I've looked at wp_rewrite_rule but I can't get anything working. Any ideas?
add_rewrite_rule(
"main-page/sub-page/([^/]+)/?",
"index.php?research-paper=$matches[1]",
"top"
);

Change the first parameter from
"main-page/sub-page/([^/]+)/?"
to
"main-page/sub-page/([.]+)/?"
The way it's set up now, you are searching for the pattern "main-page/sub-page//...", with 2 forward slashes after "sub-page", which isn't going to catch anything.

Related

Wordpress: Slug adds suffix

How do I prevent wordpress from adding number suffix to my URL.
I am trying to have the below two urls, but when I created the Arabic version the number 2 was appended to the first one. Below is what I want
http://site-name/en/seven-days
http://site-name/ar/seven-days
But this is what I get.
http://site-name/en/seven-days-2
http://site-name/ar/seven-days
I understand that this was added to wp
The pages need to have the same slug at least, even if the title should be different.
The WP engine wants unique post names and will always append a number to achieve that. You'll need to use a custom permalinks plugin to get what you want. Here's an example of someone with the same issue, working around it with a permalink plugin: https://wordpress.org/support/topic/polylang-share-same-slug-between-languages/

Intercept 404's in wp and redirect to a post based on its custom field value

So this is the my issue:
I have an 'huge' (more than 1000 pages) non wp website that i've ported to wordpress.
It's impossible to match the url's from the old website to the new
website because page titles and general information architecture
have changed
So in a custom post type that will hold the 'Listings' (listings_cpt) i've stored a custom field named old_website_url.
My goal was to intercept 404 requests with the template_redirect hook because from what i've read it looks like appropriate to intercept a 404 before the template is loaded and an actual header is sent.
So hypothetically if someone tries to see a page that doesn't exist but its url is stored in old_website_url from the listings_cpt posts i could redirect him to the post permalink that holds this value.
I've did a lot of trial/error approaches with no success.
Can someone point me in the right direction??
Is there a better way to accomplish this?
Thanks in advance
Well your approach should work. Here is what you need to do:
Create a custom 404 page that reads all posts
For each post read its old_website_url custom field
If the old_website_url custom field of a post matches the current url, the user is redirected to the url of the post.
Have a look at the WordPress documentation

Wordpress create custom pages with permalinks from custom table data

I have 1 custom table (not wp core table) where are specific products imported. I've set up custom wp page where these products are listed, custom filter created etc. In this step so far everything is OK - products are listed and filtering works, pagination works...
But what I want to extend further now - open each product page (like /product-listing-page/product-name-1) when clicking on product names in product list page... How can be this achieved "out of wp_posts" table?
Why I'm not using wp_posts table: web site have static pages and dynamic posts as news and product table is truncated and re-imported once in a day every night! So, can not really use wp_posts page when product list is dynamic and totally truncated before each import...
Any ideas? Maybe anybody already did such stuff?;-)
I don't know if you developed a plugin to handle all the custom table and code you created, but if you didn't, I strongly encourage you to do so.
Because by making your code as a plugin, you have the oportunity to use all the composants of wordpress, like the rewrite rules system, which, I think, could help you to achieve your goal.
I created a plugin some times ago for a website I have the charge and I needed a custom page to be displayed within the website (worpdress) url system, which is what you want to do if I correctly understand your question.
Unfortunately the plugin has evolved since and I didn't keep a backup of that code, but if I remember correctly how I did it back when, here is the rough approach I followed:
I create a plugin which handle :
custom db table(s)
custom php objects
...
AND (this is the intersting part) add rewrite rule to wordpress rewrite rules system as follow:
set rewrite tag (init action) (if needed)
add rewrite rule (init action)
intercept url and parse it to get the params i need in my custom
page (parse_request action)
The following functions may help you with that :
add_rewrite_tag
add_rewrite_rule
And you need to hook in :
init
parse_request
Hope it helps you start.
the solution was quite quick and easy! For my needs it was enough just to register global query var:
function register_new_query_var($vars) {
$vars[] = 'product_id';
return $vars;
add_filter('query_vars', 'register_new_query_var');
and then I changed theme template for page product-listing-page to work for product listing and individual products with using
if(!get_query_var('product_id')). So, if query empty - do listing stuff, if not empty - do individual product stuff!
For me this is quite enough and I do not need even seo friendly urls (because product list changes every day, so - every day will be new products, links, and there will be to much 404 errors)...
and I can use http://example.com/product-listing-page?product_id=1 and page displays with date which is from custom table! ;-)

wordpress custom page routing PHP

I'm trying to make a custom page on a Wordpress site.
It is more or less linked to the Wordpress itself. The code is custom but I have used links to it from Wordpress Dashboard and normal pages.
The point is that now my boss wants to create a page like example.com/me/name and that should SELECT some data about the person.
Example: site.com/me/johnx
This should run something like SELECT FROM DB WHERE first_name LIKE 'john' AND last_name LIKE '%x' let's say.
How can I create my folder / file / function "me" so that when I type "/name" I can do an explode under it and use the last parameter to show the page?
Thank you!
Edit: I know about GET method.
As in example.com/me?name=johnx
But I want to know how to do it with simple slashes "/".
I think you should add a rewrite rule, which is a function called to tell Wordpress how to handle the URL's. You can find further info here.
add_rewrite_rule() allows you to specify additional rewrite rules for WordPress. It is most commonly used in conjunction with add_rewrite_tag() (which allows WordPress to recognize custom post/get variables).

Old/new website - same category URL! What to do?

I have a problem. I bought an old domain name and I redirected all my content to it now.
So I have this category as example: http://www.formulawahad.com/category/news/ (or simply formulawahad.com/news/)
So any news link is like this: http://www.formulawahad.com/news/37329/
(My wordpress installed is not in the root - inside a folder).
Today, I got all the files from this domain which goes back to 2001. And Google found like 2000 errors. Now I want to upload them and make them available as they have a lot of backlink.
But the problem is the link of the old articles are like these:
http://www.formulawahad.com/news/090217_coulthard.htm
Or: http://www.formulawahad.com/news/080623_trulli.htm
So what do I do? create a "news" folder and place all html files in it? but then what will happen to my wordpress category formulawahad.com/news/?
I have the same problem with 2 more categories. So I need your help before I decide what to do.
Thanks a lot.
This is simple to get rid of those 2000 errors in google. In wordpress init() hook you have to make a redirect function. In databse add a table with old and new url,get url and search with old url and redirect it to new one.

Categories