Skip to content

Instantly share code, notes, and snippets.

View dbernar1's full-sized avatar

Dan Bernardic dbernar1

View GitHub Profile
@dbernar1
dbernar1 / gist:5662786
Last active December 17, 2015 19:49
Basic wp_enqueue_script
<?php
wp_enqueue_script(
$handle ='my-js',
$src = get_stylesheet_directory_uri() . '/js/my.js',
$deps = array( 'jquery' ),
$ver = '0.01',
$in_footer = true
);
@dbernar1
dbernar1 / gist:5833460
Last active December 18, 2015 19:29
A basic WordPress controller
<?php
define( 'DB_VH_REWRITE_TAG', 'feed-relay' );
add_action( 'init', 'db_vh_create_feeds_relay_urls' );
function db_vh_create_feeds_relay_urls() {
add_rewrite_rule(
$rule = 'feeds/(.*)/?',
$rewrite = 'index.php?' . DB_VH_REWRITE_TAG . '=$matches[1]',
@dbernar1
dbernar1 / gist:5858878
Created June 25, 2013 14:27
Varnish - Don't cache server error responses
sub vcl_fetch {
if ( obj.status >= 500 ) {
set obj.ttl = 0s;
set obj.cacheable = false;
}
}
@dbernar1
dbernar1 / gist:5921067
Created July 3, 2013 18:01
A sane way of creating excel files inside PHP - requires the PHPExcel library
<?php
function wf_fbc_create_and_send_excel_spreadsheet_to_browser( $headings, $data ) {
ini_set('include_path', ini_get('include_path'). PATH_SEPARATOR . dirname( __FILE__) . '/lib/phpexcel/Classes/');
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel5.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getDefaultStyle()->getFont()->setName('Times New Roman');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getStyle('1')->applyFromArray(array( 'font' => array('bold' => true,),));
add_action( 'wp_enqueue_scripts', 'my_pjn_scripts_and_styles');
function my_pjn_scripts_and_styles() {
if ( is_page( 'some-page-slug' ) ) {
wp_enqueue_script(
$handle = 'googlemap',
$src = "http://maps.googleapis.com/maps/api/js?sensor=true",
$deps = array(),
$ver = 'V3',
$in_footer = true
@dbernar1
dbernar1 / gist:6368069
Created August 28, 2013 16:31
Resetting capabilities for admin role in WordPress to the defaults, programmatically. I ran this through functions.php of the theme. List of caps copied from http://codex.wordpress.org/Roles_and_Capabilities#Administrator
<?php
$administrator = get_role( 'administrator' );
foreach (
array( 'activate_plugins', 'delete_others_pages', 'delete_others_posts', 'delete_pages', 'delete_plugins', 'delete_posts', 'delete_private_pages', 'delete_private_posts', 'delete_published_pages', 'delete_published_posts', 'edit_dashboard', 'edit_files', 'edit_others_pages', 'edit_others_posts', 'edit_pages', 'edit_posts', 'edit_private_pages', 'edit_private_posts', 'edit_published_pages', 'edit_published_posts', 'edit_theme_options', 'export', 'import', 'list_users', 'manage_categories', 'manage_links', 'manage_options', 'moderate_comments', 'promote_users', 'publish_pages', 'publish_posts', 'read_private_pages', 'read_private_posts', 'read', 'remove_users', 'switch_themes', 'upload_files', 'create_product', )
as $cap
) {
$administrator->add_cap( $cap );
}
@dbernar1
dbernar1 / homepage.js
Last active February 19, 2018 08:20
That's righchyall
when_the_user_indicates_which_classes_they_wanna_see_homework_for(
send_user_choice_for_which_classes_they_wanna_see_homework
);
when_the_user_indicates_which_calendars_they_want_to_see(
send_user_choice_of_which_calendars_they_want_to_see
);
//-----------------Calendar Details-------------------//
@dbernar1
dbernar1 / gist:6425549
Last active December 22, 2015 05:39
Naming function parameters. Also, code is text.
<?php
wp_enqueue_script( $module_slug . '-js', dirname(__FILE__) . '/js/' . $module_slug . '.js', array( 'jquery' ), self::version, true );
wp_enqueue_script(
$script_name = $module_slug . '-js',
$location = dirname(__FILE__) . '/js/' . $module_slug . '.js',
$load_after = array( 'jquery' ),
$version_number = self::version,
$load_in_footer = true
);
<?php
add_action( 'wp_enqueue_scripts', 'scripts_and_styles__of_me_theme');
function scripts_and_styles__of_me_theme() {
if ( is_page( 'forecasts' ) || is_front_page() || is_page( 'radar' ) || is_page( 'historical-data' ) ) {
wp_enqueue_script( 'jquery-ui-autocomplete' );
}
}
@dbernar1
dbernar1 / 1-template.php
Last active December 23, 2015 08:59
get_template_part() vs. require - variable scope
<?php
$hello = 'world';
get_template_part( 'hello' );
require 'hello.php';
function get_template_part( $filename ) {
require $filename . '.php';
}