Skip to content

Instantly share code, notes, and snippets.

View maxkostinevich's full-sized avatar
🚧
Work in progress

Max Kostinevich maxkostinevich

🚧
Work in progress
View GitHub Profile
@maxkostinevich
maxkostinevich / newsletterwp-data-faker.php
Last active April 16, 2016 09:56
PHP Data Faker Example
<?php
/**
* PHP Data Faker for NewsletterWP
*
* (c) 2016 Max Kostinevich - All rights reserved
* https://maxkostinevich.com
*/
$output = '';
$table_prefix = 'core_';
@maxkostinevich
maxkostinevich / csv_export.php
Created April 12, 2016 09:32
PHP CSV Export
<?php
/**
* Basic CSV Export
*
* (c) 2016 Max Kostinevich - All rights reserved
* https://maxkostinevich.com
*/
// Do some security check here
@maxkostinevich
maxkostinevich / functions.php
Last active February 11, 2023 20:03
WordPress: Export custom data to CSV
<?php
/**
* Export Data to CSV file
* Could be used in WordPress plugin or theme
*/
// A sample link to Download CSV, could be placed somewhere in plugin settings page
?>
<a href="<?php echo admin_url( 'admin.php?page=myplugin-settings-page' ) ?>&action=download_csv&_wpnonce=<?php echo wp_create_nonce( 'download_csv' )?>" class="page-title-action"><?php _e('Export to CSV','my-plugin-slug');?></a>
@maxkostinevich
maxkostinevich / emitter.js
Created February 16, 2016 10:04
JavaScript Event Emitter
function Emitter() {
this.events = {};
}
Emitter.prototype.on = function (type, listener) {
this.events[type] = this.events[type] || [];
this.events[type].push(listener);
}
@maxkostinevich
maxkostinevich / functions.php
Created January 4, 2016 17:02
PHP: Determine how many bytes sent over http
<?php
// Determine how many bytes sent over http
ignore_user_abort(true);
$handle = fopen($file_path, 'r');
while ( ! feof($handle)) {
echo fread($handle, 4096);
if (connection_aborted()) {
$transfer_success = false;
$bytes_transferred = ftell($handle);
@maxkostinevich
maxkostinevich / functions.php
Created December 29, 2015 19:25
Search in JSON-Array
<?php
function search_json($obj, $field, $value) {
foreach($obj as $item) {
foreach($item as $child) {
if(isset($child[$field]) && ($child[$field] == $value)) {
return $child;
}
}
}
return null;
@maxkostinevich
maxkostinevich / functions.php
Created December 29, 2015 19:24
WP-Admin: Add fields to Gallery Settings
<?php
add_action('print_media_templates', function(){
// define your backbone template;
// the "tmpl-" prefix is required,
// and your input field should have a data-setting attribute
// matching the shortcode name
?>
<script type="text/html" id="tmpl-my-custom-gallery-setting">
<label class="setting">
@maxkostinevich
maxkostinevich / script.js
Created December 29, 2015 19:23
Submit empty checkbox
/* Add before submit action to fix checkbox issue
Change value to "off" for not-checked checkboxes and check them.
In this case value "off" (instead of original value) will be stored.
*/
$('form#post').submit(function(e) {
$('#some-wrapper input[type=checkbox]:not(:checked)').each(function(){
var curItem=$(this);
curItem.val('off');
curItem.prop('checked', true);
@maxkostinevich
maxkostinevich / functions.php
Created December 29, 2015 19:22
Get files on server (Recursive function)
<?php
$host='http://'.$_SERVER['SERVER_NAME'];
function get_files($path,&$files = array()){
global $host;
$dir = opendir($path."/.");
while($item = readdir($dir)){
if(is_file($sub = $path."/".$item)){
$file_path = $host.substr( $path, strlen( $_SERVER[ 'DOCUMENT_ROOT' ] )).'/'.$item;
$file_info =pathinfo($sub);
@maxkostinevich
maxkostinevich / functions.php
Created December 29, 2015 19:14
WP-Admin: Change post title placeholder
<?php
add_filter( 'enter_title_here', 'my_filter' );
function my_filter( $title ){
$screen = get_current_screen();
if ( 'invoice' == $screen->post_type ){
$title = 'Your placeholder here';
}
return $title;
}