Skip to content

Instantly share code, notes, and snippets.

View feedmeastraycat's full-sized avatar
🙊

David Mårtensson feedmeastraycat

🙊
View GitHub Profile
@feedmeastraycat
feedmeastraycat / cpt-without-slug.php
Last active October 26, 2018 23:30
An experimental way to register a CPT where you can have just /%postname%/ rewrite without a prefixed slug. Not fully tested so let me know if something doesn't work. I've wanted to do this from time to time but never really found a good way to do it without some issues.
<?php
/*
Plugin Name: CPT Without Slug
Description: A test plugin to register a CPT without slug
Author: David Mårtensson
Version: 0.0.4
Author URI: http://feedmeastraycat.net/
*/
add_action( 'init', 'mycpt_init' );
@feedmeastraycat
feedmeastraycat / woocommerce_order_actions.php
Last active March 9, 2016 11:09
Took me a couple of minutes to find how you add Order actions in WooCommerce so I thought I might write it down for someone else to Google find.
<?php
add_action('woocommerce_order_actions', 'my_woocommerce_order_actions', 10, 1);
function my_woocommerce_order_actions($actions) {
$actions['my_action'] = "Do my action";
return $actions;
}
add_action('woocommerce_order_action_my_action', 'do_my_action', 10, 1);
function do_my_action($order) {
// Do something here with the WooCommerce $order object
@feedmeastraycat
feedmeastraycat / gist:4514259
Created January 11, 2013 21:53
WP rewrite example.
<?php
add_rewrite_rule('english/news/([^/]+)(/[0-9]+)?/?$', 'index.php?english=$matches[1]&page=$matches[2]', 'top');
add_rewrite_rule('english/press/press-releases/([^/]+)(/[0-9]+)?/?$', 'index.php?english=$matches[1]&page=$matches[2]', 'top');
flush_rewrite_rules();
@feedmeastraycat
feedmeastraycat / gist:4223753
Created December 6, 2012 11:11
WordPress: Latest posts with date
<?php
$lq = new WP_Query(array('posts_per_page' => 10));
if ( $lq->have_posts() ): while ( $lq->have_posts() ): $lq->the_post(); ?>
<?php the_time('Y-m-d') ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>
<?php endwhile; endif;
wp_reset_postdata();
?>
@feedmeastraycat
feedmeastraycat / wp_get_archives_specific.php
Created July 7, 2012 11:34
WordPress: Display archive links based on type and format. Uses the built in wp_get_archives() but with added filters to display archive only for a specific year, month or day
<?php
/**
* Display archive links based on type and format. Uses the built in wp_get_archives() but with
* added filters to display archive only for a specific year, month or day
* @param string|array $args Optional. Override defaults.
* @param null|int $year Specify year for archive
* @param null|int $month Specify month for archive
* @param null|int $day Specify day for archive
* @return string|void Return or echo depending on "echo" argument
* @author David M&aring;rtensson <david.martensson@gmail.com>
@feedmeastraycat
feedmeastraycat / get_post_id_by_meta_key_and_value.php
Created July 7, 2012 11:32
WordPress: Get post id by meta key and value
<?php
if (!function_exists('get_post_id_by_meta_key_and_value')) {
/**
* Get post id from meta key and value
* @param string $key
* @param mixed $value
* @return int|bool
* @author David M&aring;rtensson <david.martensson@gmail.com>
*/
function get_post_id_by_meta_key_and_value($key, $value) {