Last active
July 24, 2024 18:11
-
-
Save paulburgess/1b6485fe14817f1333e8 to your computer and use it in GitHub Desktop.
Useful functions for Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//require( get_template_directory() . '/_/inc/_custom-post-types.php' ); | |
// Load jQuery | |
if ( !function_exists( 'core_mods' ) ) { | |
function core_mods() { | |
if ( !is_admin() ) { | |
wp_deregister_script( 'jquery' ); | |
wp_register_script( 'jquery', ( "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ), false); | |
wp_enqueue_script( 'jquery' ); | |
} | |
} | |
add_action( 'wp_enqueue_scripts', 'core_mods' ); | |
} | |
// Clean up the <head>, if you so desire. | |
function removeHeadLinks() { | |
remove_action('wp_head', 'rsd_link'); | |
remove_action('wp_head', 'wlwmanifest_link'); | |
} | |
add_action('init', 'removeHeadLinks'); | |
// Navigation - update coming from twentythirteen | |
function post_navigation() { | |
echo '<div class="navigation">'; | |
echo ' <div class="next-posts">'.get_next_posts_link('« Older Entries').'</div>'; | |
echo ' <div class="prev-posts">'.get_previous_posts_link('Newer Entries »').'</div>'; | |
echo '</div>'; | |
} | |
// Posted On | |
/* | |
function posted_on() { | |
printf( __( '<span class="sep">Posted </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a> by <span class="byline author vcard">%5$s</span>', '' ), | |
esc_url( get_permalink() ), | |
esc_attr( get_the_time() ), | |
esc_attr( get_the_date( 'c' ) ), | |
esc_html( get_the_date() ), | |
esc_attr( get_the_author() ) | |
); | |
} | |
*/ | |
function posted_on(){ | |
?> | |
<?php the_time('jS F, Y'); ?> | |
<?php | |
} | |
// Custom excerpt length + format | |
function new_excerpt_more( $more ) { | |
return '…'; | |
/* | |
if(is_front_page()){ | |
return ' <span class="genericon genericon-next"></span>'; | |
} else { | |
return '…'; | |
} | |
*/ | |
} | |
add_filter('excerpt_more', 'new_excerpt_more'); | |
function custom_excerpt_length( $length ) { | |
return 25; // words | |
} | |
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); | |
// easy get slug in the loop - or just use $post->post_name | |
// http://www.tcbarrett.com/2011/09/wordpress-the_slug-get-post-slug-function/ | |
function the_slug($echo=true){ | |
$slug = basename(get_permalink()); | |
do_action('before_slug', $slug); | |
$slug = apply_filters('slug_filter', $slug); | |
if( $echo ) echo $slug; | |
do_action('after_slug', $slug); | |
return $slug; | |
} | |
// disable single post views on certain CPT - useful if using CPTs to build a single page | |
// http://wordpress.stackexchange.com/questions/128636/how-to-disable-the-single-view-for-a-custom-post-type | |
/* | |
add_action( 'template_redirect', 'wpse_128636_redirect_post' ); | |
function wpse_128636_redirect_post() { | |
$queried_post_type = get_query_var('post_type'); | |
if ( is_single() && 'sample_post_type' == $queried_post_type ) { | |
wp_redirect( home_url(), 301 ); | |
exit; | |
} | |
} | |
*/ | |
// Using Metronet reorder plugin? Specify posy types to use it on here: | |
add_filter('metronet_reorder_post_types', 'set_reorder'); | |
function set_reorder($post_types){ | |
$post_types = array('product'); | |
return $post_types; | |
} | |
// Automatically link Twitter names in content | |
function content_twitter_mention($content) { | |
return preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/', "$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>", $content); | |
} | |
add_filter('the_content', 'content_twitter_mention'); | |
add_filter('comment_text', 'content_twitter_mention'); | |
// ================================================================= | |
// ====== Custom admin + menus | |
// ================================================================= | |
/* Theme support for menus */ | |
add_theme_support( 'menus' ); | |
// Remove width and height attributes from images via WYSIWYG/admin | |
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 ); | |
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 ); | |
function remove_width_attribute( $html ) { | |
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html ); | |
return $html; | |
} | |
// Image link default = none | |
update_option('image_default_link_type','none'); | |
// logo link | |
function wpc_url_login(){ | |
return "/"; | |
} | |
add_filter('login_headerurl', 'wpc_url_login'); | |
// logo | |
// add own css file | |
function login_css() { | |
wp_enqueue_style( 'login_css', get_template_directory_uri() . '/_/css/login.css' ); | |
} | |
add_action('login_head', 'login_css'); | |
//change the menu items label Posts to News | |
function change_post_menu_label() { | |
global $menu; | |
global $submenu; | |
$menu[5][0] = 'News'; | |
$submenu['edit.php'][5][0] = 'News'; | |
$submenu['edit.php'][10][0] = 'Add News Item'; | |
$submenu['edit.php'][15][0] = 'News Category'; // Change name for categories | |
//$submenu['edit.php'][16][0] = 'Labels'; // Change name for tags | |
echo ''; | |
} | |
// ================================================ | |
// function change_post_object_label() { | |
// global $wp_post_types; | |
// $labels = &$wp_post_types['post']->labels; | |
// $labels->name = 'News'; | |
// $labels->singular_name = 'News'; | |
// $labels->add_new = 'Add News Item'; | |
// $labels->add_new_item = 'Add News Item'; | |
// $labels->edit_item = 'Edit News Item'; | |
// $labels->new_item = 'News Item'; | |
// $labels->view_item = 'View News Item'; | |
// $labels->search_items = 'Search News'; | |
// $labels->not_found = 'Nothing found'; | |
// $labels->not_found_in_trash = 'Nothing found in Trash'; | |
// } | |
// add_action( 'init', 'change_post_object_label' ); | |
add_action( 'admin_menu', 'change_post_menu_label' ); | |
// ================================================ | |
// ================================================ | |
function remove_footer_admin () { | |
echo '<p>EMMA GREENHILL</p>'; | |
} | |
add_filter('admin_footer_text', 'remove_footer_admin'); | |
// ================================================ | |
function annointed_admin_bar_remove() { | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu('wp-logo'); | |
} | |
add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove', 0); | |
// ================================================ | |
// Remove all those widgets | |
add_action('wp_dashboard_setup', 'wpc_dashboard_widgets'); | |
function wpc_dashboard_widgets() { | |
global $wp_meta_boxes; | |
// Today widget | |
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']); | |
// Last comments | |
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']); | |
// Incoming links | |
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']); | |
// Plugins | |
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']); | |
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_quick_press']); | |
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); | |
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']); | |
} | |
// Add a widget in WordPress Dashboard | |
function add_dashboard_widget_function() { | |
// Entering the text between the quotes | |
echo "<p>Custom footer text</p>"; | |
} | |
function wpc_add_dashboard_widgets() { | |
wp_add_dashboard_widget('wp_dashboard_widget', 'Admin', 'add_dashboard_widget_function'); | |
} | |
add_action('wp_dashboard_setup', 'wpc_add_dashboard_widgets' ); | |
// ================================================ | |
// CHANGE EDITOR PERMISSIONS | |
// Get the the role object | |
$role = get_role( 'editor' ); | |
// allow menus | |
$role->add_cap( 'edit_theme_options' ); | |
// allow Gravity Forms | |
$role->add_cap( 'gravityforms_edit_forms' ); | |
$role->add_cap( 'gravityforms_delete_forms' ); | |
$role->add_cap( 'gravityforms_create_form' ); | |
$role->add_cap( 'gravityforms_view_entries' ); | |
$role->add_cap( 'gravityforms_edit_entries' ); | |
$role->add_cap( 'gravityforms_delete_entries' ); | |
$role->add_cap( 'gravityforms_view_settings' ); | |
$role->add_cap( 'gravityforms_edit_settings' ); | |
$role->add_cap( 'gravityforms_export_entries' ); | |
$role->add_cap( 'gravityforms_view_entry_notes' ); | |
$role->add_cap( 'gravityforms_edit_entry_notes' ); | |
// ================================================ | |
// Move / remove / add admin menu items | |
// ================================================ | |
/** | |
* Hide ACF menu item from the admin menu | |
*/ | |
function remove_acf_menu() | |
{ | |
// provide a list of usernames who can edit custom field definitions here | |
$admins = array( | |
'admin', | |
'username-here' | |
); | |
// get the current user | |
$current_user = wp_get_current_user(); | |
// match and remove if needed | |
if( !in_array( $current_user->user_login, $admins ) ) | |
{ | |
remove_menu_page('edit.php?post_type=acf'); | |
} | |
} | |
add_action( 'admin_menu', 'remove_acf_menu' ); | |
// ================================================ | |
function remove_menus () { | |
// leave the itmes in that you want removed | |
global $menu; | |
$restricted = array( __('Appearance'),__('Links'), __('News'), __('Comments')); | |
end ($menu); | |
while (prev($menu)){ | |
$value = explode(' ',$menu[key($menu)][0]); | |
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);} | |
} | |
} | |
add_action('admin_menu', 'remove_menus'); | |
function new_nav_menu () { | |
global $menu; | |
$menu[99] = array('', 'read', 'separator', '', 'menu-top menu-nav'); | |
add_menu_page(__('Navigation', 'nav-menus'), __('Navigation', 'nav-menus'), 'edit_themes', 'nav-menus.php', '', 'dashicons-menu', 20); | |
} | |
add_action('admin_menu', 'new_nav_menu'); | |
/* Menu positions */ | |
/* | |
5 - below Posts | |
10 - below Posts | |
15 - below Links | |
20 - below Pages | |
25 - below Comments | |
60 - below first separator | |
65 - below Plugins | |
70 - below Users | |
75 - below Tools | |
80 - below Settings | |
100 - below second separator | |
*/ | |
// function custom_menu_order($menu_ord) { | |
// if (!$menu_ord) return true; | |
// | |
// return array( | |
// 'index.php', // Dashboard | |
// 'edit.php?post_type=product', // Posts | |
// 'edit.php?post_type=stockist', // Posts | |
// 'edit.php?post_type=press', // Posts | |
// 'edit.php', // Posts | |
// 'upload.php', // Media | |
// 'edit.php?post_type=page', // Pages | |
// 'edit-comments.php', // Comments | |
// 'link-manager.php', // Links | |
// 'separator1', // First separator | |
// 'nav-menus.php', // Navigation | |
// 'separator2', // Second separator | |
// 'themes.php', // Appearance | |
// 'plugins.php', // Plugins | |
// 'users.php', // Users | |
// 'tools.php', // Tools | |
// 'options-general.php', // Settings | |
// 'separator-last', // Last separator | |
// ); | |
// } | |
// add_filter('custom_menu_order', 'custom_menu_order'); // Activate custom_menu_order | |
// add_filter('menu_order', 'custom_menu_order'); | |
// Set default admin colour | |
/* | |
add_filter( 'get_user_option_admin_color', function( $color_scheme ) { | |
global $_wp_admin_css_colors; | |
if ( 'classic' == $color_scheme || 'fresh' == $color_scheme ) { | |
$color_scheme = 'midnight'; | |
} | |
return $color_scheme; | |
}, 5 ); | |
*/ | |
// Force colour schem for all | |
/* | |
add_filter('get_user_option_admin_color', 'change_admin_color'); | |
function change_admin_color($result) { | |
return 'ectoplasm'; | |
} | |
*/ | |
// ================================================ | |
// remove Wdigets menu in admin | |
// http://codex.wordpress.org/Function_Reference/remove_submenu_page#Examples | |
add_action( 'admin_menu', 'adjust_the_wp_menu', 999 ); | |
function adjust_the_wp_menu() { | |
$page = remove_submenu_page( 'themes.php', 'widgets.php' ); | |
// $page[0] is the menu title | |
// $page[1] is the minimum level or capability required | |
// $page[2] is the URL to the item's file | |
} | |
// Tidy up admin bar | |
function remove_admin_bar_links() { | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu('wp-logo'); // Remove the WordPress logo | |
$wp_admin_bar->remove_menu('about'); // Remove the about WordPress link | |
$wp_admin_bar->remove_menu('wporg'); // Remove the WordPress.org link | |
$wp_admin_bar->remove_menu('documentation'); // Remove the WordPress documentation link | |
$wp_admin_bar->remove_menu('support-forums'); // Remove the support forums link | |
$wp_admin_bar->remove_menu('feedback'); // Remove the feedback link | |
//$wp_admin_bar->remove_menu('site-name'); // Remove the site name menu | |
$wp_admin_bar->remove_menu('themes'); | |
$wp_admin_bar->remove_menu('dashboard'); | |
$wp_admin_bar->remove_menu('menus'); | |
$wp_admin_bar->remove_menu('customize'); | |
$wp_admin_bar->remove_menu('view-site'); // Remove the view site link | |
$wp_admin_bar->remove_menu('updates'); // Remove the updates link | |
$wp_admin_bar->remove_menu('comments'); // Remove the comments link | |
//$wp_admin_bar->remove_menu('new-content'); // Remove the content link | |
//$wp_admin_bar->remove_menu('w3tc'); // If you use w3 total cache remove the performance link | |
$wp_admin_bar->remove_menu('my-account'); // Remove the user details tab | |
} | |
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' ); | |
// ================================================================= | |
// ====== DEVICE DETECTION | |
// ================================================================= | |
// Device functions | |
require_once '_/inc/Mobile_Detect.php'; | |
$detect = new Mobile_Detect(); | |
$GLOBALS['device'] = ($detect->isMobile() ? ($detect->isTablet() ? 'desktop' : 'mobile') : 'desktop'); | |
$GLOBALS['device'] = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'mobile') : 'desktop'); | |
//$GLOBALS['device'] = 'mobile'; | |
if (isset($_GET['fdev'])) $GLOBALS['device'] = $_GET['fdev']; | |
function get_device() { return $GLOBALS['device']; } | |
function the_device() { echo get_device(); } | |
function is_mobile() { return $GLOBALS['device']=='mobile';} | |
function is_tablet() { return $GLOBALS['device']=='tablet';} | |
// | |
function get_device_image($url_or_id,$device_sizes,$class="") { | |
if (is_string($device_sizes)) $device_sizes = deserialize_device_sizes($device_sizes); | |
$device = get_device(); | |
if (!isset($device_sizes[$device])) return; | |
return get_image($url_or_id,$device_sizes[$device][0],$device_sizes[$device][1],$device_sizes[$device][2],$class); | |
} | |
/** | |
* the_device_image($id,array('desktop'=>array(120,130,1),'mobile'=>array(120,130,1),'mobile'=>array(120,130,1))) | |
* the_device_image($id,'desktop:120x130c,mobile:120x130c,tablet:120x130') | |
*/ | |
function the_device_image($url_or_id,$device_sizes,$class="") { | |
if (is_string($device_sizes)) $device_sizes = deserialize_device_sizes($device_sizes); | |
$device = get_device(); | |
if (!isset($device_sizes[$device])) return; | |
the_image($url_or_id,$device_sizes[$device][0],$device_sizes[$device][1],$device_sizes[$device][2],$class); | |
} | |
function deserialize_device_sizes($serialized_sizes) { | |
$serialized_sizes = explode(',',$serialized_sizes); | |
$device_sizes = array(); | |
foreach ($serialized_sizes as $size_string) { | |
preg_match('/(\w+):(\d+)x(\d+)(c)?/i', $size_string,$matches); | |
$name = $matches[1]; | |
$crop = isset($matches[4]); | |
$device_sizes[$name] = array($matches[2],$matches[3],$crop); | |
} | |
return $device_sizes; | |
} | |
// Now add the_device(); to the html class to return desktop | tablet | mobile | |
// <html class="desktop"> | |
/* ============================================= | |
============================================= | |
============================================= */ | |
// ================================================================= | |
// ====== Gravity Forms | |
// ================================================================= | |
/* Custom Gravity forms spinner */ | |
add_filter( 'gform_ajax_spinner_url', 'cwwp_custom_gforms_spinner' ); | |
function cwwp_custom_gforms_spinner( $src ) { | |
return 'http://cdnjs.cloudflare.com/ajax/libs/select2/3.2/spinner.gif'; | |
} | |
/* Remove main/large validation message at top of form */ | |
add_filter("gform_validation_message", "change_message", 10, 2); | |
function change_message($message, $form){ | |
return ""; // <-- add message here | |
} | |
/* Stop page/anchor jump on submission */ | |
add_filter("gform_confirmation_anchor", create_function("","return false;")); | |
// ================================================================= | |
// ====== Yoast SEO | |
// ================================================================= | |
// remove comment | |
function remove_yoast(){ | |
global $wpseo_front; | |
remove_action( 'wpseo_head', array($wpseo_front, 'debug_marker') , 2 ); | |
} | |
add_action('wp_enqueue_scripts','remove_yoast'); | |
// ================================================================= | |
// ====== Custom image sizes | |
// ================================================================= | |
// Add new image sizes | |
function custom_image_sizes( $image_sizes ) { | |
// get the custom image sizes | |
global $_wp_additional_image_sizes; | |
// if there are none, just return the built-in sizes | |
if ( empty( $_wp_additional_image_sizes ) ) | |
return $image_sizes; | |
// add all the custom sizes to the built-in sizes | |
foreach ( $_wp_additional_image_sizes as $id => $data ) { | |
// take the size ID (e.g., 'my-name'), replace hyphens with spaces, | |
// and capitalise the first letter of each word | |
if ( !isset($image_sizes[$id]) ) | |
$image_sizes[$id] = ucfirst( str_replace( '-', ' ', $id ) ); | |
} | |
return $image_sizes; | |
} | |
function custom_image_setup () { | |
add_theme_support( 'post-thumbnails' ); | |
//add_image_size('Mini Square - 70x70', 70, 70, TRUE); | |
add_image_size('Square - 300x300', 300, 300, TRUE); | |
add_image_size('Square - 175x175', 175, 175, TRUE); | |
add_image_size('Icon - 46x46', 46, 46, TRUE); | |
//add_image_size('Featured', 640, 220, TRUE); | |
// To hide these sizes from the admin area, comment out line below | |
add_filter( 'image_size_names_choose', 'custom_image_sizes' ); | |
} | |
add_action( 'after_setup_theme', 'custom_image_setup' ); | |
// Alternative to YOAST | |
// http://digwp.com/2013/08/basic-wp-seo/ | |
/* Basic WP SEO | |
Usage: | |
1. add this code to functions.php | |
2. replace the $default_keywords with your own | |
3. add <?php echo basic_wp_seo(); ?> to header.php | |
4. test well and fine tune as needed | |
Optional: add custom description, keywords, and/or title | |
to any post or page using these custom field keys: | |
mm_seo_desc | |
mm_seo_keywords | |
mm_seo_title | |
To migrate from any SEO plugin, replace its custom field | |
keys with those listed above. More information: | |
@ http://digwp.com/2013/08/basic-wp-seo/ | |
*/ | |
function basic_wp_seo() { | |
global $page, $paged, $post; | |
$default_keywords = 'some, words, here'; // customize | |
$output = ''; | |
// description | |
$seo_desc = get_post_meta($post->ID, 'mm_seo_desc', true); | |
$description = get_bloginfo('description', 'display'); | |
$pagedata = get_post($post->ID); | |
if (is_singular()) { | |
if (!empty($seo_desc)) { | |
$content = $seo_desc; | |
} else if (!empty($pagedata)) { | |
$content = apply_filters('the_excerpt_rss', $pagedata->post_content); | |
$content = substr(trim(strip_tags($content)), 0, 155); | |
$content = preg_replace('#\n#', ' ', $content); | |
$content = preg_replace('#\s{2,}#', ' ', $content); | |
$content = trim($content); | |
} | |
} else { | |
$content = $description; | |
} | |
$output .= '<meta name="description" content="' . esc_attr($content) . '">' . "\n"; | |
// keywords | |
$keys = get_post_meta($post->ID, 'mm_seo_keywords', true); | |
$cats = get_the_category(); | |
$tags = get_the_tags(); | |
if (empty($keys)) { | |
if (!empty($cats)) foreach($cats as $cat) $keys .= $cat->name . ', '; | |
if (!empty($tags)) foreach($tags as $tag) $keys .= $tag->name . ', '; | |
$keys .= $default_keywords; | |
} | |
$output .= "\t\t" . '<meta name="keywords" content="' . esc_attr($keys) . '">' . "\n"; | |
// robots | |
if (is_category() || is_tag()) { | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
if ($paged > 1) { | |
$output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n"; | |
} else { | |
$output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n"; | |
} | |
} else if (is_home() || is_singular()) { | |
$output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n"; | |
} else { | |
$output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n"; | |
} | |
// title | |
$title_custom = get_post_meta($post->ID, 'mm_seo_title', true); | |
$url = ltrim(esc_url($_SERVER['REQUEST_URI']), '/'); | |
$name = get_bloginfo('name', 'display'); | |
$title = trim(wp_title('', false)); | |
$cat = single_cat_title('', false); | |
$tag = single_tag_title('', false); | |
$search = get_search_query(); | |
if (!empty($title_custom)) $title = $title_custom; | |
if ($paged >= 2 || $page >= 2) $page_number = ' | ' . sprintf('Page %s', max($paged, $page)); | |
else $page_number = ''; | |
if (is_home() || is_front_page()) $seo_title = $name . ' | ' . $description; | |
elseif (is_singular()) $seo_title = $title . ' | ' . $name; | |
elseif (is_tag()) $seo_title = 'Tag Archive: ' . $tag . ' | ' . $name; | |
elseif (is_category()) $seo_title = 'Category Archive: ' . $cat . ' | ' . $name; | |
//elseif (is_archive()) $seo_title = 'Archive: ' . $title . ' | ' . $name; | |
elseif (is_archive()) $seo_title = '' . $title . ' | ' . $name; | |
elseif (is_search()) $seo_title = 'Search: ' . $search . ' | ' . $name; | |
elseif (is_404()) $seo_title = '404 - Not Found: ' . $url . ' | ' . $name; | |
else $seo_title = $name . ' | ' . $description; | |
$output .= "\t\t" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "\n"; | |
return $output; | |
} | |
// ACF Options | |
// if( function_exists('acf_add_options_page') ) { | |
// | |
// acf_add_options_page(array( | |
// 'page_title' => 'Site Settings', | |
// 'menu_title' => 'Site Settings', | |
// 'menu_slug' => 'site-settings', | |
// 'capability' => 'edit_posts', | |
// 'redirect' => false | |
// )); | |
// } | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment