Skip to content

Instantly share code, notes, and snippets.

Avatar

Daniel Schutzsmith schutzsmith

View GitHub Profile
@schutzsmith
schutzsmith / functions.php
Created Jul 12, 2022
Override taxonomy archive query in WordPress
View functions.php
function wpdev_156674_pre_get_posts( $query ) {
if (
$query->is_main_query()
&& $query->is_tax( 'region' )
) {
// Manipulate $query here, for instance like so
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'event_date' );
$query->set( 'order', 'DESC' );
View how-to-update-acf-google-map-field.php
<?php
// The field accepts a value with this structure
$value = [
'address' => '123 Example St, Townsville XYZ 1234, Country',
'lat' => - 38.1486228,
'lng' => 144.360414,
'zoom' => 14,
'place_id' => 'Ei0xMjMgTW9vcmFib29sIFN0LCBHZWVsb25nIFZJQyAzMjIwLCBBdXN0cmFsaWEiMBIuChQKEgmX0JaIHBTUahFyH_LC9sYD8hB7KhQKEglDz-DYDxTUahECZY8QisCjzg',
'street_number' => 123,
@schutzsmith
schutzsmith / register-acf-options-page.php
Created Jun 3, 2022 — forked from mishterk/register-acf-options-page.php
How to register options pages in Advanced Custom Fields for WordPress (ACF). See https://www.awesomeacf.com/snippets/register-options-page/ for more details.
View register-acf-options-page.php
<?php
// register a top-level options page
if ( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page( [
'page_title' => 'My Options Page',
'menu_title' => 'My Options Page',
'menu_slug' => 'my-options-page',
'capability' => 'edit_posts',
'parent_slug' => '',
View load-flexi-field-layout-partials-in-templates.php
<?php
// In a post/page template, loop over the ACF flexible field layouts and load the partial
// responsible for rendering the layout.
while ( the_flexible_field('my_flexi_field') ) {
get_template_part( 'components/'. get_row_layout() );
}
View define-google-maps-api-key-for-acf.php
<?php
// Define this in the site's wp-config.php file.
define('GOOGLE_API_KEY', 'your-google-api-key-here');
// Add this to your functions.php file, or a config plugin/MU plugin.
add_filter( 'acf/fields/google_map/api', function ( $api ) {
$api['key'] = GOOGLE_API_KEY;
return $api;
@schutzsmith
schutzsmith / control-acf-menu-visibility.php
Created Jun 3, 2022 — forked from mishterk/control-acf-menu-visibility.php
Hide the ACF admin menu dynamically based on site domain
View control-acf-menu-visibility.php
<?php
add_filter( 'acf/settings/show_admin', function () {
// Get the current site url.
$site_url = get_bloginfo( 'url' );
// Define an array of protected site urls.
$protected_urls = array(
'https://www.example.com',
'http://staging.example.com'
View disable-core-meta-metabox.php
<?php
add_filter('acf/settings/remove_wp_meta_box', '__return_true');
@schutzsmith
schutzsmith / acf-php-license-config.php
Created Jun 3, 2022 — forked from mishterk/acf-php-license-config.php
How to define the ACF PRO license key in code rather than storing it in the WordPress database. This Gist accompanies the article: https://www.awesomeacf.com/snippets/store-acf-pro-license-key-in-wordpress-php-configuration/
View acf-php-license-config.php
<?php
// Add this in your wp-config.php file
define('ACF_PRO_LICENSE', '{YOUR LICENSE KEY HERE}' );
@schutzsmith
schutzsmith / acf-field-group-php-to-json.php
Created Jun 3, 2022 — forked from mishterk/acf-field-group-php-to-json.php
Convert an ACF Field Group from PHP to ACF-JSON
View acf-field-group-php-to-json.php
<?php
// Get all the local field groups and loop through them for processing.
$field_groups = acf_get_local_field_groups();
foreach ( $field_groups as $group ) {
// If the field group has fields, load them into the 'fields' key.
if ( acf_have_local_fields( $group['key'] ) ) {
$group['fields'] = acf_get_local_fields( $group['key'] );
}
View register-acf-json-load-directory.php
<?php
add_filter( 'acf/settings/load_json', function ( $paths ) {
$paths[] = get_template_directory() . '/some/custom/dir';
return $paths;
} );