Skip to content

Instantly share code, notes, and snippets.

Kellen Mace kellenmace

View GitHub Profile
@kellenmace
kellenmace / remove-cpt-slug-from-permalinks.php
Created May 25, 2020
Remove CPT Slug from Permalinks - WordPress Plugin
View remove-cpt-slug-from-permalinks.php
View check-if-user-exists-wordpress.php
<?php
/**
* @param int $user_id User ID.
*
* @return bool Whether the user exists.
*/
function does_user_exist( int $user_id ) : bool {
return (bool) get_users( [ 'include' => $user_id, 'fields' => 'ID' ] );
}
@kellenmace
kellenmace / PasswordChangeEmailHandler.php
Last active Mar 26, 2020
Disable "Password Changed" Email on WPGraphQL User Registration
View PasswordChangeEmailHandler.php
<?php
class PasswordChangeEmailHandler {
/**
* User ID of the registered user.
*
* @var int
*/
private $registered_user_id = 0;
@kellenmace
kellenmace / exclude-super-admins-from-wpgraphql-users-query.php
Created Mar 4, 2020
Exclude Super Admins from WPGraphQL Users Queries
View exclude-super-admins-from-wpgraphql-users-query.php
<?php
function filter_super_admin($result, $source, $args, $context, $info, $type_name, $field_key, $field, $field_resolver) {
if ( $type_name === 'RootQuery' && $field_key === 'users') {
$result['edges'] = array_filter($result['edges'], function($edge) {
return !is_super_admin((int)$edge["node"]);
});
}
return $result;
View delete-transients-by-prefix.php
<?php
/**
* Delete all transients from the database whose keys have a specific prefix.
*
* @param string $prefix The prefix. Example: 'my_cool_transient_'.
*/
function delete_transients_with_prefix( $prefix ) {
foreach ( get_transient_keys_with_prefix( $prefix ) as $key ) {
delete_transient( $key );
View delete-all-post-count-transients.php
<?php
function delete_all_post_count_transients() {
$post_types = get_post_types();
$years = get_all_possible_years();
$months = get_all_possible_months();
foreach ( $post_types as $post_type ) {
foreach ( $years as $year ) {
foreach ( $months as $month ) {
View set-post-count-transient.php
<?php
function set_post_count_transient( $count, $post_type, $year, $month ) {
$transient_key = "km_post_count_{$post_type}_{$year}_{$month}";
set_transient( $transient_key, $count, WEEK_IN_SECONDS );
}
@kellenmace
kellenmace / ShortcodeBlockModifier.php
Last active Feb 18, 2020
Render Shortcodes in WPGraphQL Gutenberg Blocks
View ShortcodeBlockModifier.php
<?php
namespace Harness\Gutenberg;
use WPGraphQLGutenberg\WPGraphQLGutenberg;
use WPGraphQL\Registry\TypeRegistry;
use Harness\Interfaces\Hookable;
class ShortcodeBlockModifier implements Hookable {
public function register_hooks() {
@kellenmace
kellenmace / array_find.php
Last active May 21, 2020
array_find() function for PHP
View array_find.php
<?php
/**
* Get the value of the first array element that satisfies the callback function.
* Similar to JavaScript's Array.prototype.find() method.
*
* @param array $array The array.
* @param callable $callback The callback function.
*
* @return mixed The value of the element, or null if not found.
@kellenmace
kellenmace / gutenberg-graphql-schema.txt
Created Sep 20, 2019
WordPress Gutenberg GraphQL Schema Definitions
View gutenberg-graphql-schema.txt
type CoreArchivesBlock implements Block {
attributes: CoreArchivesBlockAttributes
name: String!
innerBlocks: [Block]!
isValid: Boolean!
originalContent: String!
parentId: Int
parent: PostObjectTypesUnion
renderedContent: String!
}
You can’t perform that action at this time.