Skip to content

Instantly share code, notes, and snippets.

@aahan
aahan / file.php
Last active December 28, 2015 03:09
<?php
/*
* Get category from query_var of current page
*
* http://codex.wordpress.org/Function_Reference/get_query_var
* http://codex.wordpress.org/Function_Reference/get_term
* http://codex.wordpress.org/Function_Reference/get_term_by
* http://codex.wordpress.org/Function_Reference/get_category_by_path
* http://codex.wordpress.org/Function_Reference/get_category_by_slug
* http://codex.wordpress.org/Function_Reference/get_the_category

NOTES

1. When initial code didn't work, I installed Debug Bar plugin and then not-yet-released Debug Bar Rewrite Rules plugin by Oleg Butuzov (who mailed it to me).

With the two plugins installed, click Debug link in admin bar, and in the popover select the Rewrite Rules tab. Now you'll see a list of all the rewrite rules in place on your site.

By comparing the regex, rewrite rules used for similar stuff (e.g. category archives, category archive pagination, category feeds, etc.) I was able to frame my own rules properly.

2. Resources

<?php
function h5bs_enqueue_scripts() {
wp_enqueue_script( 'typekit', '//use.typekit.net/xxxxxxx.js' );
}
add_action( 'wp_enqueue_scripts', 'h5bs_enqueue_scripts' );
function h5bs_typekit_inline() {
@aahan
aahan / convert-date-format.php
Last active December 25, 2015 22:49
Convert date to a different format.
<?php
/*
* Always input date in ISO 8601 format (YYYY-MM-DDThh:mm:ss.sTZD) to
* allow for conversion to a different format before display.
*
* e.g. 1997-07-16T19:20:30.45+01:00, 1997-07-16
*
* http://www.w3.org/TR/NOTE-datetime
* http://php.net/manual/en/function.date.php
<?php
/*
* How to use `get_post_type_object` function
* http://wordpress.stackexchange.com/q/6731
*/
if( !in_array( $post->post_type, array( 'post', 'page' ) ) ) {
// if( 'post' != get_post_type() || 'page' != get_post_type() ) {
<?php
/* Register custom post types on the 'init' hook. */
add_action( 'init', 'my_register_post_types' );
/**
* Registers post types needed by the plugin.
*
* @since 0.1.0
* @access public
<!-- Published Date -->
<span class="posted-on"><time class="entry-date published" datetime="<?php the_time('c'); ?>" pubdate title="Published on <?php the_time('d M, Y \a\t H:i:s T'); ?>"><?php the_time('d M Y'); ?></time></span>
<!-- Last Modified Date -->
<span class="modified-on"><time class="updated" datetime="<?php the_modified_time('c'); ?>" title="Last updated on <?php the_modified_time('d M, Y \a\t H:i:s T'); ?>"><?php the_modified_time('j F, Y'); ?></time></span>
<!-- Post Author (Anchor) -->
<?php
/**
* Plugin Name: Snippets Shortcode
* Plugin URI: http://wordpress.stackexchange.com/q/116044/12615
* Description: Add code snippets as a Custom Post Type. Display in regular posts and pages using a Shortcode. Rendered with Google Prettify in frontend.
* Version: 1.0
* Author: Rodolfo Buaiz
* Author URI: http://brasofilo.com
* License: GPLv2 or later
*
@aahan
aahan / wp-code-shortcode.php
Last active March 28, 2024 19:27
Enclose code blocks within [code] shortcode, and let PHP and WordPress take care of the rest.
<?php
/*
* Functionality to set up custom shortcode correctly.
*
* This function is attached to the 'the_content' filter hook.
*/
add_filter( 'the_content', 'run_bbcodes', 7 );
function run_bbcodes( $content ) {