Change ouput url of WordPress [Gallery] Shortcode - wordpress

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';

Related

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.

Wordpress: Serve images from a CDN

I use a CDN to serve my images, which I upload into Wordpress. My settings were:
It worked perfectly but it doesn't work anymore since I updated to Wordpress 4.5. This are my settings now:
This filter doesn't work either:
function my_cdn_upload_url() {
return 'http://media.mydomain.com';
}
add_filter( 'pre_option_upload_url_path', 'my_cdn_upload_url' );
Does anyone know how should I serve my images from a CDN?
Thank you.
Looks like you upgraded from quite an old WP version, didn't you? Uploads folder and path options were removed in Settings -> Media a long time ago.
So what does your real path for uploads look like? It's not a standard domain.com/wp-content/uploads// correct?
If yes - where do new images get uploaded to after the upgrade?
Try playing around with https://wordpress.org/plugins/custom-upload-dir/ and see if it helps get your images back to CDN.
The easiest way is editing the content of "upload_url_path" in the wp_options table:
upload_url_path > http://media.mydomain.com
Thank you!
The problem with the approaches above are you won't have file write permissions to upload images to your CDN.
Another approach is to upload all of your images to somewhere - e.g. Amazon S3, or keep them on affiliate network CDNs - and then store your image URLs in postmeta for your posts or products.
You then need to alter your theme files to pull the images from the postmeta fields instead of from the featured image or thumbnail fields.
FYI - this approach will massively speed up imports since WordPress will create multiple sizes of images using up CPU and disk space.
I created a plugin to solve this:
https://www.wpintense.com/product/external-images/
There is a hidden admin page where you can find all the options:
http://localhost/wp-admin/options.php
Find the upload_url_path option and set a value.
Note:
After you give it value, then the "Store uploads in this folder" which is the upload_path, and "Full URL path to files" which is the upload_url_path options will also appear on the Media Settings page:
http://localhost/wp-admin/options-media.php
The options only appear on the Media Settings page if a value is already set. This is the code in WordPress Core that defines how it works:
/*
* If upload_url_path is not the default (empty),
* or upload_path is not the default ('wp-content/uploads' or empty),
* they can be edited, otherwise they're locked.
*/
if ( get_option( 'upload_url_path' ) || get_option( 'upload_path' ) && 'wp-content/uploads' !== get_option( 'upload_path' ) ) :
BEST WAY is to use w3 total cache.
It has a built in cdn support and of course you can use it's main functionality , caching , most useful to speedup website.

Wordpress: srcset gets HTTP instead of HTTPS in all posts

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.

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.

Changing paths to images in Wordpress

I recently moved a friends blog onto his new web hosts but unfortunately the images are not working.
This is due to the old host having the following path for images:
http://www.example.com/blog/wp-content/uploads/2009/07/imagename.jpg
The new host uses a different layout and has this path for the file:
http://www.example.com/wp-content/uploads/2009/07/imagename.jpg
'Blog' has been removed.
Does anyone know the easiest way to fix this issue?
Use this query on your database:
UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com/blog','http://www.new-domain.com');
I've found this tutorial on Exporting and Importing Wordpress very helpful in such situations.
Search RegEx is a good plugin to be able to search and replace with Grep through all posts and pages. Also look in template files for hardcoded links in the form of <>php bloginfo(); ?> for the unneeded /blog/ in the path.
Search replace is a good plugin as well as Search RegEx. Especially if your data to replace is widespread throughout your site. http://wordpress.org/extend/plugins/search-and-replace/

Resources