Skip to content

Instantly share code, notes, and snippets.

View sergeliatko's full-sized avatar
💭
Looking for talented PHP / JavaScript / WordPress developers. Apply now!

Serge Liatko sergeliatko

💭
Looking for talented PHP / JavaScript / WordPress developers. Apply now!
View GitHub Profile
<?php
/**
* Load plugin text domain.
*/
add_action( 'plugins_loaded', function () {
load_plugin_textdomain( 'text-domain', false, basename( dirname( __FILE__ ) . '/languages' ) );
}, 10, 0 );
@sergeliatko
sergeliatko / set_images_alt_text_from_title.php
Created July 14, 2018 20:59
SQL query to set attachment image alt text from image title.
<?php
/**
* Sets _wp_attachment_image_alt meta key for attachment images based on title.
*
* @return array
*/
function set_images_alt_text_from_title() {
global $wpdb;
@sergeliatko
sergeliatko / autoload.php
Last active June 21, 2018 16:34
Autoloader for classes using WordPress standards.
<?php
/**
* WordPress classes autoloader.
*/
spl_autoload_register( function ( $class ) {
if ( file_exists(
$file = join(
DIRECTORY_SEPARATOR,
array(
@sergeliatko
sergeliatko / autoload.php
Last active June 21, 2018 16:18
PSR4 Autoloader
<?php
/**
* PSR-4 Autoloader.
*/
spl_autoload_register( function ( $class ) {
// validate namespace
if (
strncmp(
@sergeliatko
sergeliatko / Singleton.php
Last active April 11, 2018 14:15
Singleton trait
<?php
trait Singleton {
/**
* @var static $instance
*/
protected static $instance;
/**
* @return static
@sergeliatko
sergeliatko / wp_import_image_from_url.php
Last active January 16, 2018 23:36
PHP function to import an image from URL into WordPress media library
<?php
/**
* Imports an image from URL into media libarary.
*
* @param string $url The image URL
* @param string|null $title The title to use for image in the media library (also will be added as ALT text)
* @param int $post_parent_id Post parent ID if applicable
*
* @return int|\WP_Error ID of the newly created attachment or Wp_Error object on failure
@sergeliatko
sergeliatko / autoload.php
Created November 23, 2017 17:58
Autoload with namespases
<?php
spl_autoload_register( function ( $className ) {
if ( file_exists( $file = sprintf( '%1$s/%2$s.php', dirname( __FILE__ ), str_replace( "\\", '/', $className ) ) ) ) {
require_once( $file );
}
} );
@sergeliatko
sergeliatko / ater_tables_innodb.sql
Created November 22, 2017 16:56
Generates SQL to alter all tables from a database (DBName) to InnoDB engine.
SELECT CONCAT( "ALTER TABLE ", table_name, " ENGINE=InnoDB;" )
FROM information_schema.tables
WHERE engine = 'MyISAM'
AND table_schema = 'DBName';
@sergeliatko
sergeliatko / functions.php
Last active October 30, 2017 10:18
How to add inline styles to theme stylesheet in WordPress
<?php
/**
* Adds extra CSS rules to the HEAD html tag after to your theme stylesheet.
*/
function my_extra_css() {
wp_add_inline_style(
sanitize_title_with_dashes( wp_get_theme()->get( 'Name' ) ),
'/*** YOUR CSS HERE ***/'
);
}
@sergeliatko
sergeliatko / cc-mask.php
Last active October 3, 2017 10:01
PHP funtion to mask credit card number
<?php
/**
* Masks credit card number leaving up to 4 last available digits.
*
* @param string $n Credit card number (up to 16 characters)
* @param string $m Masking string
* @param int $v Maximum number of visible characters at the end of the credit card number (in range of 0-4)
*
* @return string
*/