wordpress auto generate pages from sub url of page - wordpress

Hoping someone can help. I don't want to create actual pages in the backend of wordpress but i want to know how i can make it so if anyone goes to a url: mydomain.com/page/sub-url.
Then i can grab that "sub-url" and output a page with content i generate via php.
If i grab that "sub-url" and i don't want to output i can do a 404 error.
I want to try to stay away from editing the htaccess file is possible but if i need to i can.
Everytime i try search for this, i get result for creating actual pages in the backend automatically which i don't want.

I think what you're looking for is a custom rewrite endpoint, which you call 'page'. Then on yourdomain.com/page/sub-url, 'page' is the parameter and 'sub-url' is the value. You can get that value with the function get_query_var().
After applying, be sure to go to your Wordpress admin->Settings->Permalinks, and click save to flush the current rewrite rules.
Update:
Use this code to show a 404 page
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
(source)

Related

Home page url rewriting Wordpress

I want to do a URL rewrite in the WordPress home page
I want to change my URL http://mysite.loc/?pays=senegal to look like http://mysite.loc/senegal.
The problem is that I am on the WordPress home page, so it will be confused with the URL of another page like http://transfert.loc/page-example.
I have already tried several optins but am completely blocked.
Here is my code example:
public function rewrite_urls(){
add_rewrite_tag( '%pays%','([^&]+)' );
add_rewrite_rule(
'([^/]+)',
'index.php?pays=$matches[1]',
'top'
);
}
Can someone help me please!
Thanks
Two problems I see- rewrite rules need to set query vars that will result in a successful main query. Setting just a custom var like slide doesn't parse to anything WordPress can load. Additionally, slide needs to be added to the recognized query vars for it to get parsed within a rule.
So, what would a rule look like that would load the front page posts in the main query? That's a good question- the posts page is a special case, the absence of any other query vars. I haven't found a way to do that with a rule, though it may exist.
An easier way to do this is with a rewrite endpoint:
function wpd_endpoint(){
add_rewrite_endpoint( 'page-example', EP_ROOT );
}
add_action( 'init', 'wpd_endpoint' );
Keep in mind that if you have code accessing values via $_GET, this still won't work, because WordPress doesn't put query vars there when rules are parsed. You can change the code to use get_query_var, or just assign it before the code tries to access it:
$_GET['page-example'] = get_query_var('page-example');

Creating personalized URLs (PURLs) in Wordpress

I need to create a site that will allow for a unique user-identifying number to go in be appended to the URL.
So for instance
test.com/12345/about/
will load the page with slug 'about' (not redirect to it).
I have tried the following:
function custom_rewrite_basic() {
add_rewrite_rule('^([0-9]+)/', '/$matches[1]', 'bottom');
}
add_action('init', 'custom_rewrite_basic');
But it does not work, any prefix to the URL redirects the page with or without the block of code above.
So in my tests
test.com/23423423/about will re-direct to test.com/about which is not what I want.
Is what I am trying to achieve possible? If so, could anyone offer advice as to how I would best go about implementing it?

301 Redirect only Posts not Pages

I am looking to redirect all the posts and not pages and custom post types from old domain to new domain. So all posts for instance
http://www.example.com/post1 should go to http://example1.com/post1
http://www.example.com/post1 should go to http://example1.com/post1
The pages i.e. http://www.example.com/page1 should still be functional
I can find alot of answers on moving blog into sub directory along with moving all the links but not for moving just the posts and not the pages
Edit
There are over 1000 posts so I am looking for a dynamic way of doing this
Thank you for your help
The hack I finally end up using was
Add ob_start to single.php as first line even before get_header()
Add following code in functions.php
change example.com with your site domain name
add_action('wp_head','redirectme');
function redirectme() {
if ('post' === get_post_type()){
wp_redirect( 'http://example.com' . $_SERVER['REQUEST_URI'] , 301 ); exit;
}
}
I don´t think this is possible with this URL schema. .htaccess (or the Apache behind it) doesn´t know if post1 is a post or page. How shall it then redirect on this base?
If you would change the URL structure for pages alone (I don´t think this is possible by default, or am I wrong?) you could target their URL scheme and redirect.
To access the old pages (with the new URL) you could then use a "broken link checker" plugin.
Update: You could possibly use the hook described here to check wether the called post is a post or page and then use wp_redirect to call your new URL.

Custom Plugin for wordpress with hierarchy of SEF pages

Here's my issue. My company needs a vendor database added to our wordpress website. None of the existing plugins will even come close to what we need, and we already have a mysql database with all of our information, so we need to create a plugin or something to do what we need.
These urls need to be direct-accessible and have SEF urls. So, for example:
mysite.com/vendors/
mysite.com/vendors/pipe-manufacturers/
mysite.com/vendor/bobs-pipes/
And, the custom content needs to appear inside the wordpress template.
There are really 2 options:
1) Find a way to write our application outside of wordpress, but find a way to bootstrap wordpress to show the header, footer, and sidebar.
2) Run the app from inside wordpress.
So I went for option #2. I created a new template file named "vendor.php", and began working. I added this code to my functions.php of my theme:
add_filter( 'template_include', 'xyz_template_check' );
function xyz_template_check() {
global $template;
$rqst = $_SERVER['REQUEST_URI'];
$ra = split("/", $rqst);
if ($ra[1] == "vendors") {
$template_file = get_stylesheet_directory() . '/vendors.php';
return $template_file;
}
return $template;
}
So what the above code does, if it sees the word "vendors" as the first part of the url after the site name, it sends you to vendor.php. This works PERFECTLY....
except...
Wordpress believes that the page is not found. It returns a 404 header, and NOT FOUND into the page title and breadcrumb.
Adding a PAGE called "Vendor Database" with the permalink "/vendors/" fixes the main page. But there will be literally hundreds of vendors and different categories. I cant be creating a custom page for each one. This needs to be dynamic.
So, how do I make wordpress give a 200, and supply an acceptable page title, breadcrumb, etc.
Don't even get me started on the danged wp_title filter. This did NOT work as documented. Although, it just occurred to me that this might be an issue with Wordpress SEO (the wp_title filter issue).
Anyone got an idea on this?
Ok got this. The solution was to use the rewrite api, as mentioned above, to look for the pattern /vendors/, letting it know that it was a valid URL. Coupled with my existing template override, this is what I needed.

Wordpress custorm URL variables causes strange redirect

I'm trying to have custom variables in my URL for Wordpress site. I have read up as much as I could find on the subject and so far have the following in my functions page:
function add_query_vars($aVars) {
$aVars[] = "mrdrct";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
And the following on my header page:
if(isset($wp_query->query_vars['mrdrct'])) {
$mVar = $wp_query->query_vars['mrdrct'];
echo "variable is $mVar <br />";
}
Just to test out if things are being passed correctly and they are. However, when I use a link with the url variable in it - say www.mydomain.com/?mrdrct=myVarable - I am not directed to my homepage of my Wordpress site which is set to a static page with a template on it - I am instead directed to a page with my latest posts on it. I cannot figure out why this is happening - any ideas? Hopefully I've explained this well enough.
Thanks.
When WP sees a query string (? after the URL) it will attempt to display matching posts using it's rewrite rules. If no posts match it will show a 404 error - I would guess you do not have a 404.php file, so WP is showing the default which is index.php (see the Wordpress Template Hierarchy for more details on that).
I'm not 100% sure what you want to achieve, but I'd suggest that you need to look at changing the query when $wp_query->query_vars['mrdrct'] is set. See the WP Codex for query_posts() for a good place to start, if you are not already familier with it.

Resources