Skip to content

Instantly share code, notes, and snippets.

View brandondove's full-sized avatar

Brandon Dove brandondove

View GitHub Profile
<?php
/**
* POST to endpoint /wp-json/my-custom-endpoint/v1/save-string
*
* Send a string and use "Application Passwords" plugin and save to option
*/
add_action( 'rest_api_init', 'my_custom_endpoint', 10 );
/**
@brandondove
brandondove / post-to-microservice.php
Last active September 13, 2019 17:26
Example of using WordPress Core's HTTP API to call a microservice
<?php
/*
* There are many different actions you might
* hook into to trigger a post to your microservice
*/
add_action( 'sample_action', 'post_to_microservice', 10 );
function post_to_microservice() {
<?php
/**
* Allows you to run new code during the custom post type registration
*
* @return void
*/
function example_ads_init() {
register_taxonomy(
'ads-formats',
<?php
// Hook into WordPress before the query runs
add_action( 'pre_get_posts', 'pj_sort_it_better' );
// Modifies the WP_Query object
function pj_sort_it_better( $query ) {
if ( is_page( 'The Page Name' ) ) {
<?php
// Hook into the init action
add_action( 'wp', 'pj_check_page' );
// Checks to see if the page we want is being requested and adds a filter to any queries that run on the resulting page
function pj_check_page () {
if ( is_page( 'The Page Name' ) ) {
// for pagination
<?php
/**
* Allows you to modify the css classes that are applied to an ad unit so you can style each unit individually
*/
function example_adsanity_post_class( $classes = array(), $post = false ) {
// Adds a dynamic class that utilizes data from the post object
if ( FALSE !== $post && $post instanceof WP_Post ) {
$classes[] = sprintf( 'custom-%d', $post->ID );
}
<?php
/**
* Allows you to modify any part of the custom post type registration
*
* see: https://codex.wordpress.org/Function_Reference/register_post_type
*/
function example_ads_setup( $args = array() ) {
// Change the permalink structure for the custom post type
$args['rewrite'] = array(
<?php
/**
* Allows you to modify the custom post type labels for the adsa custom post type
*/
function example_pj_ads_labels( $labels = array() ) {
$labels['menu_name'] = __( 'Whitelabeled Ads', 'example' );
return $labels;
}
<?php
/**
* Adds new responsive ad unit sizes to the selectable options when creating an ad
*/
function example_adsanity_ads_posts_sortable_by_clicks( $vars = array() ) {
$vars = array_merge(
$vars,
array(
'meta_key' => sprintf( '_clicks-%s', mktime( 0,0,0, date( 'n' ), date( 'j' ), date( 'Y' ) ) ),
<?php
/**
* Adds new responsive ad unit sizes to the selectable options when creating an ad
*/
function example_adsanity_ad_sizes( $sizes = array() ) {
$sizes['1x3'] = __( '1x3 - Responsive', 'example' );
$sizes['2x3'] = __( '2x3 - Responsive', 'example' );
return $sizes;