Skip to content

Instantly share code, notes, and snippets.

Avatar

Caspar Hübinger glueckpress

View GitHub Profile
@glueckpress
glueckpress / single_post_widget.php
Last active Nov 20, 2015
Makes a custom WordPress widget to display any published post.
View single_post_widget.php
<?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 Jan 17, 2012
Disable self trackbacks from your own WordPress blog.
View disable_self_ping.php
<?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 Apr 12, 2012
Block ANY access to wp-login.php and redirect to a given URL (here: home_url() ).
View block-wp-login.php
<?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 Dec 14, 2015
[WordPress] Conditional tag to check whether current page is wp-login.php or wp-register.php.
View wp-is-login.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 Jun 1, 2020
[WordPress] Check if the current widget is on first position in its sidebar
View is_first_widget.php
<?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 Apr 28, 2019
[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.
View wp-bootstrap-subheading.php
<?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 Dec 14, 2015
[WordPress] Add current ancestor class to nav menu when viewing a singular attachment page, e.g. via the image.php template file.
View current-attachment-nav-class.php
<?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 Dec 14, 2015
[WordPress] Call a registered sidebar in a page template dynamically based on the page slug.
View page-dynamically-named-sidebar.php
<?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 Jul 31, 2012
Create a clickable index list from all heading elements on a page.
View jquery.indexlist.js
/*
* 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 Dec 14, 2015
[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.
View wp-shortcode-email-link.php
You can’t perform that action at this time.