Support

Account

Home Forums General Issues Select Field + Add your own option? Reply To: Select Field + Add your own option?

  • I did add some code that allows them to choose from a list of values in the database and then populate a text field by clicking one of those values. That works like a tag cloud (although I am only allowing one entry), but I realised I dont have the value slug saved and hidden behind the scenes. If interested here is the code that could be adapted into a Select dropdown but it doesn’t save the slug. Maybe I can with further code:

    function modify_project_field( $field )
    {
    	global $wpdb;
    
    	$projects_array = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta pm WHERE meta_key = 'my_project'",ARRAY_A);
    	
    	foreach($projects_array AS $project)
    	{
    		$projects[] = $project['meta_value'];
    	}
    	
    	foreach($projects AS $project)
    	{
    		$project_links[] = '<a href="#" class="my-select-value" data-value="'.$project.'">'.$project.'</a>';
    	}
    
    	if($project_links)
    	{
    	    echo '<p>Enter a new value or choose from existing:<br/>';
    	    echo implode(', ', $project_links).'</p>';
    	    
    	    echo "
    	    <script>
    	    jQuery('.my-select-value').on('click', function()
    	    {
    		    var field = jQuery('#acf-field_6346f63fec6c2');
    			var value = jQuery(this).attr('data-value');
    			field.val(value);
    		});
    		</script>
    	    ";
    	    
    	}
    }
    add_action('acf/render_field/key=field_6346f63fec6c2', 'modify_project_field');