Skip to content

Instantly share code, notes, and snippets.

View hearvox's full-sized avatar

Barrett Golding hearvox

View GitHub Profile
@hearvox
hearvox / wp-mrp-to-acf-related-posts.php
Last active October 28, 2017 18:40
WordPress: Copy related post IDs from Microkids's Related Posts data to and ACF Relationship field (named: 'related_posts').
@hearvox
hearvox / wp-attach-media-by-custom-field.php
Last active June 26, 2017 22:02
WordPress: (very specific use script) Matches number in attachment (PDF) name to a custom field value and attaches media to that post.
<?php
/* Get unattached files in Media Library */
$args_search = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_parent' => 0,
'post_mime_type' => 'application/pdf', // Only PDFs.
'posts_per_page' => 40,
'orderby' => 'id',
'order' => 'ASC',
@hearvox
hearvox / wordpress-notes.php
Last active April 13, 2022 15:36
WordPress notes, admin URLs,, etc.
<?php
/*
WordPress settings list (serialized data hidden):
/wp-admin/options.php
Jetpack settings by modules (defualt hidden):
/wp-admin/admin.php?page=jetpack_modules
List Reusable Blocks (CPT):
@hearvox
hearvox / wp-ext-db-html-table.php
Created May 30, 2017 19:55
WordPress shortcode to access external database and output data in HTML table.
<?php
/**
* Shortcode accesses external database and returns data in HTML table.
*
* Shortcode is: [my-db-data].
* Uses WP class:
* @link https://developer.wordpress.org/reference/classes/wpdb/
* Sets transient with db data and 24 hour expiration.
*
* @return string $data_html HTML table with database data
@hearvox
hearvox / wp-remove-comments-supports.php
Last active May 30, 2017 19:52
Removes Comments from WordPress admin, inc. Admin Bar, Dashboard, column in Posts/Pages lists, and meta box in Edit Posts.
<?php
/*******************************
=Comments: Remove
******************************/
/* Remove comments support from native post types
*
* Removes Comments column from admin post lists and Comments meta-box from Edit Post.
*/
function headecon_remove_comments() {
remove_post_type_support( 'post', 'comments' );
@hearvox
hearvox / wp-top-parent-body-class.php
Last active February 23, 2023 16:22
Add class name for top-level parent category or page to body tag in WordPress posts, pages, and archives.
<?php
/**
* Insert class into body tag for highest level Category or Page.
*
* Pages and Categories with identical slugs get the same class.
* Works on Pages, Posts, and Category achives.
*
* @return string $top_slug Class name for body tag.
*/
function top_cat_or_page_body_class( $class ) {
@hearvox
hearvox / wp-script-vers-filemod.php
Created May 29, 2017 14:44
Use filemod timestamp for version number for WordPress script registration, to bust cache.
<?php
/**
* Use filemod timestamp for version number.
*
* For setting cache-busting version number in script registrations. Usage:
* wp_register_script( 'handle', $file_url, array(), headecon_filemod_vers( $file_path ) );
*
* @param string $path Path of script/style file
* @return string $vers File-modification timestamp or WordPress version
*/
@hearvox
hearvox / wp-insert-attach-media.php
Last active October 19, 2021 08:29
WordPress script adds files to Media Library; attaches files to posts.
<?php
/**
* Add files to Media Library; attach files to posts.
*
* This is for adding files to Media Library already in /uploads/.
* If files are elsewhere, adapt script.
*
* Attempts to attach file to post:
* Searches thru post content for the filename.
* If a post is found, then uses post ID and publish-date
@hearvox
hearvox / wp-list-media-limit-mime-type.php
Last active May 28, 2017 14:30
List GUIDs of all attachments in WordPress Media Library, limited by mime-type, e.g, PDFs only.
<?php
/* List GUIDs of all files of specified mime-type in Media Library. */
$query_images_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'application/pdf', // Only PDFs.
'posts_per_page' => -1,
);
$query_images = new WP_Query( $query_images_args );
print_r( wp_list_pluck( $query_images->posts, 'guid' ) );
@hearvox
hearvox / yoast_filter_primary_tax.php
Last active April 19, 2017 15:32
Use Yoast SEO (WordPress plugin) Primary Category only on Category taxonomy.
<?php
/* Use Yoast SEO's Primary Category feature only on Categories. */
function myprefix_yoast_primary_tax( $all_taxonomies ) {
$args = array( 'name' => 'category' );
$all_taxonomies = get_taxonomies( $args, 'objects' );
return $all_taxonomies;
}
add_filter('wpseo_primary_term_taxonomies', 'myprefix_yoast_primary_tax' );
?>