Wordpress/ACF Image Field HTTPS/SSL - wordpress

When using the “image” type custom field, and also having a Return Value of “Image URL”, and then in my template using <img src="<?php the_field('mycustomimage'); ?>">, it’s returning an http URL instead of an https URL, causing mixed content on my site. Other areas of the site are returning an https URL, like when I use wp_get_attachment_image_src(); for example.
How do I get it to return an https URL?

There's a variety of Insecure Content Fixing plugins out there, as well as Simple SSL Setup plugins that work well. There's also some documentation in the Codex that may help.
If neither of those fix your issue, you can simply do a string replace on the returned values. Note you may want to instead use the returning get_ functions instead of the echoing the_ functions for ease of use:
<img src="<?= str_replace( 'http://', 'https://', get_field( 'mycustomimage' ) ); ?>">
Also make sure your site is set to https:// in your General Settings if its not already
Edit:
In regards to your comment: Was the admin secured with https:// when the images were uploaded? Also the insecure content fixer and simple ssl setup plugins should effectively just require activation (or network activate) and basically be good to go.
If those don't work, you've modified all DB records, and everything should be working, you could always do a runtime find and replace, while it's a bit hacky, I have actually used code similar to this for a similar purpose before when some things just "stuck" to an old protocol despite everything saying they should be updated to a new one.
add_action( 'template_redirect', function() use($startTime){
ob_start( function( $buffer ){
if( is_ssl() ) $buffer = str_ireplace( 'src="http://', 'src="https://', $buffer );
return $buffer;
});
});
Note that this will require anything with a src attribute to have an appropriately SSL'd resource, including external resources (iframes, scripts, etc.) - otherwise you'll need to complicate it a bit with preg_replace_callback() and some regular expressions to only grab img tags.

Related

How to remove unused CSS/JS and Improve server response time for WordPress website?

I've optimized my WordPress site performance and also checked its performance using web.dev
But still, have the following issues that I couldn't solve:
Remove unused CSS
Remove unused JS
Initial server response time was short
How can I Solve this issues?
Performance optimisation is a huge theme and probably can't be covered simply in one answer, but I still can point you to the right direction.
Remove unused CSS & JS
First, you have to determine what CSS and JS files you really load on each page, find out where they come from and think about whether you really need them or not. It often happens that a lot of heavy CSS & JS files are being loaded only for the sake of a simple animation that is used once somewhere on website. You can see all the files loaded in browser console, or with the help of services like GT Metrix. If you find assets that you don't need but they're still being loaded, first try to find a source (for example: a theme or a plugin) that loads them. Popular themes and plugins have settings for disabling some parts of assets so it's an easy one. For those that don't have any settings you can try this piece of code, just replace file names with yours.
add_action( 'wp_print_styles', 'remove_styles' );
add_action( 'wp_print_scripts', 'remove_scripts' );
// Remove styles from plugins we don't need
function remove_styles() {
if ( $GLOBALS['pagenow'] != 'wp-login.php' && ! is_user_logged_in() ) {
wp_dequeue_style( 'coblocks-frontend' );
wp_dequeue_style( 'coblocks-frontend-css' );
wp_dequeue_style( 'wp-editor-font' );
wp_dequeue_style( 'duplicate-post' );
wp_dequeue_style( 'thickbox' );
}
}
// Remove scripts from plugins we don't use and don't need
function remove_scripts() {
// just in case we use those in wp-admin, exclude only from front-end loading
if ( ! is_user_logged_in() ) {
wp_dequeue_script( 'coblocks-animation' );
wp_deregister_script( 'coblocks-animation' );
wp_dequeue_script( 'thickbox' );
wp_deregister_script( 'thickbox' );
wp_deregister_script('jquery');
}
}
Ideally you need to keep your CSS and JS as small as possible but it can be tricky in modern WP themes that use a lot of it.
Initial server response time
Long response time mean that your server takes too long to return any data. 99% of the times it is either because of badly-optimised website or a weak server. Latter can be easily solved by moving to another hosting provider or changing hosting plan.
Website optimisations are trickier and may require profiling, especially if you already have proper full page caching installed . Generally, I can describe all the general cases like this:
Slow server response + no caching = try caching first. WP Super Cache, WP Rocket or similar plugins.
Slow server response + caching = try hosting providers with nginx-caching support (basically serving static content on each page load). I can recommend Kinsta or Pressjitsu.
Slow server response + a lot of dynamic content = You can't cache things the same way as regular websites. Here you need an experienced dev who should first profile you website with tools like xhprof or blackfire.io and then gradually work on optimising performance of the worst performing functions.

How to add #anchor at the end of the Wordpress URL if the traffic source is coming from Google search

I have Wordpress and I would like that all visitors who access my site by searching in google (traffic source), go directly to a specific part of the page (focus).
To focus on this part, I need to include the anchor at the end of the url: #welcome-source-google
https://example.com/category/page-or-post/#welcome-source-google
I think I need to insert this code, probably in functions.php:
function organic_source_anchor() {
if (preg_match('/(www\\.)?google\\./', $_SERVER['HTTP_REFERER'])) {
wp_redirect( get_permalink( $postID ) . '#welcome-source-google' );
exit;
}
add_action( 'template_redirect', 'organic_source_anchor' );
I got this code by researching, but since I am not a programmer, I believe there are several errors or maybe I need to put the code out of functions.
A problem that happens is that I didn’t want to disable the cache, as this frame appears to all visitors, but if the source is from google, I wanted the focus on the part this page.

Whati is the diffrence between bloginfo and template_directory_uri?

I want to clear my concept that what is the difference between blog_info() and template_directory_uri() function? Please in details would be helpful!
Thanks
As you can see in the source, get_bloginfo( 'template_directory' ) and get_bloginfo( 'template_url' ) are simply wrappers for get_template_directory_uri():
case 'template_directory':
case 'template_url':
$output = get_template_directory_uri();
break;
IF you check with these links blog_info and template_directory_uri links you can clearly understand what is the difference between these two functions.
Anyway let me explain this for you
template_directory_uri
This function provides you the complete url to the theme directory you are currently using. Suppose if you are using theme x in your wordpress front-end, then when you call template_directory_uri() function it will return http://yourdomainname.com/wp-content/themes/x. This essentialy means that this function returns template directory URI for the current theme being used.
bloginfo
What this function does is that it will return all the information associated with your site which are set in the admin general settings and admin user profile. This function gives you information about the site url, admin email , site name, site description and lot of things. Most of these are available in the General Settings menu in the admin back-end. The bloginfo function accepts an input parameter. If you do not pass any input parameter by default it will show the Site title which is set in the admin back end. You can pass various other inputs like description, url, charset, version etc. These will give the info associated with them. So what bloginfo gives us is, it will provide the information regarding the site.
From bloginfo function we can get template_uri too, just do bloginfo('template_url');
If you prints out both these functions in your php page in wordpress theme, you can clearly found out what is the difference between these two functions. May be go to your index.php and just print out these two:
echo get_template_directory_uri();
bloginfo('name');
Hope this helps you

Google Indexing Non-Existent URLs. WordPress Doesn't Show 404

I was inspecting the Google search results for: "site:mywordpress.org." And found lots or pages indexed that shouldn't exist.
There are two problems here:
I don't know how Google located, crawled, or found these URLs.
Wordpress doesn't show a 404 error, so it looks like duplicate content.
I tried the Wordpress support forums, but no one responded. I also have not been able to find anyone reporting this problem. Here's an example of what I am seeing:
mywordpress.org/blog-post/
mywordpress.org/blog-post/1363035032000/
I've added a canonical link reference to the head and I've been doing lots of Google WMT removal requests, but I'm still seeing some results like this.
I've tested this on a few wordpress installs, it seems that if you add any string of numbers to the end of a permalink it will still display the content rather than showing a 404 error.
I also noticed that the number that is being added to the permalinks is the UNIX time stamp with a few zeros on the end. As of this post the current UNIX time stamp is: 1363035971.
I'm looking for some advice on what I should do. I'm particularly interested in a PHP function that would check the url to see if there was a string of numbers at the end, and if there was, 301 redirect it to the right permalink. I'd also value any input on why Google is finding these wrong urls and if the UNIX time stamp is the clue.
Did you check if some plugin is causing this? Also check your Permalink Settings under Settings > Permalinks
Until you locate the source of your problem, you could try to get rid of it by using Redirect plugin.
This plugin has many features, two features important to your case are:
All URLs can be redirected, not just ones that don't exist
Full regular expression support
So with the help of regular expression, you would probably be able to redirect URL with numbers to a correct URL.
I had the same issue and found the solution to this issue.
Just add this to functions.php
add_action( 'template_redirect', 'so16179138_template_redirect', 0 );
function so16179138_template_redirect()
{
if( is_singular() )
{
global $post, $page;
$num_pages = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
if( $page > $num_pages ){
include( get_template_directory() . '/404.php' );
exit;
}
}
}

How to set nofollow rel attribute to all outbound links in WordPress? Any Plugin?

I want to know how to automatically set all links to nofollow in Wordpress. Is there a WP Plugin that will make all my outbound links nofollow?
Help is greatly appreciated!
If you are make this change for SEO optimization, you can't make the change only with JS because the google bots don't read the content generated by Javascript.
But, you can add a filter in your function.php like this:
function rel_nofollow( $content ) {
return preg_replace_callback( '/<a[^>]+/', 'rel_nofollow_callback', $content );
}
add_filter( 'the_content', 'rel_nofollow', 99999 );
function rel_nofollow_callback( $matches ) {
$link = $matches[0];
$exclude = '('. home_url() .'|http://([^.]+\.)?(wp.org|wp.com))';
if ( preg_match( '#href=\S('. $exclude .')#i', $link ) )
return $link;
if ( strpos( $link, 'rel=' ) === false ) {
$link = preg_replace( '/(?<=<a\s)/', 'rel="nofollow" ', $link );
} elseif ( preg_match( '#rel=\S(?!nofollow)#i', $link ) ) {
$link = preg_replace( '#(?<=rel=.)#', 'nofollow ', $link );
}
return $link;
}
This function set all links into the post to the attribute: rel="nofollow", but, if you want change all the site maybe you must try with this plugin
A number of years later, the details changed but the question remains.
For one thing, the number of relevant rel attributes has grown:
nofollow alternatives sponsored and ugc
noopener, strongly suggested for security
noreferrer, less relevant with modern browser versions
external, XHTML valid, link to an external (different) site; not nofollow nor target="_blank", so dubious utility
somewhat related noindex
Disregard numerous examples in older posts of code snippets to tune rel= attributes, as simply hacking WordPress PHP is quite undesirable these days. With frequent and (optionally) automated core software and plugin updates, and with security plugins checking all the code for non-standard mods, let's focus on the UI and plugin configuration methods instead.
While the Classic editor gives an option to Open link in a new tab, current version of the Gutenberg editor in combination with Yoast also provides UI to select nofollow or sponsored.
For more controls there are plugins. The one mentioned in earlier answers is still around, named External Links and with 80K installs and an exclusive focus on the link attributes, plus link icons and WPMU.
A much more popular plugin, with 2M+ installs is All in One SEO (AIOSEO), which among many other features provides access to noindex and nofollow.
Perhaps the most popular among SEO plugins, with 5M+ installs is Yoast SEO, but current version provides no additional help with bulk nofollow, though it adds noopener, as documented, but apparently also noreferrer to external links automatically.
Discussion so far related to external links, as nofollow or noopener are not used for internal links. Instead, concerns about duplicate content or low quality (thin content) pages raise questions about which parts of the site navigation and interlinking to index. Yoast shines here, with fine grain support for indexing author, tag or category links and pages.
Beyond search engine interpretation of individual links, rel=canonical tag allows merging together treatment of multiple internal links that actually lead to the same page or post. We are only guessing how Google interprets our 'hints' about links and hope that Yoast and WordPress produce a structure that is digestible.
Whether a particular subset of the rel and SEO plugins mentioned, in combination with a particular version of your favorite editor can work reliably alongside each other, and how to sequence their combined workflow is entirely TBD. A safe and consistent way of iteratively making specific adjustments to subsets of external and groupings of internal links, with a special treatment of affiliate and associate links is TBD. How to tackle a substantial site already in production, and retroactively apply select auto-magical adjustments is TBD. Any detailed suggestions, links to specific information and best of all, actual experiences would be rather welcome.
How browsers and spiders actually traverse a WordPress site is controlled at a different system level. The popular plugin for HTTP redirection is conveniently called Redirection. URL rewriting in Apache web server is configured via .htaccess file. cPanel and several system plugins, e.g. Really Simple SSL or WordFence, have automation in their UI for some of the settings, though conflicts may need to be resolved manually. These subjects are best covered in depth elsewhere.
For coding affectionados, two earlier attempts at different approaches:
Much tighter jQuery implementation, compared with the one below.
Really old direct hack of the entire page HTML, fetched from DOM.
Within WordPress you can select to open a link in a new window. When this is selected WordPress adds an attribute target="_blank". This is what I usually do with outbound links. If so you can use jQuery to add the attribute rel="nofollow" like this
<script type="text/javascript">
$(document).ready(function() {
$('a[target="_blank"]').attr('rel', 'nofollow');
});
</script>

Resources