Skip to content

Instantly share code, notes, and snippets.

View fgrweb's full-sized avatar

Fernando Garcia Rebolledo fgrweb

View GitHub Profile
@fgrweb
fgrweb / 0_reuse_code.js
Created May 30, 2017 11:46
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@fgrweb
fgrweb / genesis-open-graph.php
Created February 3, 2018 10:35 — forked from bradonomics/genesis-open-graph.php
Adding Open Graph Meta Tags to the Genesis Theme Head
<?php
//* Call the First Image in a Post (Used in the Open Graph Call Below)
function catch_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
@fgrweb
fgrweb / create-table.php
Created August 19, 2019 10:02
Snippet to create WordPress table with function dbDelta()
global $wpdb;
$table_name = $wpdb->prefix . 'prueba_tabla';
$wpdb_collate = $wpdb->collate;
$sql =
"CREATE TABLE {$table_name} (
id mediumint(8) unsigned NOT NULL auto_increment ,
nombre varchar(255) NULL,
PRIMARY KEY (id),
KEY first (nombre)
)
@fgrweb
fgrweb / get-api-curl.php
Created September 13, 2019 15:07
Get WordPress API data with curl, sending Barer Token
$curl = curl_init();
curl_setopt_array(
$curl,
array(
CURLOPT_URL => "https://example.com/wp-json/wp/v2/evento?fields=id,title,modified_gmt,validate,status,from,until,location,web&per_page=20",
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
@fgrweb
fgrweb / uploads_produccion.txt
Last active July 19, 2022 22:06
Código en .htaccess para que lea los archivos de medios del sitio en producción
RewriteCond %{REQUEST_URI} ^/wp-content/uploads/[^\/]*/.*$
RewriteRule ^(.*)$ https://sitioproduccion.com/$1 [QSA,L]
# Para entornos NGINX me ha funcionado poner lo siguiente en la configuración
location ~* ^.+\.(svg|svgz|jpg|jpeg|gif|png|ico|bmp|pdf)$ {
try_files $uri @image_fallback;
}
location @image_fallback {
@fgrweb
fgrweb / show-hide-payment
Last active January 23, 2022 07:40
Mostrar/ocultar métodos de pago en función de las variaciones de un producto en WooCommerce
add_filter( 'woocommerce_available_payment_gateways', 'fgr_desactivar_metodos_de_pago' );
function fgr_desactivar_metodos_de_pago( $available_gateways ) {
if ( ! is_checkout() ) return $available_gateways;
$unset = false;
$items = WC()->cart->get_cart();
foreach ( $items as $item => $values ) {
$producto = $values['data'];
$attribute = $producto->get_attribute('pa_atributo');
if ( 'Mi atributo' === $attribute ) {
@fgrweb
fgrweb / stripslashes_array.php
Last active February 9, 2022 18:52
Grabar de manera serializada un array en un post meta de WordPress, "limpiando" primero las comillas simples del array
function stripslashes_deep( $value ) {
$value = is_array( $value ) ? array_map( 'stripslashes_deep', $value ) : stripslashes( $value );
return $value;
}
function fgr_grabar_meta( $post_id, $array_data ) {
$new_data = maybe_serialize( stripslashes_deep( $array_sata ) );
update_post_meta( $post_id, 'fgr_meta', $new_data );
}
@fgrweb
fgrweb / compress-wp-content
Created March 18, 2022 18:47
Comprimir por consola excluyendo un subdirectorio (en este caso uploads)
tar -zcvf wp-content.tar.gz --exclude="wp-content/uploads" wp-content/
@fgrweb
fgrweb / woocommerce-delete-order-from-db.txt
Last active June 6, 2022 07:44
Bulk delete WooCommerce orders
DELETE FROM wp_woocommerce_order_itemmeta;
DELETE FROM wp_woocommerce_order_items;
DELETE FROM wp_comments WHERE comment_type = 'order_note';
DELETE FROM wp_postmeta WHERE post_id IN ( SELECT ID FROM wp_posts WHERE post_type = 'shop_order' );
DELETE FROM wp_posts WHERE post_type = 'shop_order';
@fgrweb
fgrweb / wp-create-user.txt
Last active June 6, 2022 07:44
Create WordPress admin account DB
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES ('newuserid', MD5('pass123'), 'First Name Last Name', 'email@email.com', '0');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');