Support

Account

Forum Replies Created

  • It isn’t so much more control, as it is a better frontend experience. People typically don’t want the entire page to have to refresh these days when submitting a form that shouldn’t change the rest of the page layout.

    Wordpress has builtin capabilities for adding custom AJAX function endpoints.

    You can add an AJAX handler using this code:

    // Sent from admin_users
    add_action( 'wp_ajax_my_ajax_action', 'my_ajax_handler' );
    
    // Sent from non-admin and anonymous users
    add_action( 'wp_ajax_nopriv_my_ajax_action', 'my_ajax_handler' );
    
    function my_ajax_handler() {
        // Access $_POST to see what your javascript sent to the AJAX endpoint here
        $result = array(
            'success' => false,
            'errorMessage' => 'Uh oh! An error has occurred...'
        );
    
        // Manipulate your custom fields here, modifying $result as necessary
    
        echo json_encode( $result );
    
        // Note that AJAX handlers should exit() or wp_die() instead of returning
        wp_die();
    }
    
    wp_localize_script( 'your-js-script-id', 'ajax_object', array( 
        'ajax_url' => admin_url( 'admin-ajax.php' )
    ) );
    

    You may call this handler by sending an AJAX POST to the ajax_object.ajax_url defined above with action set to my_ajax_action (note that this is the suffix of the add_action calls’ action name.

    For example:

    var data = {
        'action': 'my_ajax_action',
        'someOtherData': 0
    };
    jQuery.post(ajax_object.ajax_url, data, function(response) {
        response = JSON.parse(response);
        if (response.success) {
            // Success processing
        } else {
            // What to do on failure....
        }
    });
  • update_field returns true on success and false on failure, but considers a useless update (meaning the value has not changed) failure.

    You could call update_field, and then compare the new value in $_POST to the value returned by get_field to see if the update actually took place correctly, and return a message based on that test.

    The underlying code is using WordPress’ update_metadata function; you could probably get the last MySQL error (maybe using $wpdb->last_error?) to get a more detailed error message on why an UPDATE statement failed.

  • Hi woutervanerp;

    I apologize for the long delay between now and my last response, things have been a bit hectic on my end.

    I’m not sure how you are executing your update if you aren’t using acf_form() to render the frontend form. If you are trying to update the post via standard WP functions, the acf action above won’t be fired.

    If you are absolutely sure you cannot use acf_form, then you would need to be properly serializing everything when storing to the database.

    That all said, as a frontend form, you would want to use the original version but with the standard save_post action as the hook instead of acf/save_post. You would also want to check for the proper form element names instead of $_POST[‘acf’][*].

    Depending on how you code this (I am assuming AJAX), you could return a status code in several different ways. The easiest in an AJAX call would be to add a json_encode followed by wp_die() at the end of your success callback.

    echo json_encode( array(
        'success'=> $success
    ) );
    
    wp_die();
  • It requires purchase to download or see the forums πŸ™

    I do not have that plugin unfortunately, so I cannot look at what it is doing that might interfere with ACF. I doubt it would be legal to give me a copy for debugging purposes either.

    In the meantime, I do not see anything special being done with WP_Query when the post_id is ‘option’, so I’m not sure how changing to WP_Query would alter how ACF handles have_rows( ‘something’, ‘option’ )…

  • So these are editors or contributors editing their own posts in the backend with ACF fields? Or is it a frontend form using acf_form()?

  • Let’s hold off on that a little bit in case we can find a fix for other users of WP Download Manager in conjunction with ACF. πŸ™‚

    Could you provide a URL to this plugin and a URL to its forums? I’m having a hard time figuring out which WP Download Manager you are referring to (there are apparently several with similar names)!

    Thanks!

  • That sounds tricky; I think I need to know more about your use case.

    Could you give me a basic walkthrough of what you are trying to do? It will let me come up with a better solution for you πŸ™‚

  • What is the name/URL of the plugin that is conflicting? I’d love to take a look myself πŸ™‚

  • No worries! You may do either a theme or plugin, depending on how you are adding this functionality. Let’s assume you are modifying a theme, in which case you could add the following to functions.php:

    
    if( function_exists('acf_add_options_page') ) {
     
    	$option_page = acf_add_options_page(array(
    		'page_title' 	=> 'Theme General Settings',
    		'menu_title' 	=> 'Theme Settings',
    		'menu_slug' 	=> 'theme-general-settings',
    		'capability' 	=> 'manage_options',
    		'redirect' 	=> false
    	));
     
    }
    

    Then you can setup a new field group in ACF, and select as a Rule to display the field group: Options Page is equal to <Your Options Page Name>.
    In this field group you would add another image field, for the default image.

    Finally, you may modify your code above as follows:

    
    <?php 
    
    // First try the image you want
    $image = get_field('photo102');
    if( empty($image) ): 
        // If that doesn't work, try to get the Option Page setting
        $image = get_field('photo_alternate', 'option');
    endif;
    
    // Now check for existence of either option having worked
    if( !empty($image) ):
    	// vars
    	$url = $image['url'];
    	$title = $image['title'];
    	$alt = $image['alt'];
    	$caption = $image['caption'];
    
    	// thumbnail
    	$size = 'member-listing';
    	$thumb = $image['sizes'][ $size ];
    	$width = $image['sizes'][ $size . '-width' ];
    	$height = $image['sizes'][ $size . '-height' ];
    
    	if( $caption ): ?>
    
    		<div class="wp-caption">
    
    	<?php endif; ?>
    
    	 <!--  see single-cdtxprofessional.php in this area for code for adding link to picture itself if you want that -->
    
    		<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
    
    	</a>
    
    	<?php if( $caption ): ?>
    
    			<p class="wp-caption-text"><?php echo $caption; ?></p>
    
    		</div>
    
    	<?php endif; ?>
    
    <?php endif; ?>
    

    ————————-

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Sorry about the issues! Glad you got it fixed though πŸ™‚

  • Hi woutervanerp,

    The acf/save_post action should help you here.

    This code should suffice for what you want, though I haven’t tested it:

    
    <?php
    
    function my_acf_save_post_callback( $success ) {
        // Do what you'd like here
    }
    
    function my_acf_save_post( $post_id ) {
        if ( !isset( $_POST['acf'] ) ){
            return;
        }
    
        $fields = $_POST['acf'];
    
        add_action( 'acf/save_post', function( $post_id ) use ( $fields ) {
            remove_action( 'acf/save_post', $this );
            $db_fields = array();
            foreach( $fields as $field_key => $value ) {
                $db_fields[$field_key] = get_field( $field_key, $post_id );
            }
            // Serialize the value arrays from $_POST and the Database, so that they are single-level arrays, and array_diff_assoc them to generate the array of differences...
            $success = empty( array_diff_assoc( array_map( 'serialize', $fields ), array_map( 'serialize', $db_fields ) ) );
            my_acf_save_post_callback( $success );
        }, 11 );
    }
    
    add_action( 'acf/save_post', 'my_acf_save_post', 9 );
    
    ?>
    

    EDIT:

    Actually, that would work for a form save, but not for update_field or add_row, which works entirely server-side.

    Code Update:

    
    function my_acf_update_field_wrapper( $field_key, $value, $callback, $post_id = null ) {
        $new_values = array(
            $field_key => $value
        );
    
        // We need to hook the default WordPress save_post action
        add_action( 'save_post', function( $post_id ) use ( $new_values ) {
            remove_action( 'save_post', $this );
            $db_fields = array();
            foreach( $new_values as $field_key => $value ) {
                $db_fields[$field_key] = get_field( $field_key, $post_id );
            }
            // Serialize the value arrays from $new_values and the Database, so that they are single-level arrays, and array_diff_assoc them to generate the array of differences...
            $callback ( empty( array_diff_assoc( array_map( 'serialize', $fields ), array_map( 'serialize', $db_fields ) ) ) );
        } );
        if ( $post_id == null ) {
            update_field( $field_key, $value );
        } else {
            update_field( $field_key, $value, $post_id );
        }
    }
    
    function my_acf_save_post_callback( $success ) {
        // Do what you'd like here
    }
    
    function my_func() {
        // We need to update a value here
        my_acf_update_field_wrapper( 'field_abcdef123456', 'new-value', 'my_acf_save_post_callback' );
    }
    

    ————————–

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Hi squarestarmedia,

    As you have observed, ACF needs to have a value present on a Post Object for field name<->field key lookups to function correctly.

    The solution would be to use the repeater field’s key, (e.g. ‘field_abcdef123456’) instead of the name (e.g. ‘repeater_name’) when referencing the repeater.

    If I understand your use case correctly, you might also want to purge all rows in the repeater, and then add the taxonomies, so as not to have stale taxonomies should some be removed (either as non-hierarchical, or as a change in structure for hierarchical taxonomies).

    Example:

    
    <?php
    // For every row in the repeater
    while ( have_rows( 'field_abcdef123456' ) ) {
        // Delete the first row, until it is empty
        delete_row('field_abcdef123456', 1);
    }
    
    // For each taxonomomy...
    add_row( 'field_abcdef123456', array(
        'sub-field-name-or-key' => 'sub-field-value',
        // ...
    ) );
    

    ——————

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Hi JacquesMivi,

    There is an option on the Taxonomy field type to “Load Terms” from the associated Post. You might try enabling that option for the field and setting the Taxonomy type in the “Taxonomy to display” filter to espresso_event_categories.

    If you need to change the “Taxonomy to display” on the fly, this code should help you:

    
    add_filter( 'acf/load_field/key=field_54dfccb977a11', 'tsm_load_post_category_field' );
    function tsm_load_post_category_field($field) {
    // Make sure to edit the $field object too, or AJAX lookups may return the wrong Taxonomy!
    $field['taxonomy'] = 'espresso_event_categories';
    return $field;
    }
    
    add_filter( 'acf/load_value/key=field_54dfccb977a11', 'tsm_load_post_category', 10, 3 );
    function tsm_load_post_category( $value, $post_id, $field) {
    	// get terms
    	$term_ids = wp_get_object_terms($post_id, array('espresso_event_categories'), array('fields' => 'ids', 'orderby' => 'none'));
    	
    	// error
    	if( is_wp_error($term_ids) ) {
    		return false;
    	}
    	
    	// return
    	return $term_ids;
    }
    

    ——————————–

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Hi stinkykong,

    You may do any of the above. I would suggest creating an additional field for the ‘alt image’, so that changing the alt image URL down the road would not require any code changes.

    Where you might wish to store this additional field is up to you. You might choose to make it a site-wide option and store it on an ACF Options Page, or make it more flexible by assigning it to a hierarchical taxonomy, such as the Post Category.

    Take a look at the acf_add_options_page documentation and How to get values from an options page.

    To test a field’s existence, you may do something like this:

    
    <?php
    
    $my_img_url = get_field('image-field-name-or-key');
    if ( !$my_img_url ) {
        $my_img_url = get_field('default-image-field-name-or-key', 'option');
        // ProTip: You may want to also check here if the option page default image hasn't been set either!
    }
    
    // Use $my_img_url here
    
    ?>
    

    HTH πŸ™‚

    ————————

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Your code is indeed fine. I setup an options page and checked the February blog archive, and it most definitely shows the options page repeater fields using your above code and field names within footer.php. Nor can I think of a reason why that wouldn’t work purely based on ACF…

    Perhaps there is a conditional somewhere in your footer.php that is preventing the above code from executing at all? Have you verified that it gets to the first have_rows() call by outputting something unconditionally, such as

    <pre><?php echo 'Where are my options!?'; ?></pre>
    <div class="section group">
                	<?php if( have_rows('footer_widgets', 'option') ): ?>
    				<?php while( have_rows('footer_widgets', 'option') ): the_row(); ?>                
                        <div class="col span_1_of_2">                        
                            <h2><?php the_sub_field('heading'); ?></h2>
                            <?php the_sub_field('description'); ?>
                            <a href="<?php the_sub_field('button_link'); ?>" class="footer-cta-btn"><?php the_sub_field('button_name'); ?></a> 
                        </div>                 
                   <?php endwhile; ?>
                   <?php endif; ?>
                   </div>

    Or perhaps your archive template is calling a different footer template entirely, using get_footer( $name ) which loads footer-name.php.

    ——————————-

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Hi naz,

    A few possibilities. One, is the Relational Post Object field set to output the Post ID or the Post Object? Just figured I’d ask in case it’s an easy fix, as it should be Post Object by the way you are using it πŸ™‚

    Second, retrieval by field name will only work if a value for that field has already been saved to th post/page in question, so that field-name<->field-key lookup data has been populated. Check out get_field_object() documentation, under the heading field_name vs field_key. It may not be readily apparent from the documentation, but for all functions that take a field name, you may (and really should) specify a field key. You may view the field keys of your fields by selecting Field Keys in the Screen Options of the Field Group add/edit screens.

    Third, not specifying a post_id will only work if there is a current post setup. Is this code being executed within The Loop? Remember that even pages have a loop; it retrieves a single “post”. If not, you might want to specify the second parameter to get_field() ($post_id) or move your call to within The Loop.

    ————————

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Hi FrankiesFolio,

    In order to get an option field from an ACF Options Page, you need to use the string ‘option’ as the $post_id parameter in your get_field() call. How to get values from an options page documentation.

    The location of the call in your templates should not matter.

    Here is some sample code to get values from an options page repeater:

    
    <?php if( have_rows('repeater-field-name-or-field-key', 'option') ): ?>
        <ul>
        <?php while( have_rows('repeater-field-name-or-field-key', 'option') ): the_row(); ?>
            <li><?php the_sub_field('page_for_posts'); ?></li>
        <?php endwhile; ?>
        </ul>
    <?php endif; ?>
    

    ————————————————–

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

Viewing 17 posts - 1 through 17 (of 17 total)