Skip to content

Instantly share code, notes, and snippets.

View emre-edu-tech's full-sized avatar

Media Pons emre-edu-tech

View GitHub Profile
@emre-edu-tech
emre-edu-tech / functions.php
Created December 7, 2024 09:36
Add an ACF field after shop item title on Shop or Product Category page template
<?php
add_action('woocommerce_after_shop_loop_item_title', 'add_custom_product_info', 18);
function add_custom_product_info() {
global $product;
$product_unit_price = get_field('unit_price', $product->id);
// Displaying the custom field only when is set with a value
if(!empty($product_unit_price )) {
// Change text-domain with your own.
echo '<p style="color: red; font-weight: 600; font-size: 1.2rem">' . __('Ab ', 'text-domain') . $product_unit_price . '€ / Stück</p>';
}
@emre-edu-tech
emre-edu-tech / functions.php
Created December 7, 2024 08:31
Filter to limit or trim product title using characters or words except on Single Product Template
<?php
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if (!is_singular(array('product')) && get_post_type($id) === 'product') {
// return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
return wp_trim_words($title, 4); // last number = words
} else {
return $title;
}
}
@emre-edu-tech
emre-edu-tech / functions.php
Created December 5, 2024 15:37
This shortcode creates a custom woocommerce order type by an acf field called custom_product_popularity.
<?php
// creating custom woocommerce order by an acf field called custom_product_popularity
function leoshop_add_new_postmeta_orderby( $sortby ) {
$sortby['custom_popularity'] = __( 'Sortieren nach benutzerdefinierten', 'hello-theme-child');
return $sortby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'leoshop_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'leoshop_add_new_postmeta_orderby' );
function leoshop_add_postmeta_ordering_args( $sort_args ) {
@emre-edu-tech
emre-edu-tech / functions.php
Last active December 5, 2024 15:16
Wordpress shortcode for getting the number of products or name for a specific category. Can be used to show number of products for the category.
<?php
// [leoshop_get_products_by_category_id category_id='87' property='cleaning']
add_shortcode('leoshop_get_products_by_category_id', 'leoshop_get_products_by_category_id');
function leoshop_get_products_by_category_id($attr) {
// get the attributes of shortcode
$args = shortcode_atts([
'category_id' => 87,
'property' => 'cat_name',
], $attr );
$term = get_term($args['category_id'], 'product_cat');
@emre-edu-tech
emre-edu-tech / style.css
Last active October 13, 2024 13:33
This is an example main style.css file to use while developing a classic custom WordPress theme.
/*
Theme Name: Media Pons (Required)
Theme URI: https://media-pons.de/mediapons-base-theme/ (Optional)
Author: Media Pons (Optional)
Author URI: https://media-pons.de/ (Optional)
Description: Custom theme that is a base theme for developing example WordPress themes. This base theme uses Tailwindcss. (Optional)
Version: 1.0.0 (Optional)
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html (Optional)
Text Domain: media-pons (Optional - it should match the theme folder name)
@emre-edu-tech
emre-edu-tech / functions.php
Created August 26, 2024 10:33
Elementor Custom Query Filter using New Search Widget. Search widget supports ajax hitting the Rest Endpoint
<?php
// Here we use tax query but meta_query is also possible
// Unfortunately, there is no way to merge Wordpress default search behaviour, meta_query and tax_query using OR
// that I could find.
// Default behaviour for merging these queries are AND.
// Custom Elementor Query for Search
function mpons_custom_property_search_query($query) {
// Get the search term from the WP_Query object
$search_term = $query->get('s');
@emre-edu-tech
emre-edu-tech / plugin-file.php
Last active October 13, 2024 12:06
Main plugin file description or settings for Wordpress to recognize our plugin.
<?php
/**
* @package MPonsCustomPlugin
*/
/*
Plugin Name: MPons Custom Plugin
Plugin URI: https://mediapons.de
Description: This is an example plugin to practice WordPress Plugin Development.
Version: 1.0.0
@emre-edu-tech
emre-edu-tech / my-custom-plugin.php
Last active August 23, 2024 15:22
Simple Wordpress Plugin and Theme security measures using ABSPATH constant and built-in add_action() function.
<?php
/*
* ABSPATH is a constant in WordPress that stores the absolute path to the WordPress installation directory on the server.
* It is defined in the core wp-config.php file like this:
*/
define('ABSPATH', dirname(__FILE__) . '/');
/*
* PURPOSE OF THE ABSPATH CHECK
* This check below guarantees that no one from outside of the WordPress installation should access this file directly.
@emre-edu-tech
emre-edu-tech / simple-wp-theme-screenshot-generator.php
Created August 23, 2024 10:18
Creating a theme screenshot.png file for Wordpress Theme Development.
<?php
// Check if GD is installed
if (!extension_loaded('gd')) {
die("GD library is not installed. Please enable it in your PHP configuration.");
}
// Set image dimensions and properties
$width = 1200;
$height = 900;
$backgroundColor = [240, 240, 240]; // Light gray background
@emre-edu-tech
emre-edu-tech / functions.php
Created August 22, 2024 21:36
Woocommerce - Show price suffix just on single product template but not on RELATED PRODUCTS section
<?php
// Show price suffix just on single product template but not on related products section
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 10, 2 );
function custom_price_suffix( $price, $product ) {
global $woocommerce_loop;
if(is_product() && ($woocommerce_loop['name'] != 'related')) {
$price = $price . ' <span class="leoshop-price-suffix"> Preis inkl. MwSt. & exkl. Versand</span>';
}
return $price;
}