Skip to content

Instantly share code, notes, and snippets.

@FriendlyWP
FriendlyWP / single.php
Created February 5, 2016 18:44
Display all ACF field names / field values for any given post.
if (function_exists('get_field')) {
$fields = get_fields();
if( $fields ) {
foreach( $fields as $field_name => $value ) {
$field = get_field_object($field_name, false, array('load_value' => false));
if($value) {
@FriendlyWP
FriendlyWP / functions.php
Created June 25, 2015 16:18
Embed Twitter Cards
// helper function for twitter card stuff
// GET YOUTUBE ID FROM URL
function getYouTubeIdFromURL($url) {
$pattern = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i';
preg_match($pattern, $url, $matches);
return isset($matches[1]) ? $matches[1] : false;
}
@FriendlyWP
FriendlyWP / functions.php
Created June 24, 2015 23:03
Get YouTube ID from various url structures
// GET YOUTUBE ID FROM URL
function getYouTubeIdFromURL($url) {
$pattern = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i';
preg_match($pattern, $url, $matches);
return isset($matches[1]) ? $matches[1] : false;
}
@FriendlyWP
FriendlyWP / functions.php
Last active August 29, 2015 14:19
Dynamically add to Gravity Forms notification email address list (include emails stored in a custom post meta field).
// change # to the id of your form to target a specific form
add_filter( 'gform_notification_#', 'route_notification', 1, 2 );
function route_notification($notification, $form , $entry) {
global $post;
// the $post here is the post where the Form is displayed
$email_to = get_post_meta($post->ID, 'email', true);
if ($email_to){
$notification['to'] .= ',' . $email_to;
}
return $notification;
@FriendlyWP
FriendlyWP / widget-featured-books.php
Last active August 29, 2015 14:16
ACF repeater with nested relationship field, used in widget for ACF Widgets plugin integration (http://acfwidgets.com/). Used on http://julialondon.com/about/ in sidebar to display featured books.
@FriendlyWP
FriendlyWP / flexslider-youtube.js
Created January 11, 2014 19:26
Trying to get Flexslider to work with YouTube videos playing and pausing the slideshow. Here's a live example of the below code: http://tmi.friendlyweb.us/people/sample-page/
jQuery(document).ready(function($) {
$.getScript('//www.youtube.com/iframe_api');
var cnt = 0;
$('.flexslider iframe[src*=youtube]').each(function() {
$(this).attr('id', 'youtubeplayer' + cnt);
cnt++;
});
loadSlider();
@FriendlyWP
FriendlyWP / functions.php
Created November 22, 2013 23:45
Add simple section navigation (child/parent hierarchy) via a shortcode.
// DISPLAY SIMPLE SECTION NAVIGATION VIA SHORTCODE
// adapted from the fantastic Simple Section Navigation plugin which is alas no longer being maintained
// http://wordpress.org/plugins/simple-section-navigation/
add_shortcode('simplesidenav', 'mm_simplesidenav');
function mm_simplesidenav( $atts, $content = null) {
extract( shortcode_atts( array(
'exclude' => '',
'topic' => '',
//'hierarchical' => 'false',
), $atts ) );
@FriendlyWP
FriendlyWP / functions.php
Created November 22, 2013 18:37
Fixes page navigation giving 404 errors on page/2 and beyond when using custom permalinks like category/postname.
// IF USING CUSTOM PERMALINKS LIKE CATEGORY/POSTNAME, PAGE NAVIGATION BREAKS ON PAGE 2. THIS IS THE FIX
// from http://barefootdevelopment.blogspot.com/2007/11/fix-for-wordpress-paging-problem.html
add_filter('request', 'remove_page_from_query_string');
function remove_page_from_query_string($query_string) {
if ($query_string['name'] == 'page' && isset($query_string['page'])) {
unset($query_string['name']);
// 'page' in the query_string looks like '/2', so split it out
list($delim, $page_index) = split('/', $query_string['page']);
$query_string['paged'] = $page_index;
}
@FriendlyWP
FriendlyWP / functions.php
Created November 22, 2013 01:04
Hide the master administrator-level account so it can't accidentally be deleted via the admin panel.
// HIDE MASTER ADMIN ACCOUNT SO IT CAN'T BE ACCIDENTALLY DELETED
add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
global $current_user;
$username = $current_user->user_login;
if ($username !== 'MASTER_ADMIN_USERNAME_HERE') {
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.user_login != 'MASTER_ADMIN_USERNAME_HERE'",$user_search->query_where);
@FriendlyWP
FriendlyWP / functions.php
Created November 22, 2013 00:55
Remove menu items (hide menu items) for everyone except a specific user.
/*
* See http://wordpress.stackexchange.com/a/28784/16 for all options
*/
add_action( 'admin_menu', 'my_remove_menu_pages', 9999 );
function my_remove_menu_pages() {
global $current_user;
$username = $current_user->user_login;
if ($username !== 'SPECIFIC-ADMIN-USERNAME-HERE') {
remove_menu_page('edit-comments.php');