Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • @hube2

    So you want to create a taxonomy based on the location added to a post?

    Right!

    Is the new term supposed to be added to the post?

    Yes, the terms should be stored in the post. Term_relation or what it is called.

    How will this be used?

    It will be shown on the single post and so the visitor can click on it and see what other posts are in the state, city or post_code.

    So, I just want to add the google Map details, to my existing taxomonies – under the hood, automatic, the User does not have to do anything.

    Denis

  • A taxonomy field only stores the term ID, it does not store the them name or any other information related to the term as part of post meta. If you want to search posts by term names then you need to copy the term name(s) to some post meta field when the post is updated so that it is available for searching. Either this or your custom search must include a taxonomy query and I’m not sure how you’d accomplish that.

  • I don’t really have enough information. You said you want to add these values to a taxonomy, so that’s what I’m going on.

    But how do I put the info $location[ ‘city’ ] and $location[ ‘state’ ] into the Taxonomies.

    I would need more information on what this means

  • 
    update_field( 'new_spot_city', $location[ 'city' ], $post_id );	
    

    $post_id needs to be altered to the correct value for the taxonomy term you want to save the values to. This can be a term object or a string "term_{$term_id}"

  • Thank you for your reply.

    I did try that. Interestingly, when the field in question is of type: Select, it returns an object every time. However, if the field is of type: text, it only returns the object once. Then Null (false) for the rest of the loop.

  • I’ve used the below which grabs values from an option page, then creates a CSS file and enqueues it:

    
    ###################################################
    # Create Custom CSS
    ###################################################
    function acf_generate_options_css() {
    	$ss_dir = get_stylesheet_directory();
    	ob_start(); // Capture all output into buffer
    	require($ss_dir . '/inc/custom-styles.php'); // Grab the custom-style.php file
    	$css = ob_get_clean(); // Store output in a variable, then flush the buffer
    	file_put_contents($ss_dir . '/css/custom-styles.css', $css, LOCK_EX); // Save it as a css file
    	
    }
    add_action( 'acf/save_post', 'acf_generate_options_css', 20 );
    

    I then enqueue the script:

    
    function acf_enqueue_scripts() {
        wp_enqueue_style( 'custom-theme-css', get_template_directory_uri() . '/css/custom-styles.css' );
    }
    add_action( 'wp_enqueue_scripts', 'acf_enqueue_scripts', 99 );
    

    And custom-styles.php looks like this:

    
    $colour_name = get_field('colour_name','option');
    $select_colour = get_field('select_colour','option');
    .bg-colour-<?php echo str_replace(" ","-",strtolower($colour_name)); ?> {
        background-color: <?php echo $select_colour; ?>;
    }
    

    Not sure if that helps at all?

  • Could you use something like the below:

    	$args = array(
    		'numberposts'	=> -1,
    		'post_type'		=> array('post', 'page'),
    		'meta_key'		=> 'colours', // your existing colur select field
    	);
    
    	// query
    	$the_query = new WP_Query( $args );
    	if( $the_query->have_posts() ):
    		$colours = array();
    		while( $the_query->have_posts() ) : $the_query->the_post();
    		$colours[] = get_field('colours');
    		endwhile;
    	endif;
    
    	// remove duplicates
    	$filtered_colours = array_unique($colours);
    	
    	if ( $filtered_colours ) :
    	
    
    		foreach ( $filtered_colours as $colour ) :
    			echo $colour;
    		endforeach;
    	endif;

    Depends on how you need to show/use the values I guess

  • I’m looking for this as well. I found a solution here: https://stackoverflow.com/a/20293486/1766219

    Note, that there is typo on that solution, which I have corrected in below-written solution:

    
    global $wpdb; // (this is required when are you inside the function)
    
    $values = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta pm, $wpdb->posts p WHERE meta_key  = 'NAME_OF_THE_FIELD_YOURE_AFTER' and pm.post_id=p.ID  and p.post_type='CUSTOM_POST_TYPE_NAME' ",ARRAY_A);
    
    print_r($values);
    
  • I’m not sure if something like this could work:

    
    <?php
    ##########################################
    #  Populate ACF field with list of colours
    ##########################################
    function load_colours_into_acf_select_field( $field ) {
    
    	$args = array(
    		'numberposts'	=> -1,
    		'post_type'		=> array('post', 'page'),
    		'meta_key'		=> 'colours', // your existing colur select field
    	);
    
    	// query
    	$the_query = new WP_Query( $args );
    	if( $the_query->have_posts() ):
    		$colours = array();
    		while( $the_query->have_posts() ) : $the_query->the_post();
    		$colours[] = get_field('colours');
    		endwhile;
    	endif;
    
    	// remove duplicates
    	$filtered_colours = array_unique($colours);
    	
    	if ( $filtered_colours ) :
    		//Add empty option
    		$field['choices']["0"] = 'Select a colour';
    
    		foreach ( $filtered_colours as $colour ) :
    			$field['choices'] = $colour;
    		endforeach;
    	endif;
    
    	return $field;
    }
    
    //Change colour_dropdown to your field which is assigned to your custom post type
    add_filter( 'acf/load_field/name=colour_dropdown', 'load_colours_into_acf_select_field' );
    

    Hopefully, the code/comments will help. It’s not tested and simply cobbled together, however, it should be a good starting point.

  • Hi idesignandhost

    Question 1.
    Yes, you can simply use php concatenate. For example:

    
    $field_1 = get_field('field_1');
    $field_2 = get_field('field_2');
    
    $join_me = 'Hello, using a . joins things together: '.$field_1.' and '.$field_2;

    Question 2
    You can try something like:

    
    function wp_disable_acf_fields( $field ) {
    	if( !is_admin() ){
    		$field['disabled'] = 1;
    	
    	}
    	return $field;
    }
    add_filter('acf/load_field/key=field_5996a05bc0d7d', 'wp_disable_acf_fields');	#replace field key
    
  • Hi @miksynder

    Could you not use the file field instead? Then add a field for each image size you require?

    Perhaps not ideal but a possible solution

  • This one has me stumped.

    Uploading an image, via ACF as usual, but I need to create multiple sized versions of that same image.
    I could just use the regular add_image_size but, this means any other image uploaded, also gets created at these sizes which is not what I want (there will be around 100+ post_thumbnails created each time).

    So, my question:
    Using ACF to upload an image, how would I then create multiple sized copies of that image?

  • I came here to report the exact same issue! Everything was fine until recently. Perhaps the last WordPress update broke something? I am using the same versions as you.

    Everything appears to be sending correctly and the response looks correct on the initial save, but the data is not saved.

    /wp-admin/admin-ajax.php?_fs_blog_admin=true

    Request

    acf[field_5fc3aad04255a]: 10
    acf[field_5fc7261aae126]: 2021
    action: acf/validate_save_post
    nonce: e31d46a48c
    post_id: 4987

    Response

    {“success”:true,”data”:{“valid”:1,”errors”:0}}

    The update sends the exact same data to the exact same endpoint and gets the exact same response, but it works.

  • Has there been any progress on this? I have a repeater field with a minimum of 2 items and in the repeater a WYSIWYG block, which have default (some Lorem ipsum text) and when I add a block to the Gutenberg editor a completely empty block is shown. If I click the editor and edit the content the preview becomes visible.

    The code above does nothing for me (or do I need to change some values to my field names?)

    I made a screencast to better illustrate the issue. As you can se when you edit the block it becomes visible

    https://www.youtube.com/watch?v=IzM-OMoHbWQ

  • One more detail : the MySQL version is 15.1

  • I found the problem, if your innerblocks is in a template locked you need to add templateLock="false" like that echo '<InnerBlocks allowedBlocks="' . esc_attr( wp_json_encode( $allowed_blocks ) ) . '" templateLock="false" />';

  • Here’s what I heard back from support in case it helps anybody else. It’s not a bug, per se, but related to the newer HTML escaping and now requires a filter, customized to your needs:

    ACF 5.10 introduced a new safer escaping system for anything rendered by ACF to prevent XSS attacks, by using WordPress’s kses system.

    You can find more information on this here: https://www.advancedcustomfields.com/resources/html-escaping/

    Specifically, at the bottom of that article you can see our code snippet you’ll need to add to your theme or plugin in order to allow SVGs inside your markup, as WordPress does not allow them by default.

  • Barring my if conditions being off, this is my working code for now…

    Big thanks for the help!!

    /**
     * ==============================================================================
     *             RENAME UPLOADED USER AVATAR IMAGE FILE WITH USERNAME
     *    When an image is uploaded to Edit User form through an ACF field (field_6140a6da30a17),
     *    rename file with the username of said user.
     * ==============================================================================
     */
    
    // 1. PASS USER_ID FROM USER-EDIT.PHP TO MEDIA UPLOADER, TO GET USERNAME FOR 
    // cf. https://support.advancedcustomfields.com/forums/topic/force-an-image-file-upload-to-a-particular-directory/
    // cf. https://wordpress.stackexchange.com/questions/395730/how-to-get-id-of-edit-user-page-during-wp-handle-upload-prefilter-whilst-in-med/395764?noredirect=1#comment577035_395764
    add_filter('plupload_default_params', function($params) {
        if (!function_exists('get_current_screen')) {
            return $params;
        }
        $current_screen = get_current_screen();
        if ($current_screen->id == 'user-edit') {
            $params['user_id'] = $_GET['user_id'];
        } elseif ($current_screen->id == 'profile') {
            $params['user_id'] = get_current_user_id();
        }
        return $params;
        });
    
    // 2. ON UPLOAD, DO THE RENAME
    // Filter, cf. https://wordpress.stackexchange.com/questions/168790/how-to-get-profile-user-id-when-uploading-image-via-media-uploader-on-profile-pa
    // p1: filter, p2: function to execute, p3: priority eg 10, p4: number of arguments eg 2
    add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
    function custom_upload_filter( $file ) {
        // Working with $POST contents of AJAX Media uploader
        $theuserid = $_POST['user_id'];         // Passed from user-edit.php via plupload_default_params function
        $acffield  = $_POST['_acfuploader'];    // ACF field key, inherent in $_POST
        // If user_id was present AND ACF field is for avatar Image
        if ( ($theuserid) && ($acffield=='field_6140a6da30a17') ) {
            // Get ID's username and rename file accordingly, cf. https://stackoverflow.com/a/3261107/1375163
            $user = get_userdata( $theuserid );
            $info = pathinfo($file['name']);
            $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
            $name = basename($file['name'], $ext);
            $file['name'] = $user->user_login . $ext;
            // Carry on
            return $file;
        // Else, just use original filename
        } else {
            return $file;
        }
    }
  • Your code with no = typo and reverting _GET to $params seems to work…

    add_filter('plupload_default_params', function($params) {
      if (!function_exists('get_current_screen')) {
        return $params;
      }
      $current_screen = get_current_screen();
      if ($current_screen->id == 'user-edit') {
        $params['user_id'] = $_GET['user_id'];
      } elseif ($current_screen->id == 'profile') {
        $params['user_id'] = get_current_user_id();
      }
      return $params;
    });

    That is:

    a) The depopulated lists issue is gone.

    b) It still seems to successfully pass user_id to the uploader, allowing me to fetch the username with which to rename the file.

    ++

    Now, for a bonus…

    In my wp_handle_upload_prefilter function custom_upload_filter(), I should really be checking that the image upload in question is coming from the specific ACF Image field keyed field_6140a6da30a17…

    Do you happen to know if the AJAX $_POST in custom_upload_filter() already has access to any information about the instigating field in question, anything which would give away field_6140a6da30a17? (I see traces of it in the uploader’s HTML).

    Or would I somehow need, on user-edit.php, to a) check for its presence and b), if present, send it through the plupload_default_params function? (I presume I can stuff $params[] with multiple other values).

  • I’ve been learning from Stackexchange.

    A. Allegedly, when the Media uploader Ajax appears, the user_id is no longer available. So there’s a solution (eventually arrived at) in calling the referrer at that point (wp_handle_upload_prefilter), then parsing out user_id, in order to fetch the user and username with which to rename the file. My working code does that.

    Still, I think I need a way to conditionalise that in two ways: i) only for uploads where the referrer is user-edit.php or profile.php, ii) only for uploads where the instigator is the ACF field with key field_6140a6da30a17. The latter one may get me back to square one – establishing a line of communication between the field group in admin and the media uploader box.

    B. The answer to my different question deals more specifically with adding parameters to an Ajax request. Don’t fully understand it yet, would need to take the time.

  • I was wrong, it is not in the ajax request, but the function get_current_user_id() is available during to use in your wp_handle_upload_prefilter filter.

  • If I can find some time I will see if I can test what is submitted. My thought is that the current user ID must be passed as part of the ajax request because WP would check to ensure that the user is logged in and has access to the media library.

  • To update the record…

    (1)

    The “Renaming” part in the following successfully renames a file to a username, retaining the file extension – but only if a valid user ID is supplied as $userid (hard-coded here as $userid=2)…

    // Filter entry, cf. https://wordpress.stackexchange.com/questions/168790/how-to-get-profile-user-id-when-uploading-image-via-media-uploader-on-profile-pa
    add_filter('wp_handle_upload_prefilter', 'my_pre_upload', 2, 2);
    // param 1: filter
    // param 2: function to execute
    // param 3: priority, eg 10
    // param 4: number of arguments, eg 2
    
    // pass the $userid as argument to your function
    function my_pre_upload($file, $userid=2){
        
        // if no user specified, get $current_user
        $userid = $userid ?: get_current_user_id();
        $user = get_userdata( $userid );
    
        // Renaming,  cf. https://stackoverflow.com/a/3261107/1375163
        $info = pathinfo($file['name']);
        $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
        $name = basename($file['name'], $ext);
        $file['name'] = $user->user_login . $ext;
    
        // Return
        return $file;
    }

    Therefore, the question becomes (still), at what point can I access user_id such that I can pass it through add_filter?

    (2)

    So, I am experimenting with whether I can set the user ID as a variable at the time the ACF field is loaded…

    // Get and remember User ID when field is loaded??
    
    function user_avatar_filter( $field ) {
    
        $myuserid = $_GET['user_id'];
        echo '<pre>load_field: GET user_id is '.$myuserid.'</pre>';
    
        // Important: return the field
        return $field;
    }
    add_filter('acf/load_field/key=field_6140a6da30a17', 'user_avatar_filter');

    For test, that code successfully echos out, to the ACF field’s part of the Edit User admin, the correct user ID based on whose Edit User page we are viewing.

    So, can that somehow be passed to my_pre_upload() as parameter $userid… ?

    I haven’t managed it, but I don’t know whether that’s down to a) my poor understanding of global/local variable context or b) that this still isn’t the way this works.

  • No problem.

    Below is the code I am using to grab a random selection from a repeater field (‘after_options’).

    <?php 
    	$options = get_field( 'after_options', 'option' );
    	if( is_array( $options ) ) {
    		$option = array_rand( $options ); 
    		$signupHTML = $options[$option]['html'];
    	} 
    ?>
    <div class="signupAfter">
    	<div class="signupText">
    		<?php echo $signupHTML; ?>
    	</div>
    	<div class="signupForm">
    		<input type="text" name="fname" class="fname" placeholder="First">
    		<input type="text" name="lname" class="lname" placeholder="Last">
    		<input type="text" name="email" class="email" placeholder="Email">		 
                    <span class="button">Subscribe</span>
    	</div>
    </div>
  • I suspect the error is because the fact that user id is not available and the die statement is causing the upload error.

    I would try to look into what is available in $_POST, although I don’t know exactly how you can do this because all of this is happening in an AJAX request (this is the reason your die statement is causing an error)

    You could try outputting the data to the error log

    
    ob_start(); print_r($_POST); error_log(ob_get_clean());
    

    You might find the user ID included in $_POST that is submitted for the AJAX request.

Viewing 25 results - 5,251 through 5,275 (of 21,337 total)