Skip to content

Instantly share code, notes, and snippets.

View sscovil's full-sized avatar

Shaun Scovil sscovil

View GitHub Profile
@sscovil
sscovil / post-keywords-shortcode.php
Created June 14, 2013 03:55
Add shortcode [keywords] to a post, to list all keywords found in the content of a post. Keywords can be added to the post in which the shortcode is added by creating custom field(s) named 'keyword'. Keywords can be defined using the shortcode attribute 'words' (ex: [keywords words=apples,oranges] ). Post ID can be specified using the shortcode …
<?php
/**
* Plugin Name: Post Keywords Shortcode
* Plugin URI: https://gist.github.com/sscovil/5779312
* Description: Searches a post for keywords and displays them if found.
* Version: 0.1
* Author: Shaun Scovil
* Author URI: http://shaunscovil.com
* License: GPL2
*/
@sscovil
sscovil / gist:5798612
Created June 17, 2013 17:30
Code example of how to remove the first row from a CSV file that is imported by the CSV to SortTable plugin for WordPress. Add this code you your `functions.php` file.
<?php
/**
* CSV to SortTable: Remove First Row
*
* @param array $data
* @return array
*/
function csv_to_sorttable_remove_first_row( $data ) {
array_shift( $data );
return $data;
@sscovil
sscovil / wordpress-archive-post-count.php
Last active December 18, 2015 15:59
Add this function to your WordPress theme's `functions.php` file and call it from `archive.php` using the template tag `my_archive_post_count()`. If $echo parameter is set to false, it will simply return the HTML as a string. Post count is appended with previous- and next page links if there is more than one page.
<?php
function my_archive_post_count( $echo = true ) {
global $wp_query;
$showing = '';
if ( $wp_query->found_posts > 1 ) {
// Get previous & next page links.
$args = array(
'sep' => ' &#8212; ',
@sscovil
sscovil / wordpress-redirect.php
Last active December 19, 2015 05:39
Written in response to a WP support forum question.
<?php
/**
* WordPress Conditional Redirect Function
*
* In this example, the conditional tag checks if the current page is a page with a slug of test and,
* if true, replaces example.com with test.example.com and redirects.
*/
function my_custom_redirect() {
if ( is_page( 'test' ) ) {
@sscovil
sscovil / csv-to-sorttable-add-header-row.php
Last active December 19, 2015 07:39
Example of how to add a header row to the table data array, using my CSV-to-SortTable plugin for WordPress. Add this to your `functions.php` file.
<?php
/**
* CSV to SortTable: Add Header Row
*
* @param array $data
* @return array
*/
function csv_to_sorttable_add_header_row( $data ) {
$header_row = array( 'Column 1', 'Column 2', 'Column 3' );
array_unshift( $data, $header_row );
<?php
/**
* Plugin Name: CSV to SortTable: Add Header Row
* Plugin URI: https://gist.github.com/sscovil/5919960
* Description: Adds a custom header row to tables created by CSV to SortTable plugin.
* Version: 0.1
* Author: sscovil
* Author URI: http://shaunscovil.com
* License: GPL2
*/
<?php
// Use WP_User_Query to grab an array of user IDs.
$user_query = new WP_User_Query( array(
'fields' => 'ID'
) );
$user_ids = $user_query->get_results();
// Loop through each user ID.
foreach( $user_ids as $user_id ) {
// Use get_userdata() to get a user object with info stored in the 'user' database table.
@sscovil
sscovil / plugin-class-autoloader.php
Last active December 20, 2015 08:09
This class autoloader function eliminates the need to include_once every class file in your WordPress plugin. It adheres to WordPress file naming conventions, so instantiating a class named MyClass would attempt to include_once a file called inc/class-myclass.php.
<?php
/**
* Plugin Name: My Plugin
* Description: A sample plugin with class autoloader, for object-oriented programming.
* Version: 0.1
* Plugin URI: http://shaunscovil.com/
* Author: Shaun Scovil
* Author URI: http://shaunscovil.com/
* License: GPL2
*/
<?php
/**
* Percent
*
* @param $a integer Number that represents 100%.
* @param $b integer Number to compare to $a.
*
* @return string Number B as a percentage of Number A.
*/
public static String mapToString(Map<String, String> map, String kvSeparator, String setSeparator) {
StringBuilder stringBuilder = new StringBuilder();
for (String key : map.keySet()) {
if (stringBuilder.length() > 0) stringBuilder.append(setSeparator);
String value = map.get(key);
stringBuilder.append((key != null ? key : "null"));
stringBuilder.append(kvSeparator);
stringBuilder.append(value != null ? value : "null");
}