Skip to content

Instantly share code, notes, and snippets.

@Phoenix2k
Phoenix2k / .asciilogo
Last active April 14, 2021 15:41
Rainbow shell logo
@Phoenix2k
Phoenix2k / search_replace.php
Created June 20, 2018 19:20
PHP: Search and replace function
<?php
/**
* Search and replaces text.
*
* @param String $text Text to be replaced
* @param Array $args Key value pairs representing search and replace respectively
* @return String Text with search and replace applied.
*/
function search_and_replace( $text = '', $args = [ ] ) {
if ( empty( $args ) || ! array( $args ) ) return $text;
@Phoenix2k
Phoenix2k / sort_alphabetically.js
Last active October 14, 2018 07:02
JavaScript - Sort an array of objects according to a specific key
const data = [
{ "name": "A" },
{ "name": "C" },
{ "name": "B" },
{ "name": "D" }
];
/**
* sortObjectArrayByKey
*
@Phoenix2k
Phoenix2k / wordpress-custom-tax-columns.php
Created December 12, 2017 14:13
WordPress: Custom taxonomy columns with sorting support
<?php
/**
* This example uses 'main-category' as a custom taxonomy and values
* from Carbon Fields for sorting https://carbonfields.net
*/
// Add custom column title
add_filter( 'manage_edit-main-category_columns', function( $columns ) {
$column_position = 2;
$before = array_slice( $columns, 0, $column_position, true );
@Phoenix2k
Phoenix2k / wp_media_to_csv.php
Created June 22, 2017 16:04
WordPress: Media files to CSV
<?php
global $post;
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post_parent' => null );
$attachments = get_posts( $args );
$csv = "ID, Name, Title, Alt, Description, Caption, URL,\n";
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post );
$attachment = wp_prepare_attachment_for_js( get_the_ID() );
$csv .= '"' . str_replace( '"', '\"', $attachment[ 'id' ] ) . '"' . ",";
@Phoenix2k
Phoenix2k / ISO_639-1_to_ISO_639-2.json
Created May 29, 2017 13:07
JSON: ISO 639-1 to ISO 639-2 conversion table
{
"ab": "abk",
"ae": "ave",
"af": "afr",
"ak": "aka",
"am": "amh",
"an": "arg",
"ar": "ara",
"as": "asm",
"av": "ava",
@Phoenix2k
Phoenix2k / inline_stylesheet.php
Last active June 20, 2018 19:26
WordPress: Inline stylesheet in <head>
<?php
/**
* Prints out entire stylesheet in the <head> section of the site.
* Supports replacing relative URLs
*/
add_action( 'wp_head', function() {
$stylesheet = get_stylesheet_directory() . '/style.min.css';
if ( ! file_exists( $stylesheet ) ) return echo '<!--404-CSS-->';
$stylesheet = file_get_contents( $stylesheet );
@Phoenix2k
Phoenix2k / convert_links.php
Last active February 3, 2017 00:25
Class: Converts plain text links into <a> HTML elements (supports `rel` and `target`)
@Phoenix2k
Phoenix2k / login_page_check.php
Created October 7, 2016 23:14
WordPress: How to check if you're on the login/register page
<?php
// Both login and register page
if ( isset( $GLOBALS[ 'pagenow' ] ) && ( false === in_array( $GLOBALS[ 'pagenow' ], array( 'wp-login.php', 'wp-register.php' ) ) ) ) {
// Do something
}
// Login page only
if ( isset( $GLOBALS[ 'pagenow' ] ) && ( false === in_array( $GLOBALS[ 'pagenow' ], array( 'wp-login.php' ) ) ) ) {
// Do something
}
@Phoenix2k
Phoenix2k / override_jetpack_sharing_image.php
Last active October 7, 2016 23:15
WordPress: Jetpack snippets
<?php
/**
* Override Jetpack Open Graph sharing image
*
* @link https://jetpack.com/tag/open-graph/
*/
add_filter( 'jetpack_images_get_images', 'override_jetpack_images_get_images', 20, 3 );
function override_jetpack_images_get_images( $media, $post_id, $args ) {
$root_directory = get_bloginfo( 'stylesheet_directory' );