Skip to content

Instantly share code, notes, and snippets.

View man4toman's full-sized avatar
:octocat:
A Bit Git!

Morteza Geransayeh man4toman

:octocat:
A Bit Git!
View GitHub Profile
@man4toman
man4toman / wp-sanitize-array.php
Last active October 4, 2018 11:39
Sanitizing Arrays in WordPress
<?php
function wp_sanitize_array( $input ) {
return array_map( function( $val ) {
return sanitize_text_field( $val );
}, $input );
}
@man4toman
man4toman / get-image-from-url.php
Last active October 4, 2018 11:40
Some methods for get/leech and save images from url.
<?php
/*
* Method 1: use file_put_contents, but it disabled in many shared hosts
*/
$url = "http://site.ltd/test-image.jpg";
file_put_contents("/path/to/folder/" . basename($url), file_get_contents($url));
/*
@man4toman
man4toman / check-it.php
Last active October 4, 2018 11:40
Check file exists from url
<?php
/*
* Method 1: use curl, it must return 200 for http return code
*/
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if( $httpCode == 200 ){ echo "File exists"; }
@man4toman
man4toman / crx-downloader.php
Created October 1, 2018 10:06
Download .crx Chrome Extension file with PHP
<?php
if(isset($_POST["submit"])){
if(isset($_POST["url"]) && filter_var($_POST["url"], FILTER_VALIDATE_URL)){
//Get extension id
$pos = strrpos($_POST["url"], '/');
$end = $pos === false ? $_POST["url"] : substr($_POST["url"], $pos + 1);
$id = substr($end, 0, strpos($end, "?"));
$durl = "https://clients2.google.com/service/update2/crx?response=redirect&x=id%3D".$id."%26uc&prodversion=32";
//Simply get!
header("Location: $durl");
@man4toman
man4toman / download-and-attach.php
Last active October 4, 2018 11:40
Download an image from the URL and attach it to a post in WordPress
<?php
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
if(!function_exists('wp_get_current_user')) {
include(ABSPATH . "wp-includes/pluggable.php");
}
$url = "http://xyz.ltd/filenam.png";//Just a sample
$file = array();
@man4toman
man4toman / excerpts-in-pages.php
Last active October 4, 2018 11:41
Add excerpts to Pages in WordPress
<?php
/**
* More parameters:
* @link http://codex.wordpress.org/Function_Reference/add_post_type_support
**/
add_post_type_support( 'page', 'excerpt' );
@man4toman
man4toman / editor-post-excerpt.php
Created October 4, 2018 11:38
Add tinymce editor for post excerpt
<?php
add_action( 'add_meta_boxes', array ( 'add_tinymce_to_excerpt', 'switch_boxes' ) );
class add_tinymce_to_excerpt{
public static function switch_boxes(){
if ( ! post_type_supports( $GLOBALS['post']->post_type, 'excerpt' ) ){
return;
}
remove_meta_box( 'postexcerpt', '', 'normal' );
add_meta_box( 'postexcerpt2', 'Excerpt' , array ( __CLASS__, 'show' ), null, 'normal', 'core' );
}
@man4toman
man4toman / related-products.php
Created October 8, 2018 06:31
List related products in single prodcut pgae
@man4toman
man4toman / www-post-thumb.php
Created October 15, 2018 11:19 — forked from gmazzap/www-post-thumb.php
WordPress plugin that allow to use an external image url as featured image.
<?php namespace GM\WWWPostThumbnail;
/**
* Plugin Name: WWW Post Thumbnail
* Description: Allow to use an external image url as featured image.
* Plugin URI: https://gist.github.com/Giuseppe-Mazzapica/928bc22e5f49a654cf7c
* Author: Giuseppe Mazzapica
* Author URI: https://github.com/Giuseppe-Mazzapica
* License: MIT
* Version: 0.1.0
*
@man4toman
man4toman / meta-exists.php
Created October 16, 2018 06:48
Determine if a meta key is set for a given object
<?php
/**
* @link https://developer.wordpress.org/reference/functions/get_post_meta/
**/
// Only for post types
if( get_post_meta( $post_id, '_meta_key', true ) ) {
// do something
}