Skip to content

Instantly share code, notes, and snippets.

@snrjosh
snrjosh / wpf-confirm.php
Last active June 18, 2024 16:00
Confirmation message
<?php
function wpf_dev_frontend_confirmation_message( $message, $form_data, $fields, $entry_id ) {
// Only run on my form with ID = 924
if ( absint( $form_data[ 'id' ] ) !== 924 ) {
return $message;
}
// Email form field - ID #1
$email_for_order_delivery = $fields[ '1' ][ 'value' ];
@snrjosh
snrjosh / wpf-populate-fields-same-form.php
Created March 22, 2024 16:39
WPForms - Populate a field from another field on same form
<?php
/**
* Populate a field from another field on same form
*
* @link https://wpforms.com/developers/how-to-pre-populate-fields-in-the-same-form/
*/
add_action( 'wpforms_wp_footer_end', function() {
?>
<script>
@snrjosh
snrjosh / wpf-dropdown-position.php
Last active March 22, 2024 16:41
Force WPForms Modern Dropdown field to open downwards
<?php
// Filter: https://wpforms.com/developers/wpforms_field_select_choicesjs_config/
//Dropdown Style must be set to Modern, see: https://a.supportally.com/i/xJag2i
function wpf_dev_modern_dropdown_position_bottom( $config, $forms ) {
// Change 18591 to an ID of your actual form or remove this condition to apply to all forms.
// See: https://wpforms.com/developers/how-to-locate-form-id-and-field-id/#form-id
if ( ! array_key_exists( 18591, $forms ) ) {
return $config;
@snrjosh
snrjosh / wpf-exclude-fields-in-notification-email.php
Last active March 22, 2024 16:41
Exclude individual fields from WPForms {all_fields} smart tag in notification email
<?php
add_filter( 'wpforms_entry_email_data', function ( $fields, $entry, $form_data ) {
// Bail early if form ID is not equal to 123
if ( $form_data['id'] !== 123 ) {
return $fields;
}
// Unset field from notifications with field ID #2
unset( $fields[2] );
return $fields;
@snrjosh
snrjosh / dynamic-html-attributes-acf-link.php
Created June 5, 2023 14:08
Add target="_blank" to external links.
@snrjosh
snrjosh / acf-relationship-filter-options-page.php
Created May 11, 2023 21:33
Filter posts on ACF Relationship field based on Options page
<?php
/**
* Filter posts queried on ACF Relationship field based on Options page
* Filter : https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
* $post_id : The Options page 'post_id' setting specified in acf_add_options_page
*
*/
//Modify which posts are queried based on Options page
add_filter('acf/fields/relationship/query/name=field_name', 'modify_rel_field_post_query', 10, 3);
@snrjosh
snrjosh / custom-title-content-labels-acf-form.php
Last active March 8, 2023 22:48
Change post_title and post_content Field Labels on ACF Form
<?php
/**
* https://www.advancedcustomfields.com/resources/acf_form/
*
* Modify ACF Form Post Title Field Label
* @ default 'Post Title'
*/
function acf_form_post_title_label( $field ) {
if ( is_page( 5 ) ) { // form on page ID 5.
$field['label'] = 'Product Name';
@snrjosh
snrjosh / dynamically-populate-gravity-form-select-acf.php
Last active March 8, 2023 18:31
Dynamically populate Gravity Forms Select field with ACF Select field choices
<?php
/*
* Dynamically populate Gravity Forms Select field with ACF Select field Choices
*
* Add form ID at end of each filter to target a specific form
*
* Ref: https://docs.gravityforms.com/dynamically-populating-drop-down-fields/
*
*/
@snrjosh
snrjosh / hide-acf-field-in-admin.php
Last active November 27, 2024 21:10
Hide ACF field on the Admin screen
<?php
/**
* Prevent a field from being rendered
* Filter : https://www.advancedcustomfields.com/resources/acf-prepare_field/
*
*/
function hide_acf_field_on_admin( $field ) {
// Don't show this field if it contains a value.
if ( $field['value'] ) {
@snrjosh
snrjosh / order-posts-multiple-meta-keys
Last active March 8, 2023 16:44
Order posts by multiple ACF fields
$args = array(
'meta_query' => array(
'meta_key_1' => array(
'key' => 'product_price', // field_name_1
'value' => array( 500, 1000 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
'meta_key_2' => array(
'key' => 'product_type', //field_name_2