Using WordPress ‘filter_SSL()’ PHP function

The filter_SSL() WordPress PHP function formats a URL to use https. It can be particularly useful when you need to ensure that a specific URL is always accessed securely.

Usage

You can use this function to convert a given URL to its https version. Here’s a generic example:

$url = "http://example.com";
$secure_url = filter_SSL($url);
echo $secure_url; // Outputs: https://example.com

Parameters

  • $url (string): The URL that needs to be converted to https.

More information

See WordPress Developer Resources: filter_SSL()

Please note that the filter_SSL() function is not natively available in WordPress. It’s a custom function that you can create in your theme or plugin. It’s a common practice to create such a function when you need to enforce https on specific URLs.

Examples

Changing a website URL to https

If your website has been recently moved to https and you have a URL stored in your database, you can use filter_SSL() to ensure the URL is accessed securely.

// Assume you have a URL from the database
$db_url = "http://old-website.com";

// Use the filter_SSL function to convert it to https
$secure_db_url = filter_SSL($db_url);

// Now, you can use the secure URL for any purpose
echo $secure_db_url; // Outputs: https://old-website.com

When creating links to other pages on your site, use filter_SSL() to ensure they’re always accessed securely.

// Assume you have a relative URL to a page on your site
$relative_url = "/about-us";

// Generate the full URL
$full_url = "http://my-website.com" . $relative_url;

// Use the filter_SSL function to convert it to https
$secure_url = filter_SSL($full_url);

// Now, you can use the secure URL in your HTML
echo '<a href="' . $secure_url . '">About Us</a>'; // Outputs: <a href="https://my-website.com/about-us">About Us</a>

Converting URLs in an array

If you have an array of URLs that need to be converted to https, you can use filter_SSL() with array_map().

// Assume you have an array of URLs
$urls = ["http://website1.com", "http://website2.com", "http://website3.com"];

// Use array_map with filter_SSL to convert all URLs to https
$secure_urls = array_map('filter_SSL', $urls);

// Now, all URLs in $secure_urls are https
print_r($secure_urls); 
/* Outputs:
Array
(
    [0] => https://website1.com
    [1] => https://website2.com
    [2] => https://website3.com
)
*/

Working with URLs from a form submission

If you’re handling a form submission that includes a URL, use filter_SSL() to ensure the URL is secure before storing it in the database.

// Assume $_POST['website'] contains a URL from a form submission
$submitted_url = $_POST['website'];

// Use the filter_SSL function to convert it to https
$secure_url = filter_SSL($submitted_url);

// Now, you can store $secure_url in the database