Skip to content

Instantly share code, notes, and snippets.

Avatar

Olaf Lederer finalwebsites

View GitHub Profile
@finalwebsites
finalwebsites / categories_with_childs.php
Created Aug 23, 2020
Hierarchical categories for the plugin WooCommerce Google Product feed
View categories_with_childs.php
<?php
add_filter( 'woocommerce_gpf_elements_google', function ( $elements, $product_id, $variation_id ) {
$elements['product_type'] = array_map( function ( $item ) {
if ( empty( $item->parent ) ) {
return $item->name;
}
$parent_list = array_reverse( get_ancestors( $item->term_id, 'product_cat', 'taxonomy' ) );
$parent_list = array_map( function ( $term_id ) {
$term = get_term( $term_id, 'product_cat' );
@finalwebsites
finalwebsites / cf7_recaptcha.php
Created Aug 23, 2020
Dequeue cf7 JavaScript and reCaptcha scripts and preventing them from loading everywhere.
View cf7_recaptcha.php
<?php
function my_register_cf7_js() {
// Dequeue cf7 and recaptcha scripts, preventing them from loading everywhere
add_filter( 'wpcf7_load_js', '__return_false' ); // Might as well use their filter
wp_dequeue_script( 'google-recaptcha' );
// If current post has cf7 shortcode, enqueue!
global $post;
if ( isset( $post->post_content ) AND has_shortcode( $post->post_content, 'contact-form-7' ) ) {
@finalwebsites
finalwebsites / add-content-storefront.php
Last active Aug 16, 2020
Voorbeelden voor blogpost over WordPress action en filter hooks https://childtheme.nl/wordpress-action-filter-hooks/
View add-content-storefront.php
<?php
add_action( 'storefront_header', 'ctheme_storefront_header_content', 40 );
function ctheme_storefront_header_content() {
echo '
<div class="header-usp">
Gratis verzending - Voor 14 uur besteld? Volgende dag bezorgt.
</div>';
}
@finalwebsites
finalwebsites / stock_status_names.php
Created Jul 3, 2020
Different stock status names for your WooCommerce product detail page
View stock_status_names.php
<?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 Mar 25, 2020
Store Elementor form submissions using a custom post type
View functions.php
<?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 Mar 3, 2020
Use this function to fix the missing protocol part from an URL
View check_fix_url.php
<?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 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 Feb 29, 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'] ) {
You can’t perform that action at this time.