Skip to content

Instantly share code, notes, and snippets.

View encoderit-arman's full-sized avatar

Arman H encoderit-arman

View GitHub Profile
@encoderit-arman
encoderit-arman / Php pagination
Created March 7, 2022 09:52
php pagination , wordpress pagination
<?php
$total_pages = $product_data->max_num_pages;
$current_page = (isset($_GET['page'])) ? $_GET['page'] : 1;
if ($current_page == 1) {
$prev_page = false;
} else {
$prev_page = $current_page - 1;
}
if ($current_page == $total_pages) {
@encoderit-arman
encoderit-arman / Save image from any host wordpress
Last active March 2, 2022 11:07
Save image from remote url with wordpress, Save image from csv with wordpress
$post_id = wp_insert_post($array);//create new post and save its id
$img = media_sideload_image( $url, $post_id);//download image to wpsite from url
$img = explode("'",$img)[1];// extract http.... from <img src'http...'>
$attId = attachment_url_to_postid($img);//get id of downloaded image
set_post_thumbnail( $post_id, $attId );//set the given image as featured image for the post
if (isset($_POST["data"])) {
// Get file extension
$file_extension = pathinfo($_FILES["data"]["name"], PATHINFO_EXTENSION);
// Validate file input to check if is not empty
if (!file_exists($_FILES["data"]["tmp_name"])) {
$response = array(
"type" => "error",
"message" => "File input should not be empty."
);
//Saving
add_action('save_post', 'custom_save_function');
//Trashing
add_action('wp_trash_post', 'custom_trash_function');
//Restoring
add_action('untrash_post', 'custom_restore_function');
//Deleting
// inline js
<img src="https://secure.gravatar.com/avatar?d=wavatar" onload="javascript: alert('success')" onerror="javascript: alert('failure')" />
// external js
<img src="https://secure.gravatar.com/avatar?d=wavatar"/>
window.addEventListener("load", event => {
var image = document.querySelector('img');
/** --------------Short version **/
/* WP_HTML_Compression */
function HTML_Compression($str){
$str = preg_replace('/<!--.*?-->/', '', $str);
$str = str_replace(' />', '/>', $str);
$str = preg_replace("((<pre.*?>|<code>).*?(</pre>|</code>)(*SKIP)(*FAIL)"."|\r|\n|\t)is", "", $str);
$str = preg_replace("((<pre.*?>|<code>).*?(</pre>|</code>)(*SKIP)(*FAIL)"."|\s+)is", " ", $str);
return $str;
}
function HTML_Compression_finish($html){
jQuer -
jQuery('body').trigger('update_checkout');
/* to update info on your checkout page, you need to trigger the update_checkout function
so add this in your javascript file for your theme or plugin
*/
/* what this does is update the order review table but what it doesn't do is update shipping costs;
the calculate_shipping function of your shipping class will not be called again;
function namespace_force_individual_cart_items( $cart_item_data, $product_id ) {
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'namespace_force_individual_cart_items', 10, 2 );
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['product_id'] == $product_id) {
WC()->cart->remove_cart_item($cart_item_key);
break;
}
}
function display_product_review($product_id) {
$product = wc_get_product( $product_id );
$comments = get_approved_comments( $product->id );
$product_link = '/product/' . $product->post->post_name . "/#tab-reviews/";
if ( !empty ($comments) ) {
echo $comments[0]->comment_content . '<br><a href="'. $product_link . '">Read more reviews...</a>';
} else {