Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
A Bit Git!

Morteza man4toman

:octocat:
A Bit Git!
View GitHub Profile
@man4toman
man4toman / crx-downloader.php
Created Oct 1, 2018
Download .crx Chrome Extension file with PHP
View crx-downloader.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 / editor-post-excerpt.php
Created Oct 4, 2018
Add tinymce editor for post excerpt
View editor-post-excerpt.php
<?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 / wp-sanitize-array.php
Last active Oct 4, 2018
Sanitizing Arrays in WordPress
View wp-sanitize-array.php
<?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 Oct 4, 2018
Some methods for get/leech and save images from url.
View get-image-from-url.php
<?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 / download-and-attach.php
Last active Oct 4, 2018
Download an image from the URL and attach it to a post in WordPress
View download-and-attach.php
<?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 / check-it.php
Last active Oct 4, 2018
Check file exists from url
View check-it.php
<?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 / excerpts-in-pages.php
Last active Oct 4, 2018
Add excerpts to Pages in WordPress
View excerpts-in-pages.php
<?php
/**
* More parameters:
* @link http://codex.wordpress.org/Function_Reference/add_post_type_support
**/
add_post_type_support( 'page', 'excerpt' );
@man4toman
man4toman / related-products.php
Created Oct 8, 2018
List related products in single prodcut pgae
View related-products.php
@man4toman
man4toman / www-post-thumb.php
Created Oct 15, 2018 — forked from gmazzap/www-post-thumb.php
WordPress plugin that allow to use an external image url as featured image.
View www-post-thumb.php
<?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 Oct 16, 2018
Determine if a meta key is set for a given object
View meta-exists.php
<?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
}