include file and https stops my css from working - php

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.

Related

How to get current page URL in Wordpress 6.0?

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;

Wordpress get_template_directory_uri() not returning https and only return http

I install ssl on my site everything working fine except one thing, all the links coming from get_template_directory_uri() function are not returning https
https://www.bearapeninsula.com here is a website link.
As you can see everything else is working fine but if you go on other pages and inspect it and check console you will see the mixed content errors which is because of get_template_directory_uri() not displaying https.
Make sure the WordPress and Site Address URL under the general settings include https
The image shows, http, you need https
get_tample_directory_uri() uses the site_url() , tacking on the name of your WordPress content directory, and the location of your theme plus its name. site_url will return http if is_ssl() is false, which depends on your General Settings, as stated above.
get_template_directory_uri() dosn't return full URL but only URI. To get website url, you can use get_site_url() function

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.

get_stylesheet_directory_uri() and get_template_directory_uri() not returning "http"

I've made a local copy using xampp of my wordpress website. The theme was written by me.
Locally, every image link using get_template_directory_uri() or get_stylesheet_directory_uri() is broken because it is missing the "http" prefix. If I add it manually, then the image is displayed correctly.
This is the code:
<?php echo get_stylesheet_directory_uri() . '/images/myimage.jpg' ?>
the above functions online generate:
http://www.mywebsite.it/wp-content/themes/mytheme/images/myimage.jpg"
locally on xampp they generate:localhost/myfolder/wp-content/themes/mytheme/images/myimage.jpgand the image is not displayed. If I manually add http:// before localhost it works.
Interestingly enough, the function get_template_directory_uri() is also used in functions.php to enqueue some stylesheets, and there it generates the correct url beginning with http even if locally.
I don't know hot to fix this, I don't want to change every link here locally, and having to remember to fix it back before putting it online. Sorry for my english.
Edit: I was able to automatically add "http" by escaping the url, with this code
<?php echo esc_url(get_stylesheet_directory_uri() . '/images/myimage.jpg') ?>
This fixes the problem locally, and doesn't affect the output online, therefore I can sync the code like this... still, I don't know why it behaved like this.
Please check WordPress Address (URL) and Site Address (URL) in Settings

wordpress insecure URL fix issue

I am a beginner in PHP & i am in process of converting all my http links to https.
Following is my code footer.php
function css_generator() {
/* #footer_background_image */
.td-footer-wrapper::before {
background-image: url('#footer_background_image');
}
$td_css_compiler->load_setting('footer_background_image');
Where can i apply preg_replace function to replace http link with https?. The value of footer_background_image is always getting generated as http
Thanks
You're looking at this from the wrong way. Wordpress has built-in support for HTTPS for the backend, wich can be enabled in wp-config.php, and for the front-end, wich can be used by changing the URL in your admin->reading page.
If you have a bunch of hardcoded links rather than soft, Wordpress generated links, you can choose to use .htaccess to force the user to change to HTTPS.
Do note that HTTPS data can not be cached and this will may make your site slower for visitors. Depending on the type of site this can be a big deal.

Categories