Skip to content

Instantly share code, notes, and snippets.

View finalwebsites's full-sized avatar

Olaf Lederer finalwebsites

View GitHub Profile
@finalwebsites
finalwebsites / stock_status_names.php
Created July 3, 2020 19:24
Different stock status names for your WooCommerce product detail page
<?php
/*
Using this function you can use different values on the frontend while keeping the original strings or translations.
How to use:
Place the PHP code below into the functions.php file from your WordPress child theme.
If you like, change the strings inside the quotes for the $status variables.
The action hook will place the information below the add to cart button (based on the original WooCommerce template files).
*/
@finalwebsites
finalwebsites / functions.php
Created March 25, 2020 06:28
Store Elementor form submissions using a custom post type
<?php
// place this code into you child theme's functions.php file
// Do you use Advanced Custom Fields? Uncomment the next row, otherwise the custom fields meta box doesn't show up in your edit post screen
//add_filter('acf/settings/remove_wp_meta_box', '__return_false');
add_action( 'init', 'fws_elementor_submissions_post_type' );
function fws_elementor_submissions_post_type() {
$args = array(
'public' => false,
@finalwebsites
finalwebsites / check_fix_url.php
Created March 3, 2020 09:23
Use this function to fix the missing protocol part from an URL
<?php
function check_url($url) {
if (substr($url, 0, 4) != 'http') {
$url = 'http://'.$url;
}
$response = get_headers($url);
if ($response[0] == 'HTTP/1.1 200 OK') {
return $url;
} elseif (in_array(substr($response[0], 9, 3), array(302, 301))) {
foreach ($response as $r) {
@finalwebsites
finalwebsites / pagecache-php-example.php
Last active January 28, 2020 14:14
Example scripts for PHP caching blog post
<?php
require_once __DIR__.'/../vendor/autoload.php';
try {
$cache = new PageCache\PageCache();
$cache->config()
->setCachePath('/your/cache_path/')
->setEnableLog(true)
->setCacheExpirationInSeconds(86400);
$cache->init();
} catch (\Exception $e) {
@finalwebsites
finalwebsites / download-example.php
Last active December 21, 2022 01:26
PHP download file script code example
<?php
echo '
<a href="http://mydomain.com/download.php?download_file=some_file.pdf">PHP download file</a>';
@finalwebsites
finalwebsites / index.html
Last active July 7, 2022 12:20
Example code for the Ajax live search tutorial
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ajax Live Search - DEMO</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link href="starter-template.css" rel="stylesheet">
@finalwebsites
finalwebsites / redirect_to_checkout_after_login.php
Last active October 9, 2019 13:18
Redirect to the WooCommerce checkout page after login
<?php
add_filter( 'woocommerce_registration_error_email_exists', function( $html ) {
$url = wc_get_page_permalink( 'myaccount' );
$url = add_query_arg( 'redirect_checkout', 1, $url );
$html = str_replace( 'Please log in', '<a href="'.$url.'"><strong>Please log in</strong></a>', $html );
return $html;
} );
add_filter( 'woocommerce_login_redirect', function( $redirect, $user ) {
if ( $_GET['redirect_checkout'] ) {
@finalwebsites
finalwebsites / add_wordpress_website_serverpilot.php
Last active September 15, 2019 19:12
Add a WordPress website via ServerPilot API and use WP-CLI for some modifications.
<?php
include_once 'ServerPilot.php';
include_once 'ServerPilotException.php';
function randomPassword($pw_length = 8) {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array();
$alphaLength = strlen($alphabet) - 1;
for ($i = 0; $i < $pw_length; $i++) {
@finalwebsites
finalwebsites / conf.php
Last active September 23, 2019 11:11
Start your VPS on a different host node using PHP and the UpCloud API.
<?php
// Enter here your server UID and the login / password
$uid = 'xxx-xxx-xxx-xxx-xxx';
$host = 'https://api.upcloud.com/1.2/server/'.$uid;
$username = 'XXX';
$password = 'XXXXXXXX';
function showRating($id, $css_class = 'post-ratings') {
global $wpdb;
$path = plugins_url('wp-postratings/images/'.get_option('postratings_image'));
$rating = $wpdb->get_var("SELECT AVG(rating_rating) AS rating FROM $wpdb->ratings WHERE rating_postid = $id");
$i = 0;
$html = '
<span class="'.$css_class.'">';
if (!empty($rating)) {
while ($i > floor($rating)) {