Skip to content

Instantly share code, notes, and snippets.

Avatar

David Ryan dryan1144

View GitHub Profile
@dryan1144
dryan1144 / setup-meta-transient-rest-api.php
Created Dec 24, 2019
Define basic custom field and callback function
View setup-meta-transient-rest-api.php
<?php
/***
Register custom field for existing endpoint with callback function
Endpoint: /wp/v2/your_post_type
Note: Could also be a custom endpoint using register_rest_route
***/
register_rest_field('your_post_type', 'custom_field_name', array(
@dryan1144
dryan1144 / post-meta-transient.php
Last active Dec 24, 2019
Stores transient in post meta
View post-meta-transient.php
<?php
/***
Stores persisent data as post meta instead of using Transient API
@param $post_id int post ID to store post meta for
@parm $meta_key str "transient" name
@param $update_func str function to update the meta key with a new value
@param $expiration time when value should expire
@dryan1144
dryan1144 / delete-transients.php
Last active Dec 24, 2019
Clear transient on post save
View delete-transients.php
<?php
function yournamespace_delete_transient( $post_id, $post, $update ) {
delete_transient('yournamespace_data_example');
}
//{your_post_type} with a custom post type
add_action('save_post_{your_post_type}', 'yournamespace_delete_transient', 10, 3);
@dryan1144
dryan1144 / wp-rest-transient-example.php
Last active Dec 24, 2019
Sets a transient for elements in a WP REST API call
View wp-rest-transient-example.php
<?php
function yournamespace_rest_transient_example() {
//check for transient first
$data = get_transient( 'yournamespace_data_example' );
if ( !empty( $data ) ) {
$data = $terms_transient;
@dryan1144
dryan1144 / filter-taxonomy-term-url.php
Created Jul 26, 2017
Filter taxonomy term URLs for SF Pro
View filter-taxonomy-term-url.php
<?php
function hck_filter_taxonomy_archives( $url, $sfid) {
if (is_tax('topics')) {
$slug = get_queried_object()->slug;
$url = site_url() .'/'. get_post_type() .'/'. $slug;
}
return $url;
View rewrite-shared-terms-archives.php
<?php
function hck_rewrite_post_types() {
add_rewrite_rule('^articles/([^/]*)/?','index.php?post_type=articles&topics=$matches[1]','top');
add_rewrite_rule('^questions/([^/]*)/?','index.php?post_type=questions&topics=$matches[1]','top');
add_rewrite_rule('^amas/([^/]*)/?','index.php?post_type=amas&topics=$matches[1]','top');
View cpt-with-shared-taxonomy.php
<?php
function hck_questions_cpt() {
$labels = array(
'name' => _x( 'Topics', 'healthcarekit' ),
'singular_name' => _x( 'Topic', 'healthcarekit' ),
'search_items' => __( 'Search Topics', 'healthcarekit' ),
'all_items' => __( 'All Topics', 'healthcarekit' ),
'parent_item' => __( 'Parent Topic', 'healthcarekit' ),
View filter-sf-pro-query.php
<?php
function hck_filter_taxonomy_archives( $query_args, $sfid ) {
if( $sfid == 509 ) {
$query_args = array(
'post_type' => 'articles',
);
@dryan1144
dryan1144 / woocommerce-functions.php
Last active Dec 25, 2018
Change WooCommerce order confirmation text in
View woocommerce-functions.php
<?php
add_filter( 'woocommerce_thankyou_order_received_text', 'my_custom_order_received_text' );
function my_custom_order_received_text() { ?>
<div class="order-received-text">
<h5>Your Order Was Successful!</h5>
<p>Congratulations and a huge thank you for registering with us!</p>
<p>You will receive a confirmation email shortly. If you have questions, please do not hesitate to <a href="/contact">contact us</a></p>
<p>Thanks again!</p>
View woocommerce-titles.php
<?php
if (is_product()) {
echo '<h1>Register</h1>';
} elseif (is_cart()) {
echo '<h1>Review Order</h1>';
You can’t perform that action at this time.