Support

Account

Forum Replies Created

  • I’ve done something similar with Gravity forms, whereby someone would submit content and I needed to use the added image field to set the featured image on the new post.

    The code used this:

    
    $upload_path = rgar($entry, '11');
    			if (!empty($upload_path)){
    				$attachmentid	= wp_create_image_id($upload_path, $member_post_id);
    				$setfeatimg		= set_post_thumbnail($member_post_id, $attachmentid);
    			}	
    

    Obviously, you would need to alter from a gravity form field to your.
    $member_post_id was the post ID I needed to add the image to

    
    function wp_create_image_id( $image_url, $parent_post_id = null ) {
    	// Bail if the image url isn't valid
    	if( empty( $image_url ) || ! esc_url( $image_url ) )
    		return false;
    	// Escape the url, just to be save
    	$image_url = esc_url( $image_url );
    	// Cache info on the wp uploads dir
    	$wp_upload_dir = wp_upload_dir();
    	// get the file path
    	$path = parse_url( $image_url, PHP_URL_PATH );
    	// File base name, e.g. image.jpg
    	$file_base_name = basename( $image_url );
    	// Full path, set up to work with a WP in a subdirectory or default location
    	if( site_url() != home_url() ) {
    		$home_path = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) );
    	} else {
    		$home_path = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
    	}
    	// Remove the trailing slash on the home path
    	$home_path = untrailingslashit( $home_path );
    	// Combine the two to get the uploaded file path
    	$uploaded_file_path = $home_path . $path;
    	// Check the type of file. We'll use this as the 'post_mime_type'.
    	$filetype = wp_check_filetype( $file_base_name, null );
    	// error check
    	if( !empty( $filetype ) && is_array( $filetype ) ) {
    		// Create attachment title - basically, pull out the text
    		$post_title = preg_replace( '/\.[^.]+$/', '', $file_base_name );
    		// Prepare an array of post data for the attachment.
    		$attachment = array(
    			'guid'           => $wp_upload_dir['url'] . '/' . basename( $uploaded_file_path ), 
    			'post_mime_type' => $filetype['type'],
    			'post_title'     => esc_attr( $post_title ),
    			'post_content'   => '',
    			'post_status'    => 'inherit'
    		);
    		// Set the post parent id if there is one
    		if( ! is_null( $parent_post_id ) && absint( $parent_post_id ) )
    			$attachment['post_parent'] = absint( $parent_post_id );
    		// Insert the attachment.
    		$attach_id = wp_insert_attachment( $attachment, $uploaded_file_path );
    		//Error check
    		if( !is_wp_error( $attach_id ) ) {
    			//Generate wp attachment meta data
    			if( file_exists( ABSPATH . 'wp-admin/includes/image.php') && file_exists( ABSPATH . 'wp-admin/includes/media.php') ) {
    				require_once( ABSPATH . 'wp-admin/includes/image.php' );
    				require_once( ABSPATH . 'wp-admin/includes/media.php' );
    				$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file_path );
    				wp_update_attachment_metadata( $attach_id, $attach_data );
    			} // end if file exists check
    		} // end if error check
    		return $attach_id; 
    	} else {
    		return false;
    	} // end if $filetype
    } // end function prg_create_image_id
    

    If you need to get the image path, please read this forum post

  • Hi @miksynder

    You would need to query your posts to see if your custom field exists, something like:

    
    <?php
    $args = array(
        'post_type'      => array('post', 'page'),
        'posts_per_page' => 10,
        'post_status'    => 'publish',
        'meta_query'     => array(
            'relation' => 'AND',
            array(
                'key'     => 'start_date',
                'compare' => 'EXISTS',
            ),
        ),
    );
    // query
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<h2>Amazing Events List</h2>
    	<ul>
    	<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    	<?php $start_date = get_field('start_date');?>
    		<li>
    			<a href="<?php the_permalink(); ?>">
    				<?php if($start_date): echo $start_date; endif; ?>
    				<?php the_title(); ?>
    			</a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
    
  • Try changing
    $field_value = get_field_object($field[‘name’]);

    To:
    $field_value = get_field_object('my_field');

    Ensure you set my_field to the name of the actual ACF field

  • Hi @nikcree

    If the fields are all from an options page, should they not be more like:

    
    if( get_field('acf_address','option') ||
    	 		get_field('acf_address_2','option') ||
    	 		get_field('acf_address_city','option') ||
    	 		get_field('acf_address_state','option') ||
    	 		get_field('acf_address_postcode','option') ||
    	 		get_field('acf_address_country','option')
    	 ): 
    
  • Hi Telion,

    If you create some additional fields, you can add them to the form.

    For example, if you create a field and select the taxonomy option, you can then specify categories as the taxonomy.

    When you call the form on your page, you can then include either individual fields OR you can call in the field group:

    
    acf_form(array(
        'field_groups'	=> array(320),
    ));
    

    The group ID comes from URL when you’re setting up the additional fields:
    wp-admin/post.php?post=320&action=edit

    If you want certain fields only, you can use the below example:

    
    acf_form(array(
        'fields'	=> array(
           'field_5a1e82b766ac6',
           'field_595b5f02d4a72',
        ),
    ));
    

    Just change the field keys to the ones you need.

  • 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 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

  • Hi @vicmar

    If you’ve added custom fields to the billing form, this information is store against the order ID.

    So on the thank you page, you need to access the order ID, then you should be able to access your custom field data.

    For example:
    $variable2 = get_field(‘billing_wooccm11’,$order_id);

    I also believe this line is wrong:
    add_action(‘woocommerce_thankyou’, ‘action_woocommerce_before_checkout_form’);

    You’re trying to use a hook for the thank you page but mixed with an action on the checkout page (I may be wrong here!)

    I think this is the correct hook:

    
    function action_woocommerce_thankyou( $order_get_id ) { 
    // your code goes here
    }; 
    add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 ); 
    

    Or you could copy the thank-you.php file to your theme and add the code directly – obviously you need to still

  • @ForbiddenChunk

    You say data is returned, so I’m assuming title, permalink and post ID return ok?

    What does cars.php include?

    Do you declare your ACF fields on the template?

    From memory, I think I had similar from an ajax call, does it work if you change your include to:

    include( locate_template( 'includes/cars.php', false, false ) );

  • Hi peggysmiller,

    It depends on what you’re trying to do? Can you explain a little more and perhaps share what you already have?

  • Hi All,
    Just to say, I found the issue for me was running WP 5.6.1

    The inserter helper panel simply wouldn’t show. After days of Googling, trying different code, the default theme and no other plugins (except ACF), I tried rolling back on a whim

    Once running 5.6, suddenly it started working again.

    Hopefully, this may help someone else if they come across this thread
    Cheers

  • Sorry to add to this. The preview (on hover) just seems to show a blue line: https://pasteboard.co/JMXWfHU.png

    Anyone else had this? Its almost as if the preview div is collapsed.

    I’ve disabled everything and tried the 2021 theme but still no luck – think I’m going a little insane on this one lol

  • Hi All,

    I could do with some help on the inserter help panel.

    I have the following code:

    
    	// register a one column block
    	acf_register_block_type(
    		array(
    		'name'				=> 'one-column',
    		'title'				=> __('One Column'),
    		'description'		=> __('A one column block.'),
    		'render_template' 	=> get_template_directory() . '/blocks/content-one-column.php',
    		'icon'				=> 'menu',
    		#'icon'				=> file_get_contents( get_template_directory() . '/images/blocks/one-column.svg' ),
    		#'mode'              => 'auto', #Available settings are auto, preview and edit. 
    		'supports' 			=> array( 'align' => false ),
    		'keywords'			=> array( 'one column', 'layout' ),
    		
    		#'render_template'   => get_template_directory() . '/blocks/content-one-column.php',
    		#'enqueue_script'    => get_template_directory_uri() . '/blocks/content-one-column.js',
    
    		'example'  			=> array(
    									'mode' => 'preview',
    									'attributes' => array(
    										'data' => array(
    											#'title'		=> "One Column Block",
    											'content'	=> "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent euismod sem eget tortor suscipit, et ultricies lectus ultrices.",
    											#'content'	=> '<img src="'.get_template_directory_uri() . '/images/block-preview/one-column.jpg'.'">'
    											#'gutenberg_preview' => __('<img src="'.get_template_directory_uri() . '/images/block-preview/one-column.jpg'.'">'),
    										)
    									)
    								)				
    		)
    		
    	);
    

    My template (blocks/content-one-column.php) includes the fields:
    – title
    – content
    So my understanding is you use those in the ‘data’ field and when you hover over the block you want to add, it would then show this content.

    I replaced this with an image (thought it would be easier to style). Oddly, this was working but has now stopped. I can’t even get the previous one to work.

    Am I right in thinking you just need the above? You don’t need to enqueue_script a js file?

    Be great to get this working, so any pointers are very much appreciated!

    Thanks

  • Just to follow on from this, I believe the issue is due to the preview.

    If I’ve got this right, the preview pane sets the size via in line styles, so the Bootstrap grid isn’t being applied as opposed to the actual browser resizing

    Can’t think of a way around it! Just be good to be more accurate on the preview front you see.

    Thanks

  • It’s ok, I found the issue, was a code discrepancy
    Apologies

  • Hi @prithsr ,

    No problem. It would go in the functions file.

    I’ve tried to add notes to help but appreciate it may not make sense

    Any questions, let me know

    Cheers

  • Hi @prithsr

    Just one other thought on this.

    You may need to move the date check conditional into the user results loop, this is because I assume the date info is set against the user, therefore, as you then get the user->ID from the user results loop, you can then access those custom fields!

    Cheers

  • Hi @prithsr

    Ok, if I’ve understood right, here’s how I think I’d possibly approach it:

    I’d use the acf/save_post function:

    add_action('acf/save_post', 'my_acf_save_post');
    function my_acf_save_post( $post_id ) {
    
        // Get newly saved values.
        $values = get_fields( $post_id );
    
        // Get post author ID
        $author_id = get_post_field ('post_author', $post_id);
    
        // Add a custom field to the user 
        update_user_meta( $author_id, 'some_custom_field', '1' );
    
    }

    You could then query the users via a php file you run on cron:

    
    	#query our users
    	$args  = array(
    		'role__in'     	=> array('editor'),
    		'role__not_in' 	=> array('administrator'),
    		'exclude'      	=> array(1),	
            'meta_query'        => array(
    			array(
    				'key'       => 'some_custom_field',
    				'value'     => 1
    			)
            )					
    	);	
    	$user_query = new WP_User_Query( $args );
    	$user = get_userdata( $user->ID );
    

    You could wrap the whole query in a date check to compare the current date with the date set by the user

    If the date matches and the query returns a result, you can then reset the custom meta for the user/author:

        // Reset custom field to the user 
        update_user_meta( $user->ID, 'some_custom_field', '0' );

    So roughly the email function could look like:

    ###################################################
    # Send Emails When custom_post_typePublished
    ###################################################
    add_action( 'transition_post_status', 'send_email_notification', 10, 3 );
    function send_email_notification( $new_status, $old_status, $post ) {
    
    	if ( 'publish' !== $new_status or 'publish' === $old_status || 'custom_post_type' !== get_post_type( $post ) )
    	return;
    	
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    	return;	
    	
    	#get the post ID, we can now use this on both publish and update functions
    	$post_id = $post->ID;	
    	
    	#add date check here
    	$todays_date = '';
    	$specific_date  = '';
    	if ($todays_date == $specific_date ) {
    
    		#query our users
    		$args  = array(
    			'role__in'     	=> array('subscriber'),
    			'role__not_in' 	=> array('administrator'),
    			'exclude'      	=> array(1),	
    			'meta_query'        => array(
    				array(
    					'key'       => 'some_custom_field', //only want to send to specific users
    					'value'     => '1'
    				)
    			)					
    		);	
    		$user_query = new WP_User_Query( $args );
    		
    		#get our email data if we have any results
    		if ( ! empty( $user_query->get_results() ) ) {
    			#create an array
    			$email_recipients = array();
    			foreach ( $user_query->get_results() as $user ) {
    				$user = get_userdata( $user->ID );
    					
    				$email_recipients[] = array(
    					'forename'	=> $user->first_name,
    					'email'		=> $user->user_email
    				);	
    				
    				update_user_meta( $user->ID, 'some_custom_field', '0' ); //reset the custom meta until front end form retriggers
    			}
    		}
    		
    		#filter duplicates (may not need this)
    		#$email_recipients = array_unique($email_recipients);	
    		
    		#proceed with the email if we have a result
    		if ($email_recipients) {
    	
    			foreach ($email_recipients as $recipients) {			
    	
    				$to			= $recipients['email'];
    				$subject	= "Email Subject Goes Here";
    				$message	= "<p>Dear {$recipients['forename']}</p>";
    	
    				#create the headers
    				$headers = array(
    					'Content-Type: text/html; charset=UTF-8',
    					#'Cc:' . '[email protected]',
    					#'Bcc:' . '[email protected]',				
    					'From: '.get_option( 'blogname' ).' <[email protected]>'
    				);	
    				#send the email
    				wp_mail ($to, $subject, $message, $headers);					
    			}
    		}	
    	
    	}
    
    }

    It may require some spit and polish but should be a rough starting point I hope!?

    Cheers

  • Morning @rudtek

    Thanks for confirming my code worked, that was useful.

    I ended up stripping anything else out my functions file, low and behold, the map displayed.

    I worked through adding the other functions back in until it broke. I then found the below was the reason why it wasn’t working:

    
    ###
    # REMOVE QUERY STRINGS FROM STATIC RESOURCES
    ###
    function cleanup_query_string( $src ){ 
    	$parts = explode( '?', $src ); 
    	return $parts[0]; 
    } 
    add_filter( 'script_loader_src', 'cleanup_query_string', 15, 1 ); 
    add_filter( 'style_loader_src', 'cleanup_query_string', 15, 1 );
    

    It now works!

    Thanks for your time and help!!

  • Thanks

    I still get the same issue

    I’m totally baffled as it’s a blank theme with only a couple of plugins but yet it won’t work!

    Very odd!

  • @rudtek yep, thought it was safer removing it in the forum!

  • Hi @rudtek

    Thanks for the reply. Can’t believe I missed the typo.

    Ok, I’ve amended the typo and tried with true (several ways) but still no joy:

    
    function prefix_enqueue_scripts() {
    
        wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=KEY' );   
    	#wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=KEY', true );  
    	#wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=KEY', '', true );  
        wp_enqueue_script( 'google-map-init', get_template_directory_uri() . '/js/acf-map.js', array('jquery', 'google-maps'), '', true);
    
    }
    add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );
    

    Checking the console logs, it shows:
    Google Maps JavaScript API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys

    If I check the source code, I can see the script is called but seems to get truncated:
    <script type='text/javascript' src='https://maps.googleapis.com/maps/api/js'></script>
    It’ missing the API key or is that correct?

    Thanks

Viewing 25 posts - 301 through 325 (of 366 total)