Skip to content

Instantly share code, notes, and snippets.

View yoren's full-sized avatar

Yoren Chang yoren

View GitHub Profile
@yoren
yoren / functions.php
Last active March 21, 2023 23:15
Parent Post From Another Post Type And A New URL Structure In WordPress
<?php
// Flush rewrite rules after switch theme
function my_rewrite_flush() {
flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );
// A little help so we can get the stylesheet from parent theme
// Remove line 10-19 if this is not a child theme
function my_enqueue_styles() {
@yoren
yoren / functions.php
Last active September 14, 2022 21:02
Add Featured Image Thumbnail URL to wp-json/wp/v2/posts Endpoint
<?php
function my_rest_prepare_post( $data, $post, $request ) {
$_data = $data->data;
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$thumbnail = wp_get_attachment_image_src( $thumbnail_id );
$_data['featured_image_thumbnail_url'] = $thumbnail[0];
$data->data = $_data;
@yoren
yoren / gist:4553933
Last active May 26, 2022 01:03
WordPress: Select post parent from another post type
add_action('admin_menu', function() {
remove_meta_box('pageparentdiv', 'chapter', 'normal');
});
add_action('add_meta_boxes', function() {
add_meta_box('chapter-parent', 'Part', 'chapter_attributes_meta_box', 'chapter', 'side', 'high');
});
function chapter_attributes_meta_box($post) {
$post_type_object = get_post_type_object($post->post_type);
if ( $post_type_object->hierarchical ) {
@yoren
yoren / functions.php
Created October 25, 2016 09:23
Get terms by custom post types and taxonomy
<?php
/**
* my_terms_clauses
*
* filter the terms clauses
*
* @param $clauses array
* @param $taxonomy string
* @param $args array

Zip excluding specific directories

Exclude .git file and node_modules directory

$ zip -r9 [target_file] [source_file] -x *.git* node_modules/\*

Exclude .git file and files in node_modules directory, but keep node_modules directory

$ zip -r9 [target_file] [source_file] -x *.git* node_modules/**\*
@yoren
yoren / functions.php
Created July 24, 2015 15:59
Testing if a post is in a descendant category
<?php
/**
* Tests if any of a post's assigned categories are descendants of target categories
*
* @param int|array $cats The target categories. Integer ID or array of integer IDs
* @param int|object $_post The post. Omit to test the current post in the Loop or main query
* @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
* @see get_term_by() You can get a category by name or slug, then pass ID to this function
* @uses get_term_children() Passes $cats
* @uses in_category() Passes $_post (can be empty)
@yoren
yoren / functions.php
Last active April 20, 2020 15:38
Changing arguments for the built-in taxonomies in WordPress
<?php
function ofix_register_tax_args( $args, $taxonomy, $object_type ) {
if ( 'link_category' === $taxonomy ) {
$args['public'] = true;
$args['query_var'] = true;
}
return $args;
@yoren
yoren / main.html
Last active June 12, 2019 08:39
Display WordPress posts with AngularJS and JSON API
<ul>
<li ng-repeat="post in posts">
<a href="{{post.ID}}">
{{post.title}}
</a>
</li>
</ul>
### Keybase proof
I hereby claim:
* I am yoren on github.
* I am yoren (https://keybase.io/yoren) on keybase.
* I have a public key whose fingerprint is 87BF F3DA 7D0C 3BE8 1151 C583 8F39 C153 6FC8 D42C
To claim this, I am signing this object:
@yoren
yoren / functions.php
Last active February 17, 2019 20:03
Return the right previous_post_link / next_post_link when change posts order
<?php
function my_previous_post_where() {
global $post, $wpdb;
return $wpdb->prepare( "WHERE p.menu_order < %s AND p.post_type = %s AND p.post_status = 'publish'", $post->menu_order, $post->post_type);
}
add_filter( 'get_previous_post_where', 'my_previous_post_where' );