Wordpress: srcset gets HTTP instead of HTTPS in all posts - wordpress

In Wordpress 4.4 images get automatically a srcset attribute. My problem with this was the following (I solved it while I was writing this question, see my answer below):
in order to transit everything to https, I replaced all the src="http://... references in the posts table by src="https://... (I changed it later to src="//... for supporting both protocols);
the images on all the posts get the correct URL in the src attribute;
however in all the images that get the srcset attribute the URLs in it are always with http:// references.
Why does this happen? Why these URLs don't get my newly updated https:// beginnings?

After searching for a while in the wp-includes folder, the wp_calculate_image_srcset method in the media.php file uses these 2 lines:
$image_baseurl = _wp_upload_dir_baseurl();
$image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
And this $image_baseurl will actually form the new URL for the srcset attribute, i.e. even if the entire URL is in the wp_poststable and used in the src attribute, its beginning won't be used.
This means that if your base url in the wp_options table is still in http://, the images will get that protocol and won't show up by default by your browser when navigating in https.
For solving this, you just need to change the URLs inside the option_value in the wp_options table to https:// or just // if you still want to support both protocols (double slashed). You can do this in a single query:
UPDATE `wp_options`
SET `option_value` = replace(option_value, 'http://', '//')
WHERE `option_name` IN ('siteurl', 'home')

If you don't want to change your WordPress Address (URL) to https then just put this code inside your active themes functions.php file
function codextent_ssl_srcset( $sources ) {
foreach ( $sources as &$source ) {
$source['url'] = set_url_scheme( $source['url'], 'https' );
}
return $sources;
}
add_filter( 'wp_calculate_image_srcset', 'codextent_ssl_srcset' );
** Also add this in top line of wp-config.php file.
$_SERVER['HTTPS'] = 'on';

Change Following Setting in admin under Setting->General:
WordPress Address (URL) : https://yoursitename.com
Site Address (URL) : https://yoursitename.com
And press [Save Changes] button. Finally refresh your page and your image will be displayed on your browser with correct srcset attribute.

Related

Wordpress - URL directory parameter without redirect

I've created a wordpress plugin that uses a shortcode in combination with an URL parameter to load a certain text.
The default page is:
domainname.com/test
[shortcode for plugin]
... default template ...
It can load some text using these parameters:
domainname.com/test?book_id=BWBR0002656&art_id=1
[shortcode for plugin]
... loaded the text for the book_id and art_id ...
I'd like to change the javascript parameter url from the current domainname.com/test?book_id=BWBR0002656&art_id=1 to domainname.com/test/BWBR0002656/1 without triggering a wordpress 404 redirect.
Is this possible to do with the shortcode page still functioning ?
I managed to get it working using this:
add_action( 'init', 'wpa5413_init' );
function wpa5413_init()
{
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
'test/([^/]+)',
// The resulting internal URL: `index.php` because we still use WordPress
// `pagename` because we use this WordPress page
'index.php?pagename=test',
// This is a rather specific URL, so we add it to the top of the list
// Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
'top' );
}
it will load the /test/ page with the args, and the javascript will handle the rest.
Everything after /test/ is accepted without an 404 error. Even: /test/a/b/c/d/e

Can you change the value of the Bloginfo in the general-template of WordPress?

I am facing a great challenge, I am working on a site that has been converted from HTML into WordPress and I have the front-page displaying all the content right but not the other pages as the urls are not pointing correctly. I wonder if instead of changing manually all the URL from 150 pages there is a way that I change the main url path values in wp-includes/general-template.php from this:
<?php bloginfo('template_url'); ?>
into something like the real URL: http://example.com/
In other word by doing so, if successful, I will make only one change on the wp-includes/general-template.php and have all the broken links fixed...?
Anyone has a clue?
Many thanks all....(I am running out of time and options....help is greatly appreciated..:)
Daniele
If I understand your question right, wordpress does not work like that, if you have broken links that point to incorrect domain you need to make the change from phpMyAdmin using this query to replace URLs:
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl');
but make sure to change the table prefix, and do a search on the incorrect url in your database to know what tables have that url, then change table name and column in the query above based on your search.
Found a way around by changing the wp-includes/general-template.php on line 693 from the following:
case 'template_directory':
case 'template_url':
$output = get_template_directory_uri();
to the following:
case 'template_directory':
case 'template_url':
$output = site_url( 'mising_url_path_here' );
now I have fixed all the broken link for js/images/other files and can be viewed.

Change ouput url of WordPress [Gallery] Shortcode

I recently installed ta plugin that now uploads my images from the media library to s3.
I have also FTP's the entire uploads folder to s3 which encompasses about 4000 images.
I have used throughout my site the wordpress gallery shortcode however somewhere and somehow it outputs the siteurl.
How do I change this so that I can override the url to be the one from my S3 bucket?
I will admit I have no idea what I am doing here or where to start and I really will appreciate your help :)
You can filter images src attribute output and replace old url with new url as follow. copy below code to your theme functions.php and replace www.oldurl.com and www.newurl.com with your own urls.
add_filter('wp_get_attachment_image_src', function ($image) {
if(is_array($image)){
$image[0] = str_replace('www.oldurl.com', 'www.newurl.com', $image[0]);
}
return $image;
}, 999);
Looking into the wp_get_upload_dir(), that's a wrapper for wp_upload_dir(), that's again wrapper for _wp_upload_dir(), we see that the upload url can be modified through the upload_url_path option.
Since you're moving all of your uploads to S3, you could try to add your S3 bucket base url into the upload_url_path option.
You should test this first on your dev install, just to see how it works with your current setup.
You might want to make a search & replace via the database.
You can see here how to make a SQL Query to change the Image Path in the posts: 13 Useful WordPress Queries
UPDATE wp_posts SET post_content = REPLACE (post_content, 'src="http://www.oldsiteurl.com', 'src="http://yourcdn.newsiteurl.com');
UPDATE wp_posts SET guid = REPLACE (guid, 'http://www.oldsiteurl.com', 'http://yourcdn.newsiteurl.com') WHERE post_type = 'attachment';

Wordpress - uploading images to one domain but URL should be a different domain

I'm trying to get my head around this. I know Wordpress pretty well but I'm officially confused.
I have a subdomain where users can login to wordpress at dev.mysite.com/wp-admin. When they upload images to dev.mysite.com/wp-content/images/uploads, those files are lsynced to www.mysite.com/wp-content/uploads. I changed wp-config.php so that it will work on either domain.
This works okay except that in wordpress posts, when you upload an image, it uploads the domain as well so it puts in html like and my users have to go in and change dev. to www. manually since "dev." is password protected so the images won't even show up just a password prompt if they forget to do that.
Is there a way to make it so wordpress doesn't include the full path to the image? Where is that in wordpress' guts?
Add this to your themes functions.php:
add_filter( 'wp_get_attachment_url', 'wp_update_attachment_url_domain', 10, 2 );
function wp_update_attachment_url_domain ($url, $post_id)
{
$url = str_ireplace('dev.mysite.com', 'www.mysite.com', $url);
return $url;
}
This will updated the url of the image by replacing the host name.

Wordpress images link

I'm just starting with wordpress. I create blog and right now I have index.php with list of posts. If I add images to posts in admin panel , images have href but they redirect to attachment - not to post. Of course I can use:
has_post_thumbnail()
but this works only if I set it in admin.
My question is : How to create redirect to post as default (not to attachment ) for images added to post.
This is typical of WordPress and is supposed to happen. It is up to the author when inserting media into the page how it is displayed. See the Attachment Settings in the Bottom Right of the Corner. You will want to select None or Custom URL based on your preferences. Yours is currently set to "Media File" Like the screenshot below.
You can add this snippet of code to your themes functions.php file to change the default settings:
function image_overrides() {
update_option('image_default_align', 'center' ); // Changes the alignment
update_option('image_default_link_type', 'none' ); // Changes the Link type
update_option('image_default_size', 'large' ); // Changes the default size
}
add_action('after_setup_theme', 'images_overrides');

Resources