How to get current page URL in Wordpress 6.0? - php

Trying to obtain the page url with the following code prior to Wordpress 6.0 works, but after update to Wordpress 6.0, $wp->request is coming through as an empty string on all pages. I have Post name set in permalinks under Common Settings if that matters.
The code below no longer works for getting the current url in the browser as of Wordpress 6.0:
global $wp;
$current_url = add_query_arg($wp->query_vars, $wp->request);
If there a new way to obtain the current url in the browser with Wordpress 6.0? I would need to obtain it as early as possible. The code above worked as is directly within the functions.php file without any hooks. Is there something similar to this in Wordpress 6.0?
EDIT: Tested with a Fresh install of Wordpress 6.0
In the twentytwentytwo theme functions.php file (placed the following at the top of the file):
global $wp;
$current_url = add_query_arg($wp->query_vars, $wp->request);
error_log(var_export($current_url, true));
error_log(var_export($wp->request, true));
Look at wp-content/debug.log and see that both are empty strings no matter what url you go to. Obviously, you will need to enable the debug log in wp-config.php first.
What is the correct way in Wordpress to obtain the current page url?
Someone mentioned that this might be related to the do_parse_request changes in Wordpress 6.0 outlined here: https://make.wordpress.org/core/2022/04/27/changes-to-do_parse_request-filter-in-wordpress-6-0/ but I'm not so sure about that as this is happening on a Fresh Install of Wordpress with the default theme.

The way of getting the current url should also work in Wordpress 6. $wp->request gives you the path structure of the url (like /parent/child ). To pass the whole url as the second argument, you can use the following code:
global $wp;
$current_url = add_query_arg( $wp->query_vars, home_url( $wp->request ) );
Moreover your problem might be, that you did not wrap your code inside a function, and the data you need is not there yet. You need a hook that fires after the data is available.
But
If you are ok with using another way of getting the current url, I would suggest:
function getCurrentUrl() {
$protocol = is_ssl() ? 'https://' : 'http://';
return ($protocol) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
$currentUrl = getCurrentUrl();
echo $currentUrl;

Related

Change permalink using PHP in Wordpress on one page

I am working on a cardealer website but there is a problem with the URL/permalink. When you want to share the page it doesn't use the actual URL but the permalink set on the page. I know this sounds logical but the URL is created in a external PHP file. This PHP file get's the information of the car out of the database and shows it on the page. The actual URL also get's build in this PHP file. So the permalink set in WordPress is a default and the actual URL is what I would like to share.
Permalink set in WordPress:
Actual URL on the page:
So if someone shares this page I want to send the actual URL (https://www.prinsauto.nl/vakgarage-aanbod-details/audi-a6-avant...) but not the permalink. Again, the actual URL is build in a PHP file.
How can I do that?
WordPress is not very easy when it comes to custom URLs. Viewing the code of the template-loader.php file, it seams that you could use a template file to run your page functionality.
As can seen in the code (line 77) you can use the template_include filter in your plugin (or template's functions.php file) to check for your specific "route" and return the template file to load.
Example:
add_filter('template_include', 'your_function_name', 0, 1);
function your_function_name($template) {
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, 'vakgarage-aanbod-details') !== false) {
return 'template-file-name.php';
}
else {
return false;
}
}
All the PHP logic you described in your question should go in included template file.

wordpress custom rewrite rule to external non-wordpress php page

Hello Everyone and thank you for your help!
I have an external (outside wordpress in root directory) php file that I would like to use the custom_rewrite_rule function to make the url pretty as well as pass on information for variables that will look up an API record from that URL. You might ask why I haven't done it the regular way and made it a page template inside wordpress, and that is because I am using a custom JQuery slideshow that seems to have trouble running inside a wordpress environment.
So what I have in my child themes functions.php is:
function custom_rewrite_rule() {
add_rewrite_rule('^yacht-details/([^/]*)/([^/]*)/?','yacht-details.php?info=$matches[1]&boatid=$matches[2]','top');}
add_action('init', 'custom_rewrite_rule', 10, 0);
and in the php file am using the standard $_GET to acquire the information.
$info = $_GET["info"];
$boatid = $_GET["boatid"];
My actual problem is what I am actually getting is literally $matches[1] and $matches[2] instead of the variables.
What am I doing wrong? Does this need to be done with .htaccess instead of functions.php? The problem I run in to going that route is wordpress does not seem to like more than one subfolder.
Thank you again.

Using PHP to generate internal links

I'm using a localhost site as the basis for creating other sites from. I'm using php to generate internal links by finding the current domain, and displaying it with a shortcode. I intend to upload my site to a live host and therefore the domain will change, and my internal links won't be broken.
//add shortcode that displays current site name
function GR_site_name(){
$currentDomain = $_SERVER['HTTP_HOST'];
return $currentDomain.'/wordpress';
}
add_shortcode('GR_site_name', 'GR_site_name');
?>
I've tested this by adding the code to live sites and it works perfectly.
I've read the post at how safe is $_SERVER["HTTP_HOST"]? however it details it as 'generally unsafe' due to the script that it runs.
Is $_SERVER['SERVER_NAME'] safer than $_SERVER['HTTP_HOST']? Or are both regarded as bad practice?
Make a config file with define('host','https://example.com') and use host wherever you want and when you upload script to server just change url in config file and you're good to go
Use a combination of the standard WP_HOME & WP_SITEURL constants, along with the site_url() function.
I'd recommend using one local copy of wp-config.php and a different one for the live site, and then add the following to them:
define('WP_SITEURL', 'http://www.example.com/wordpress'); // Or 'http://localhost/wordpress' for the local copy
define('WP_HOME', 'http://www.example.com');
That way, you don't need a custom function or shortcode - you can simply call site_url() wherever you need it to get the URL of your WP site.
Alternatively, if you need to keep the site URL variable and to accept anything that points to it:
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress');
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress');
If you need to keep the shortcode for use within the context of a post, you can change it to:
function GR_site_name(){
return site_url();
}
Note that setting WP_HOME and WP_SITEURL override any database setting for the site's URL, and if you visit the Settings->General page, you'll see those two fields are greyed out.

Best way to have script urls in WordPress

I am developing my own theme for private use and i have a lot of scripts working with urls with extra parameters.
I'm finding my self adding a lot of empty pages to my site just to be able to connect some php file to specific url so i will be able to send parameters to that specific address.
Is there a better way i can have urls that can be associated with specific php files without adding pages or other things to the site. just like work out of the box when installing my theme?
You should try like this code:
//page url: http://www.example/myurl
add_action( 'init', 'my_rewrite' );
function my_rewrite() {
global $wp_rewrite;
//wp-content/plugins/myplugin/myurl.php
add_rewrite_rule('myurl/$', WP_PLUGIN_URL . '/myplugin/myurl.php', 'top');
$wp_rewrite->flush_rules(true);
}
More info:
https://developer.wordpress.org/reference/classes/wp_rewrite/
https://codex.wordpress.org/Class_Reference/WP_Rewrite

include file and https stops my css from working

Okay, so I have a really odd problem... I have this line at the top of my page, because I want to get the latest wordpress blog post (which is fine, the post comes back);
<?php
require('./blog/wp-load.php');
?>
But then whenever I visit a page on my website that has https:// in the url it seems to stop my css file from working would anyone be able to help? thanks
in wordpress its always best to use the right api method:
http://codex.wordpress.org/Function_Reference/site_url
<?php
$url = site_url();
require($url. /blog/wp-load.php');
?>
The site_url template tag retrieves the site url for the current site (where the WordPress core files reside) with the appropriate protocol, 'https' if is_ssl() and 'http' otherwise. If scheme is 'http' or 'https', is_ssl() is overridden. Use this to get the "wordpress address" as defined in general settings. Use home_url() to get the "site address" as defined in general settings.

Categories