Skip to content

Instantly share code, notes, and snippets.

View johnalarcon's full-sized avatar
😎
Coding potently.

John Alarcon johnalarcon

😎
Coding potently.
View GitHub Profile
@johnalarcon
johnalarcon / alar-minimum-php.php
Last active May 6, 2018 20:54
A simple object-oriented approach to ensure that a minimum version of PHP is installed before allowing installation of your WordPress plugin. This code should live in your plugin's root file; your actual (PHP7) plugin code is included at line 73.
<?php
/**
* Plugin Name: My Plugin Name
* Description: Create a robust admin settings page for your WordPress plugin in minutes! More than 20 HTML5 input types are supported, including clickable labels, input hints, and even beautiful tooltips. Simply define your needed sections and inputs – everything else is automatic. Your settings are displayed with tabbed navigation that looks exactly like WordPress built it – no wonky styles, no silly cartoon characters, no in-your-face nags. The CSS totals less than 50 lines and the Javascript is less 25. This is built entirely inline with WordPress core standards and has no outside dependencies. The interface loads super-fast – you can even navigate across sections/tabs, changing settings along the way, and then click “Save” just once to save all the settings at once. Because I love you. But, you know, just as friends.
* Version: 1.0.0
* Author: John Alarcon
* Author URI: https://github.com/johnalarcon/my-plugin-name
* Text Domain: my-plugin-name
* Domain Pat
@johnalarcon
johnalarcon / exclude-filetypes.php
Created June 6, 2018 22:22
A simple snip for removing MS document support from the WordPress Media Uploader. Created after a brief discussion during our 6/5/18 meetup.
/**
* Remove support for files of type doc, docx, docm, dotx, and dotm from the
* WordPress Media Uploader. Place this code into your theme's functions.php file.
*/
add_filter('upload_mimes', 'jalarcon_remove_filetypes', 1, 1);
function jalarcon_remove_filetypes($filetypes) {
unset($filetypes['doc']);
unset($filetypes['docx']);
unset($filetypes['docm']);
@johnalarcon
johnalarcon / code-comment-counter.php
Last active September 1, 2018 03:07
This bit of code takes a relative path, scans that directory, then determines how many lines of code and how many lines of comments are within.
<?php
$dir = 'C:/xampp/htdocs/wordpress/wp-content/plugins/my-plugin-name/';
echo get_code_size($dir);
function get_code_size($dir) {
// The directory to scan; include trailing slash.
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
@johnalarcon
johnalarcon / functions.php
Last active November 1, 2018 23:23
Change core WP post item labels
add_action('admin_menu', 'change_admin_menu_for_editors');
function change_admin_menu_for_editors() {
global $menu;
global $submenu;
// Update labels for Posts menu items.
$menu[5][0] = __('Blog Articles', 'my-text-domain');
$submenu['edit.php'][5][0] = __('All Articles', 'my-text-domain');
$submenu['edit.php'][10][0] = __('Add New Article', 'my-text-domain');
$submenu['edit.php'][15][0] = __('Blog Categories', 'my-text-domain');
}
@johnalarcon
johnalarcon / constants.php
Last active November 24, 2018 18:14
Path/URL constants for use in WordPress/ClassicPress plugins to speed IDE autocompletion.
<?php
// Declare the namespace.
namespace MYPLUGINNAME;
// Prevent direct access.
if (!defined('ABSPATH')) {
die();
}
@johnalarcon
johnalarcon / autoloader.php
Created December 18, 2018 21:02
Autoloader for ClassicPress/WordPress Plugin Classes
<?php
/**
* Autoload classes
*
* Autoloaded classes? Yes. Support for namespaces, too? Of course! It helps
* to have a good understanding of how this autoloader works, so you can use
* it to your benefit, and not as a reason to pull out your hair. ;) Classes
* do not have to be namespaced, but it is recommended they are, for maximum
* plays-well-with-others...ness. Let's start with the super-basics...
@johnalarcon
johnalarcon / functions.php
Last active December 28, 2018 02:48
Remove the WP 5.0 update nag
<?php
// Add the following to your functions.php file to hide the WP 5.0 update nag.
add_action('after_setup_theme', 'codepotent_remove_wp50_update_nag');
function codepotent_remove_wp50_update_nag() {
add_filter('pre_option_update_core','__return_null');
add_filter('pre_site_transient_update_core','__return_null');
}
@johnalarcon
johnalarcon / functions.php
Last active January 7, 2019 01:33
Prevent username enumeration via WordPress / ClassicPress REST API.
/**
* Prevent username enumeration via REST API
*
* This function allows normal (anonymous) access to the REST API, but makes
* sure that site usernames are not exposed through it. This code can be added
* to your theme's functions.php file.
*
* Note: there has been a report that this code may interfere with JetPack's operation.
* See https://twitter.com/PrysmcatBooks/status/1082022370817261568
*/
@johnalarcon
johnalarcon / functions.php
Last active January 30, 2019 04:03
Filter examples for the Username Protection plugin for ClassicPress. These code snips can be added to a functionality plugin or the functions.php file.
<?php
// Change text used for display names in feeds. Default set by plugin is your site's name.
add_filter('codepotent_username_protection_feeds', 'codepotent_feeds');
function codepotent_feeds($display_name) {
return $display_name;
}
// Change the REST API error message. Default set by plugin is "authentication required".
add_filter('codepotent_username_protection_rest_error', 'codepotent_rest_error');
@johnalarcon
johnalarcon / update-classicpress-urls.sql
Created January 30, 2019 05:48
SQL query to update URLs after moving a ClassicPress site.
UPDATE cp_options SET option_value = replace(option_value, 'OLD_URL', 'NEW_URL') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE cp_posts SET guid = replace(guid, 'OLD_URL', 'NEW_URL');
UPDATE cp_posts SET post_content = replace(post_content, 'OLD_URL', 'NEW_URL');
UPDATE cp_postmeta SET meta_value = replace(meta_value,'OLD_URL', 'NEW_URL');
UPDATE cp_usermeta SET meta_value = replace(meta_value, 'OLD_URL', 'NEW_URL');