Skip to content

Instantly share code, notes, and snippets.

View glueckpress's full-sized avatar

Caspar Hübinger glueckpress

View GitHub Profile
@glueckpress
glueckpress / single_post_widget.php
Last active November 20, 2015 22:51
Makes a custom WordPress widget to display any published post.
<?php
// Make sure we have a theme domain for localization
if( ! defined( 'THEME_DOMAIN' ) )
define( 'THEME_DOMAIN', get_stylesheet() ); // Any translation has to go in your stylesheet directory
/**
* Makes a custom widget to display any published post.
*
* @package WordPress
@glueckpress
glueckpress / disable_self_ping.php
Created January 17, 2012 22:35
Disable self trackbacks from your own WordPress blog.
<?php
// Retrieved from: http://wp-snippets.com/disable-self-trackbacks/
add_action( 'pre_ping', 'disable_self_ping' );
function disable_self_ping( &$links ) {
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, get_option( 'home' ) ) )
unset($links[$l]);
}
@glueckpress
glueckpress / block-wp-login.php
Created April 12, 2012 07:28
Block ANY access to wp-login.php and redirect to a given URL (here: home_url() ).
<?php
add_action( 'init', 'custom_init_function' );
function custom_init_function() {
global $pagenow;
if( ! is_user_logged_in() && 'wp-login.php' == $pagenow ) {
wp_redirect( home_url() ); // redirect URL
exit();
@glueckpress
glueckpress / wp-is-login.php
Last active December 14, 2015 21:42
[WordPress] Conditional tag to check whether current page is wp-login.php or wp-register.php.
<?php
/**
* Conditional tag to check whether current page is wp-login.php or wp-register.php.
*/
if( ! function_exists( 'wp_is_login' ) ) {
function wp_is_login() {
return in_array( $GLOBALS[ 'pagenow' ], array( 'wp-login.php' , 'wp-register.php' ) );
}
}
@glueckpress
glueckpress / is_first_widget.php
Last active June 1, 2020 09:06
[WordPress] Check if the current widget is on first position in its sidebar
<?php
/**
* Goes in function widget(…) inside class Your_Widget
*/
// 1. Get all active widgets from all sidebars.
$all_sidebars_widgets = wp_get_sidebars_widgets();
// 2. Get only the active widgets in our currently inhabitated sidebar.
@glueckpress
glueckpress / wp-bootstrap-subheading.php
Last active April 28, 2019 11:22
[WordPress] Add Bootstrap sub-heading to post title. Simply write something like "My post title | Smaller sub-line added here." into your post title or widget title field.
<?php
/**
* Append a sub-heading to WordPress post titles
*
* Input: My title | my sub-title
* Output: My title <small>my sub-title</small>
*
* The filter the_title() is used for posts/pages AND nav menus in WordPress.
* In order to filter only post/page titles, not nav menu items, we need to
@glueckpress
glueckpress / current-attachment-nav-class.php
Last active December 14, 2015 21:50
[WordPress] Add current ancestor class to nav menu when viewing a singular attachment page, e.g. via the image.php template file.
<?php
/**
* Add current item classes for attachment views
*/
function _s_current_attachment_nav_class( $classes, $menu_item ) {
global $wp_query, $post;
if( true == $wp_query->is_singular && 'attachment' == $post->post_type && in_array( $menu_item->object_id, $post->ancestors ) ) {
$classes[] = 'current-menu-ancestor';
@glueckpress
glueckpress / page-dynamically-named-sidebar.php
Last active December 14, 2015 21:50
[WordPress] Call a registered sidebar in a page template dynamically based on the page slug.
<?php
// This whole thing goes into a page template,
// so we can get the page object via the_post() here
global $wp_query;
the_post();
// Store page slug for later
$gp_current_page_slug = $post->post_name;
// Page content
@glueckpress
glueckpress / jquery.indexlist.js
Created July 31, 2012 15:17
Create a clickable index list from all heading elements on a page.
/*
* Assuming your post/page content happens in .entry-content and you structured it semantically using h3 elements,
* these functions will create a clickable index list at the top of your content.
* Modify hard-coded selectors as you see fit; I used .indexlist for the ul element,
* .indexitem for each li item and h3 elements within .entry-content to create the list items from.
*/
$.fn.inlineScrolling = function(options) {
return this.each(function(event) {
@glueckpress
glueckpress / wp-shortcode-email-link.php
Last active December 14, 2015 21:51
[WordPress] Concise shortcode function for an anti-spambotted email link in WordPress. Parameters for class, style (check for support in your wysiwyg editor!) and title attributes. Parameters before, after, before_text and after_text for most concise control of the final HTML output.