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).
Related
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! ;-)
Is there a way one can create custom permalinks in Wordpress?
For instance I am trying to create the following url:
website.com/our-services/douchebag-removal
but when I create the page douchebag removal I can not add the / to the permalink so it can look like this our-services/douchebag-removal.
This is what I get:
website.com/our-servicesdouchebag-removal
Please share your thoughts!
This is a simple one that gets me every time, on the our-douchebag-removal page you need to chose the parent page "Our Services" in order for the url path to reflect that.
Another solution, easier to use and not needing you to create parent pages: https://wordpress.org/plugins/custom-permalinks/ It's basically a plugin that allows you to put any url on a wordpress page/post. Let me know if it helped!
I have a Wordpress page /users and it displays a list of all users created. Each user has a link to its profile page. What I need to do is create that page. I want it to go like this /users/{user_id}-{first_name}-{last_name}, for example - /users/1-john-doe. I have a template for /users and also for profile pages. Question - how do I properly make a page with that profile template so that it would have the url described above? The page does not need to select the user automatically, it needs to pass the user slug so I can read it in that page and query for that user (as a variable or something).
Thanks.
What you are looking for is the WordPress Rewrite API.
This allows you to define a new "endpoint" for your URL's, and allows you to pick up the variable from the URL via built-in WordPress functionality.
A great article on it can be found here: Custom Endpoints
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.
I have created a simple basic component in joomla named, careerform so I want to know that what will be its url? Will it be :
index.php/?option=com_careerform
or in sef it will be something like:
index.php/components/careerform
or it will be like this while using .htaccess
/careerform
Is it true or what are joomla default URLs with different settings? Please tell what you know.
thanks for your time.
While what you are asking to do is possible, it would be rather complicated to rename a component. As part of the renaming you would have to update the names of dozens if not hundreds of classes throughout every file of the component. Very likely to run into some bugs because of this.
The far easier prospect is to just avoid these types of urls in your site. Joomla will only fall back to that style of url if a menu item does not exist for the view. Because of that, you can make the url into this:
http://sitename.com/any-component-alias/
To do this, go into the menu manager and create a new menu item. If you don't want this as part of the main menu, you can create a new menu. (I typically have a menu called "Hidden" for menu items that I want aliased but don't actually link to throughout the site from a menu.) When creating the menu item, make sure the type matches the component and view. The alias will then be whatever is entered in the alias box just below the title.
The one issue you may run into with this is that a component may not have a menu type for a particular view. In that case, you you would need to add the necessary metadata.xml file to the view (which again would be much easier than renaming the component!). A good tutorial for that can be found here: http://docs.joomla.org/Adding_view_layout_configuration_parameters.
yes if SEF will be enabled then index.php/component/careerform will be used.
You can create custom URLs by developing a router for your component.
There is very good documentation for it here.