Skip to content

Instantly share code, notes, and snippets.

@only-cliches
Last active March 2, 2020 20:48
Show Gist options
  • Save only-cliches/3ee59aa8c159954abfdc595def2c53d3 to your computer and use it in GitHub Desktop.
Save only-cliches/3ee59aa8c159954abfdc595def2c53d3 to your computer and use it in GitHub Desktop.
WP Grid Builder Add Meta Filter
// frontend -> templates -> blocks -> default-blocks.php
/**
* Retrieve the post meta-data key value
*
* @since 1.0.0
*
* @param string $key Meta-data key.
*/
function wpgb_get_metadata( $key = '' ) {
if ( empty( $key ) ) {
return;
}
$key = trim( $key );
$post = wpgb_get_post();
if ( ! isset( $post->metadata ) ) {
return apply_filters('wp_grid_builder/card/metadata', null, $key, $post);
}
$acf_field = wpgb_get_acf_field( $key );
if ( ! empty( $acf_field ) ) {
return apply_filters('wp_grid_builder/card/metadata', $acf_field, $key, $post);
}
if ( ! isset( $post->metadata[ $key ] ) ) {
return apply_filters('wp_grid_builder/card/metadata', null, $key, $post);
}
return apply_filters('wp_grid_builder/card/metadata', $post->metadata[ $key ], $key, $post);
}
/**
* Retrieve block action link
*
* @since 1.0.1 Check ACF link field url key for metadata.
* @since 1.0.0
*
* @param array $action Holds action args.
* @return string
*/
function wpgb_get_block_link( $action = [] ) {
if ( empty( $action['action_type'] ) ) {
return;
}
if ( 'link' !== $action['action_type'] ) {
return;
}
if ( empty( $action['link_url'] ) ) {
$action['link_url'] = 'single_post';
}
$post = wpgb_get_post();
switch ( $action['link_url'] ) {
case 'metadata':
$link = isset( $action['meta_key'] ) ? wpgb_get_metadata( $action['meta_key'] ) : '';
$link = ! empty( $link['url'] ) ? $link['url'] : $link; // If ACF link field (url key).
return apply_filters('wp_grid_builder/card/link', is_string( $link ) ? $link : '', $action, $post);
break;
case 'custom_url':
return apply_filters('wp_grid_builder/card/link', isset( $action['custom_url'] ) ? $action['custom_url'] : '', $action, $post);
break;
case 'author_page':
return apply_filters('wp_grid_builder/card/link', wpgb_get_author_posts_url(), $action, $post);
break;
default:
return apply_filters('wp_grid_builder/card/link', wpgb_get_the_permalink(), $action, $post);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment