Skip to content

Instantly share code, notes, and snippets.

View cabrailsford's full-sized avatar

Chris Brailsford cabrailsford

View GitHub Profile
@cabrailsford
cabrailsford / role-cleanup.php
Last active July 14, 2020 18:06
Functions to enforce showing of excerpt regardless of previous checkbox selection, and to hide the excerpt checkbox option.
<?php
// Update post to force show of excerpt
function edit_post_show_excerpt() {
$user = wp_get_current_user();
$unchecked = get_user_meta( $user->ID, 'metaboxhidden_post', true );
if( !empty( $unchecked ) ){
$key = array_search( 'postexcerpt', $unchecked );
if ( $key !== FALSE ) {
array_splice( $unchecked, $key, 1 );
update_user_meta( $user->ID, 'metaboxhidden_post', $unchecked );
@cabrailsford
cabrailsford / yoast-img.php
Created July 14, 2020 17:29
Function to get YoastSEO Open Graph image from postmeta, falling back to post_thumbnail if it exists.
<?php
function yoast_og_img( $classes = '', $link = false, $size = 'medium_large' ) {
$yoastImg = $yoastImgID = $post_thumb = false;
$before = $after = null;
$yoastImg = get_post_meta( get_the_ID(), '_yoast_wpseo_opengraph-image', true);
$yoastImgID = attachment_url_to_postid( $yoastImg );
$post_thumb = has_post_thumbnail( get_the_ID() );
@cabrailsford
cabrailsford / manga-release.php
Last active July 14, 2020 17:24
WordPress function to create or update custom taxonomy terms from an ACF date-picker on post save.
<?php
function create_manga_categories( $post_id ) {
$date = get_field('manga_group')['manga_release_date'];
if(get_post_type( $post_id ) == 'manga' && $date) {
$date_convert = DateTime::createFromFormat('Ymd', $date);
$date_converted = $date_convert->format('F Y');
$date_converted2 = $date_convert->format('F j');
$date_converted3 = $date_convert->format('F-j-Y');
$terms = [];
$parent_term_check = get_term_by('name', $date_converted, 'manga_release_date', 'ARRAY_A');
@cabrailsford
cabrailsford / scheduled_post_modified_date.php
Last active July 14, 2020 17:23
WordPress function to run when a scheduled post is published, to change the modified date/time to match. Currently, it will only show the last saved data, not its published information, unless you modify it after publishing.
<?php
// Scheduled posts should update modified date when published
function update_modified_date_to_post_date( $post ) {
$updated_data = [
'ID' => $post->ID,
'post_modified' => $post->post_date,
'post_modified_gmt' => $post->post_date_gmt
];
wp_update_post( $updated_data );
}