Skip to content

Instantly share code, notes, and snippets.

@vincentorback
Last active May 20, 2016 17:29
Show Gist options
  • Save vincentorback/d05917bdefd7fcaac86a to your computer and use it in GitHub Desktop.
Save vincentorback/d05917bdefd7fcaac86a to your computer and use it in GitHub Desktop.
Some fun WordPress snippets
<?php
/**
* Check if on posts-page
*/
function is_page_for_posts () {
return ( is_home() || (is_archive() && ! is_post_type_archive() ) );
}
/**
* Check if is search page
* @return boolean [description]
*/
function is_search_page () {
if (is_search()) {
return true;
}
$search_template_page = get_pages_with_template('search.php');
if ($search_template_page && is_page($search_template_page[0]->ID)) {
return true;
}
}
/**
* Get post page url
*/
function get_page_for_posts_url () {
return get_permalink(get_option( 'page_for_posts' ));
}
/**
* Get pages using a specific template
*/
function get_pages_with_template($template) {
return get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => $template
));
}
/**
* Get page depth
*/
function get_page_depth ($page) {
if (!$page) {
return;
}
$parent_id = $page->post_parent;
$depth = 0;
while ($parent_id > 0) {
$parent = get_page($parent_id);
$parent_id = $parent->post_parent;
$depth++;
}
return $depth;
}
/**
* Totaly disable comments
*/
add_action( 'init', function () {
remove_post_type_support( 'post', 'comments' );
remove_post_type_support( 'page', 'comments' );
});
add_action( 'admin_menu', function () {
remove_menu_page( 'edit-comments.php' );
});
add_action( 'wp_before_admin_bar_render', function () {
global $wp_admin_bar;
$wp_admin_bar->remove_menu( 'comments' );
});
/**
* Custom avatars.
* @link https://codex.wordpress.org/How_to_Use_Gravatars_in_WordPress
*
* @param $user_contact Existing avatars
*/
add_filter( 'avatar_defaults', function ( $avatar_defaults ) {
$my_avatar = get_bloginfo( 'template_directory' ) . '/images/my-avatar.gif';
$avatar_defaults[$my_avatar] = "My Avatar";
return $avatar_defaults;
}, 999);
/**
* Custom left admin footer text
* @link https://developer.wordpress.org/reference/hooks/admin_footer_text/
*/
add_filter( 'admin_footer_text', function () {
return '<span id="footer-thankyou">Website by <a href="//vincentorback.se" target="_blank">Vincent Orback</a></span>';
}, 999);
/**
* Custom right admin footer text (where the WordPress version nr is)
*/
add_filter( 'update_footer', function () {
return '¯\_(ツ)_/¯';
}, 999);
/**
* Stay logged in for longer
*/
add_filter( 'auth_cookie_expiration', function () {
return 31556926; // 1 year
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment