Skip to content

Instantly share code, notes, and snippets.

View klebinhopk's full-sized avatar
🏠
Working from home

Kleber Pereira klebinhopk

🏠
Working from home
View GitHub Profile
$category_ids = array(); //Inicializa a array onde guardaremos os IDS
$fromDB = term_exists($string['category'][$i], 'category' ); // Busca a categoria no banco. Se existir, teremos o ID dela, se não, NULL
if($fromDB === NULL) // Se for NULL quer dizer que é uma categoria nova que precisa ser adicionada
{
$term = wp_insert_term($string['category'][$i], 'category'); // Adiciona a categoria nova no banco e guarda o ID gerado;
$category_ids[] = $term['term_id'];
} else {
$category_ids[] = $fromDB['term_id']; // Guarda no array o ID da categoria que já existe no banco
}
@klebinhopk
klebinhopk / functions.php
Created July 20, 2016 22:40 — forked from lenivene/functions.php
Insert thumbnail (attachment) post by url
<?php
/**
* Insert thumbnail by URL
* Is very good to execute wp_insert_post
*
* @author Lenivene Bezerra
* @param string $url Image url external or no
* @param int $post_ID Post id to insert thumbnail
* @return ID Thumbnail | WP_Error
*/
@klebinhopk
klebinhopk / php-html-css-js-minifier.php
Created July 24, 2016 22:03 — forked from taufik-nurrohman/php-html-css-js-minifier.php
PHP Function to Minify HTML, CSS and JavaScript
<?php
/**
* ----------------------------------------------------------------------------------------
* Based on `https://github.com/mecha-cms/mecha-cms/blob/master/engine/plug/converter.php`
* ----------------------------------------------------------------------------------------
*/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Auto Detect</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
@klebinhopk
klebinhopk / insert-posts.php
Created November 11, 2017 01:31
Insert a post into WordPress from an external script
<?php
// Load WordPress
require_once 'path/to/www/wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
// Set the timezone so times are calculated correctly
date_default_timezone_set('Europe/London');
// Create post
<?php
header('Content-Type: text/html; charset=utf-8');
set_time_limit(0);
require "classes/SimpleImage.php";
require "funcoes/functions.php";
//FAZ A CONEXÃO COM O BANCO
$conn = conn();
@klebinhopk
klebinhopk / scraper.js
Created June 21, 2018 18:02
Google news scraper
const request = require('request');
const cheerio = require('cheerio');
var searchTerm = 'tech';
var searchUrl = 'https://www.google.com/search?q=' + searchTerm + '&tbm=nws';
var savedData = [];
request(searchUrl, function(err, response, html) {
// First we'll check to make sure no errors occurred when making the request
if (err) {
@klebinhopk
klebinhopk / gist:204975332e85e27afa2507be107479b6
Created November 18, 2018 12:52
Codeigniter, Model, PHP: Check if user already exists in database
public function user_exists($user_id)
{
$this->db->where('user_id', $user_id);
$query = $this->db->get('sometablenamehere');
if($query->num_rows >= 1)
{
//if query finds one row relating to this user then execute code accordingly here
}
}
@klebinhopk
klebinhopk / gist:360c17a6fea284620bfba6f8e13e1466
Created November 18, 2018 12:57
PHP, PDO, EXPORT, EXPORT TO CSV: Use PDO to create a standalone php script for use in cron jobs where codeigniter urls can't be run by cron
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
die(':)');
}
echo "Cron is running";
$hostname = 'localhost';
$username = 'cl50-whiskey_p';
$password = 'rmBRsm!kK';
@klebinhopk
klebinhopk / gist:41d87f1aff15059869718e09730b335a
Created November 18, 2018 12:57
MYSQL, EXPORT CSV: Export to csv straight from MySQL - note MySQL may not have permission to write this file to your app directory
$app_directory = dirname(BASEPATH);
$export_location = $app_directory . '/assets/exports/';
$filename = "export-" . date('Y-m-d H-i-s') . ".csv";
$export_location = $export_location . $filename;
$query = $this->db->query("
SELECT users.id, users.firstname, users.lastname, users.email, users.dob, messages.line1, messages.line2, messages.line3, messages.barcode_selected, address.my_address, address.firstname, address.lastname, address.address1, address.address2, address.town, address.postcode, address.datecreated
FROM users
JOIN messages