Last active
May 24, 2018 20:50
-
-
Save peterwegren/40bd4e88e4fd8839bedb5e3d6621505e to your computer and use it in GitHub Desktop.
WP Helper Functions
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 | |
/* | |
* Remove Dashboard Items | |
*/ | |
if (is_admin()) { | |
if (!(function_exists('pl_remove_dash_items'))) { | |
function pl_remove_dash_items() | |
{ | |
// Check that the built-in WordPress function remove_menu_page() exists in the current installation | |
if (function_exists('remove_menu_page')) { | |
// remove comments | |
remove_menu_page('edit-comments.php'); | |
// remove widgets | |
remove_submenu_page('themes.php', 'widgets.php'); | |
// remove theme customizer, header, and background | |
global $submenu; | |
foreach($submenu['themes.php'] as $menu_index => $theme_menu){ | |
if($theme_menu[0] == 'Header' || $theme_menu[0] == 'Background' || $theme_menu[0] == 'Customize') | |
unset($submenu['themes.php'][$menu_index]); | |
} | |
} | |
} | |
} | |
add_action('admin_menu', 'pl_remove_dash_items'); | |
} | |
/* | |
* Get page ID by slug | |
*/ | |
function get_post_id_by_name($post_name, $post_type = 'post'){ | |
$post_ids = get_posts( | |
array( | |
'name' => $post_name, | |
'post_type' => $post_type, | |
'numberposts' => -1, | |
'fields' => 'ids' | |
) | |
); | |
return array_shift($post_ids); | |
} | |
/** | |
* Force Yoast meta box to bottom of page. | |
*/ | |
function yoasttobottom() { | |
return 'low'; | |
} | |
add_filter('wpseo_metabox_prio', 'yoasttobottom'); | |
/* | |
* Remove featured image meta box from pages | |
*/ | |
function remove_featured_image() { | |
remove_meta_box( 'postimagediv', 'page', 'side' ); | |
} | |
add_action('do_meta_boxes', 'remove_featured_image'); | |
/* | |
* Rewrite search URLs | |
*/ | |
function search_url_rewrite() { | |
if ( is_search() && !empty($_GET['s']) ) { | |
wp_redirect( home_url("/search/") .urlencode(get_query_var('s')) .'/' ); | |
exit(); | |
} | |
} | |
add_action('template_redirect', 'search_url_rewrite'); | |
/* | |
* Rewrite search URLs - retain post_type query var | |
*/ | |
// function search_url_rewrite() { | |
// if ( is_search() && array_key_exists('s', $_GET) ) { | |
// if ( array_key_exists('post_type', $_GET) ) { // retain post_type query | |
// wp_redirect( home_url("/search/") .urlencode(get_query_var('s')) .'/?post_type=' .urlencode(get_query_var('post_type')), 301 ); | |
// } else { | |
// wp_redirect( home_url("/search/") .urlencode(get_query_var('s')) .'/', 301 ); | |
// } | |
// exit(); | |
// } | |
// } | |
// add_action('template_redirect', 'search_url_rewrite'); | |
/* | |
* Prevent <p> from wrapping <img> in content | |
*/ | |
function filter_ptags_on_images( $content ){ | |
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); | |
} | |
add_filter('the_content', 'filter_ptags_on_images'); | |
// load GF scripts in the footer | |
// http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_init_scripts_footer/ | |
// https://bjornjohansen.no/load-gravity-forms-js-in-footer | |
add_filter( 'gform_init_scripts_footer', '__return_true' ); | |
function wrap_gform_cdata_open( $content = '' ) { | |
if ( ( defined('DOING_AJAX') && DOING_AJAX ) || isset( $_POST['gform_ajax'] ) ) { | |
return $content; | |
} | |
$content = 'document.addEventListener( "DOMContentLoaded", function() { '; | |
return $content; | |
} | |
add_filter( 'gform_cdata_open', 'wrap_gform_cdata_open', 1 ); | |
function wrap_gform_cdata_close( $content = '' ) { | |
if ( ( defined('DOING_AJAX') && DOING_AJAX ) || isset( $_POST['gform_ajax'] ) ) { | |
return $content; | |
} | |
$content = ' }, false );'; | |
return $content; | |
} | |
add_filter( 'gform_cdata_close', 'wrap_gform_cdata_close', 99 ); | |
/** | |
* defer_parsing_of_js | |
* | |
* defers parsing of js files | |
* | |
* @param string $url | |
* @return string return .js URL w/ defer directive | |
*/ | |
function defer_parsing_of_js ( $url ) { | |
if ( FALSE === strpos( $url, '.js' ) ) return $url; | |
if ( strpos( $url, 'jquery.js' ) ) return $url; | |
return "$url' defer "; | |
} | |
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment