set wordpress post's permalink for specific page - wordpress

When I add/edit post I can change slug of the permalink. But I can't add slashes.
So assume I have
http://domain.com/myslug
but I need
http://domain.com/myslug1/myslug2
when I save it does
http://domain.com/myslug1myslug2
How can I overcome this?

When you visit a page, let's say domain.com/careers, WordPress has to figure out which post to retrieve from the database.
It will look at all of its rewrite rules and try to find one that matches the request. In this case, the last rule will catch it:
[(.?.+?)(/[0-9]+)?/?$] => index.php?pagename=$matches[1]&page=$matches[2]
It will pass in careers ($matches[1]) as the pagename value, dissect it accordingly, and then look in the database for a post called careers.
When you visit a page called domain.com/careers/internships, the last rule will apply again. This time it will pass in careers/internships, dissect it accordingly, set the parent of the post to careers, and then look in the database for a post called internships that matches the parent.
If WordPress were to let you set the slug to myslug1/myslug2, a couple assumptions WordPress makes would be wrong.
There is no parent page called myslug1
There is no page called myslug2
There is no rewrite rule that lets WordPress know how to handle a myslug1/myslug2 slug
All that said, it may still be possible to accomplish what you want, but maybe not in the way of making up slugs will-nilly. There has to be a pattern, some kind of rule, that you can give WordPress that says "if x, then y".
For example, you could have a meta box with an input field that whenever you entered something in it, that slug would appear at the end of the URL. Or perhaps you can sandwich a custom slug between domain.com and myslug2.
Here are a few things to look into:
add_rewrite_rule function - Allows you to specify additional rewrite rules
add_rewrite_tag function - Make WordPress aware of custom query string variables
query_vars filter - Allows query vars to be manipulated prior to executing the query
template_redirect action - Executes just before WordPress determines which template page to load
Another approach might be to create a parent page with myslug1 and then a child page with myslug2. Or, depending on your needs, you may also take a look at custom post types. You can have a custom post type called myslug1 and then posts with in it, where one post would be myslug2.
If you have specific questions about implementing one of the aforementioned, look through Stack Overflow or post another question with details if you can't find the answer.

Related

Custom post type slug duplicates

I purchased a plugin that allows buddypress users to add new pages to their profile and ran into an issue that caused me to reach out to the developers, only to realize that this seems to be an issue with every custom post type, and is not isolated to a single plugin. This very well may be a Wordpress thing, but this is extremely inconvenient and I was wondering if there was a way around this.
When users add new pages to their profile, even if the permalink is unique, on the chance that a user names their page the same name as another user, a number denoting a duplicate is added to the permalink.
For example:
User 1 makes a page titled Services (mysite.com/user1/services
User 2 also makes a page titled Services (mysite.com/user2/services-2)
I have changed the post type permalink to include /%author%/%post-name% which works correctly, and changes the permalink, except a number is still added to the end if more than one page has the same title. How can I stop this from happening if the pages are made by different authors?
This was the message I sent to the developers of the plugin, who informed me the issue isn’t really the permalinks, it’s more that the pages are a custom post type and thus beholden to the same slug rules that normal pages have, where if you created 2 pages named “contact”, the second one gets “contact-2” and so on.
I had assumed the user’s buddypress/author page would count as a parent page, but it seems that’s not the case. This appears to occur on my own custom post types as well as any custom post type plugin I happen to use, but I had never tested that until just now. Even if the permalink itself is unique, custom post types cannot have the same title/slug even if created by different users and I can see this hindering user experience.
Does anyone have any thoughts?
Adding author to permalink does not make the page unique. Pages are defined as individual objects and their permalinks are related only to the last section of the link.
The system compares only the post name (the slug) not the full URL or permalink.
mysite.com/user1/{post_slug}
If the {post_slug} is the same as another post slug then the system adds incrementation.
Just to clarify the answer - The post permalink is defined by the post slug and not the permalink itself.
What I would do in your case is to set these pages to an ID-based permalink.

Redirect to wordpress page without page ID

I'm looking for a way to redirect to a specific wordpress page without knowing the ID.
I have a shortcode that is meant to redirect to a registration page once a button is clicked.
Before, I was able to just use a page ID. Now I am using WPML and the page ID's are all different for each language.
How can I redirect to a specific wordpress page without knowing the specific page ID for the current language?
Maybe you could use get_page_by_path() or get_page_by_title().
Both functions accept Post Type as argument, it's not only restricted to "pages". And they will return a full $post object, ie, $post->ID.
Maybe a WPML function is needed, an equivalent of those. Or maybe a combination.
See the docs for Language dependent IDs.
You could add a custom field which will be shared by all versions of the same article, and then query by that instead. Something like an article GUID.

Set the permalink to any domain in Wordpress

Is this possible to achieve without too much effort or a plugin? It's simply to instantly redirect to a site I've made in my portfolio if the post doesn't have any content.
Note that I don't want a script in the actual post to redirect since I only use Wordpress as the backend and fetch all the data to be used by a JavaScript backbone app.
EDIT : Added some extra info in a comment
Short Answer: No. You cannot set your Post Permalink to an external asset without directly editing the database.
Long Answer: First off, you need to understand the purpose of the Permalink. It isn't just an arbitrary link to somewhere. It's reserved specifically for storing where that specific post lives within the context of your Wordpress installation.
Your post doesn't live on example.com. It has its own ID, and Wordpress has a specific spot set aside for it where it can be seen on YOUR domain. Leave it as such.
Instead, what you should be doing is employing the tools Wordpress gives you to achieve what you want. A Plugin doesn't exist for what you want because - quite frankly - it's a fairly trivial task when approached CORRECTLY and doesn't warrant the use of an external resource.
So what's the correct way?
Use a Custom Field. Name your Custom Field something like 'externalHref' and fill that in with your link to the website you want to point to in case there's no post content.
Then in whatever templates you're using to generate your posts, just check either for the existence of your Custom Field called 'externalHref' or check for the non-existence of Post Content. In either case, instead of generating a link using The Permalink you can simply build a link that points to wherever you need.

Wordpress rewrite (remove) part of url

I searched around for an answer. There's a couple that look like this, but most point to a plugin, or feature examples that I can't get working in my case...
So the question:
I created a custom post type for the homepage of my site. I made it the default front page, so now when I enter: localhost/vhh I automatically get forwarded to this page: localhost/vhh/homepage/homepage
The first "homepage" refers to the custom post type. The second is the name of the "post".
Now I want to remove both, or at a minimum 1 "homepage", so I get:
localhost/vhh or localhost/vhh/homepage
Can this be done with a WP rewrite rule? And how?

Post privacy in Wordpress

Can a post be hidden from home page, archive view, category lists etc. and viewable only if you have a direct link to it? The blog doesn't have registered readers and is open to public so that would be a mean of hiding some posts from public view without using the password protection.
I asked this question in Wordpress section and the idea there was to use conditional code so I'm asking the question here as well to get closer to the code.
If I used conditioning, would I have to input each post's ID separately to PHP file for archive, categories, search and such?
EDIT:
After reading a bit more all over, I had an idea of creating a private category and then use some kind of conditioning so that posts from that category are hidden. According to Codex, certain category can be hidden from, for example front page but I don't know if there's a way to hide it altogether except when you have a direct link.
Creating a "Private" category is a good solution. It is quite possible to hide this category altogether except via direct link. You just have to "block all the exits" with conditional code.
The default WordPress theme displays posts via the Post Loop. See http://codex.wordpress.org/The_Loop, especially the section entitled "Exclude Posts from Some Category". Just find all the places in your theme's PHP files (e.g. index.php) where this loop is used, and add the conditional code. You'd also need to filter your category list and blog archives in the side menu. Don't add filtering in single.php, otherwise the private post won't display on its own page.
You'd probably want to add a similar condition to filter search results so that private posts aren't leaked via the blog's search tool. There may be more "exits" I haven't thought of, but I'll be sure to update as I do. I'm glad to look at specific code if you so desire.
Understanding WordPress' post query and loop really opens up a world of possibilities for customization.
I found the simplest way - just use Simply Exclude Wordpress plugin. It has the option to exclude each post (or tag, for that matter) from front page, archive, search or feed. It works flawlessly. You can still view the posts by using direct links.
(Not actually an answer that includes code but a working solution nonetheless.)

Resources