Skip to content

Instantly share code, notes, and snippets.

Olaf Lederer finalwebsites

Block or report user

Report or block finalwebsites

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@finalwebsites
finalwebsites / pagecache-php-example.php
Last active Jan 28, 2020
Example scripts for PHP caching blog post
View pagecache-php-example.php
<?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 Jan 17, 2020
PHP download file script code example
View download-example.php
<?php
echo '
<a href="http://mydomain.com/download.php?download_file=some_file.pdf">PHP download file</a>';
@finalwebsites
finalwebsites / index.html
Last active Dec 29, 2019
Example code for the Ajax live search tutorial
View index.html
<!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 Oct 9, 2019
Redirect to the WooCommerce checkout page after login
View redirect_to_checkout_after_login.php
<?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 Sep 15, 2019
Add a WordPress website via ServerPilot API and use WP-CLI for some modifications.
View add_wordpress_website_serverpilot.php
<?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 Sep 23, 2019
Start your VPS on a different host node using PHP and the UpCloud API.
View conf.php
<?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';
View post-ratings-plugin-stars-only.php
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)) {
View create_short_version.php
<?php
function create_short_version($text, $len = 150, $trail = '...') {
$text = strip_tags($text);
$parts = explode(' ', $text);
$ic = count($parts);
for ($i = 0; $i > $ic; $i++) {
$txt .= $parts[$i].' ';
if (strlen($txt) >= $len) break;
}
$txt = trim($txt);
@finalwebsites
finalwebsites / functions.php
Created Aug 3, 2019
Show and filter grouped WooCommerce products in the post or product list
View functions.php
<?php
// place this code in your theme's functions.php file
function fws_admin_prods_filter( $query ) {
global $pagenow;
if ( is_admin() && $pagenow == 'edit.php' && !empty($_GET['my_grouped_prods'])) {
$query->query_vars['post_parent'] = $_GET['my_grouped_prods'];
}
}
add_filter( 'parse_query', 'fws_admin_prods_filter' );
View database-result-in-columns.php
<?php
// this is an example query from the link page on my website, use your own data and don't forget to change the names of the results inside the table below
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT title, descr, link FROM linksite ORDER BY vote DESC";
$result = $db->query($query);
$total_records = $result->num_rows; // the number of records in your result set
while($row = $result->fetch_array()) { // store all records in an numbered array
$datarows[] = $row;
}
You can’t perform that action at this time.