In adddition to hatsumatsu: Writing the timestamps should probably consider the current timezone settings of your WP installation. I was struggling with that for several hours now, checking the timestamps back and forth. Here’s my approach:
/**
* Convert values of named ACF date time pickers from Y-m-d H:i:s to timestamp
* @param string $value unmodified value
* @param int $post_id post ID
* @param object $field field object
* @return string modified value
*/
function acf_save_as_timestamp( $value, $post_id, $field ) {
if( $value ) {
$tz = new DateTimeZone(get_option('timezone_string'));
$date = DateTime::createFromFormat('Y-m-d H:i:s', $value, $tz);
$value = $date->getTimestamp();
}
return $value;
}
add_filter( 'acf/update_value/name=your_field_name', 'acf_save_as_timestamp', 10, 3 );
Or in case you want all date_time_pickers saving timestamps:
add_filter( 'acf/load_value/type=date_time_picker', 'acf_load_as_timestamp', 10, 3 );
Just in case someone needs that
F.Y.I: Checking the post vars shows the right value to save:
acf[field_55b9e5d6b6561] = 19691123, that means 23. November 1969. So it is stored right into the database.
I tried two things:
$timestamp = strotime('19691123');
$birthdate = date_i18n( get_option( 'date_format' ), $timestamp );
which brings the wrong day: 22. November 1969
$timezone = new DateTimeZone(get_option('timezone_string'));
setlocale(LC_TIME, "de_DE.utf8");
$geburtsdatum = '19691123;
$geburtsdatum_dt = new DateTime($geburtsdatum , $timezone);
$birthdate = $geburtsdatum_dt->format(get_option( 'date_format' ));
which brings the correct date, but unfortunately in english language.
I of course can replace the month names in the strings, but that does not solve the root of the problem: The field in the backend is still showing the wrong date. Any thoughts on this? I am a bit in despair …
A little error in the ss_acf_integrate class: Change these two:
/**
* Creates a json load point
*
* @since 1.0.0
*/
public function acf_json_load_point( $paths ) {
$paths[] = $this->acf_json;
return $paths;
}
/**
* Creates a json save point
*
* @since 1.0.0
*/
public function acf_json_save_point( $path ) {
$path = $this->acf_json;
return $path;
}
Very good idea, Keith. I bundeled this into a separate class that I can easily use in the plugin boilerplate. Assuming you have the ACF files in a folder “acf” in a directory “lib”:
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) )exit;
/**
* Integrate ACF if needed
*
* @author AndrΓ© R. Kohl <[email protected]>
*/
class ss_acf_integrate {
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
* @access protected
* @var string $plugin_name The string used to uniquely identify this plugin.
*/
protected $plugin_name = '';
/**
* The current version of the plugin.
*
* @since 1.0.0
* @access protected
* @var string $version The current version of the plugin.
*/
protected $version = '1.0.0';
/**
* The modus of ACF: Either "installed" if found as a plugin, "bundeled" when used via include ore false if not found
*
* @since 1.0.0
* @access public static
* @var string $acf_modus The used modus.
*/
public static $acf_modus;
/**
* The path to the bundeled ACF
*
* @since 1.0.0
* @access protected
* @var string $acf_dir The path to the folder.
*/
protected $acf_dir;
/**
* The URL to the bundeled ACF
*
* @since 1.0.0
* @access protected
* @var string $acf_url The url to the folder.
*/
protected $acf_url;
/**
* The path to the json files
*
* @since 1.0.0
* @access protected
* @var string $acf_json The path to the folder.
*/
protected $acf_json;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @var string $name The name of this plugin.
* @var string $version The version of this plugin.
*/
public function __construct( $name = false, $version = false ) {
if($name){
$this->plugin_name = $name;
}
if($version){
$this->plugin_version = $version;
}
$this->acf_dir = plugin_dir_path( dirname( __FILE__ ) ) .'lib/acf/';
$this->acf_url = plugin_dir_url( dirname( __FILE__ ) ) .'lib/acf/';
$this->acf_json = plugin_dir_path( dirname( __FILE__ ) ) .'lib/acf_json/';
if(is_dir ( $this->acf_json )) {
if(!is_writable($this->acf_json )){
chmod($this->acf_json , '777');
}
} else {
mkdir($this->acf_json, 0777, true);
}
if ( class_exists('acf') ){
self::$acf_modus = 'installed';
} else if(file_exists($this->acf_dir.'acf.php')) {
self::$acf_modus = 'bundeled';
} else {
self::$acf_modus = false;
}
$this->init();
}
/**
* Initiate the integration
*
* @since 1.0.0
*/
private function init(){
if(!self::$acf_modus){
return;
}
if ( 'bundeled' === self::$acf_modus ) {
// Customize ACF path
add_filter('acf/settings/path', array($this, 'acf_settings_path'));
// Customize ACF URL
add_filter('acf/settings/dir', array($this, 'acf_settings_dir'));
// Stop ACF upgrade notifications
add_filter( 'site_transient_update_plugins', array($this, 'stop_acf_update_notifications' ), 11);
// Create JSON save point
add_filter('acf/settings/save_json', array($this,'acf_json_save_point'));
// Include ACF
require_once( $this->acf_dir . 'acf.php' );
}
// Show/Hide ACF admin based on config
if(defined('ACF_SHOW_ADMIN') && false === ACF_SHOW_ADMIN){
add_filter('acf/settings/show_admin', '__return_false');
}
// Create a json load point
add_filter('acf/settings/load_json', array($this, 'acf_json_load_point'));
}
/**
* Filters the path to the ACF folder
*
* @since 1.0.0
*/
public function acf_settings_path( $path ) {
$path = $this->acf_dir ;
return $path;
}
/**
* Filters the URL to the ACF folder
*
* @since 1.0.0
*/
public function acf_settings_dir( $path ) {
$path = $this->acf_url ;
return $path;
}
/**
* Stops the upgrade notifications of ACF
*
* @since 1.0.0
*/
public function stop_acf_update_notifications( $value ) {
unset( $value->response[ $this->acf_dir . 'acf.php' ] );
return $value;
}
/**
* Creates a json load point
*
* @since 1.0.0
*/
public function acf_json_load_point( $paths ) {
$paths[] = $this->acf_json_load;
return $paths;
}
/**
* Creates a json save point
*
* @since 1.0.0
*/
public function acf_json_save_point( $path ) {
$path = $this->acf_json_save;
return $path;
}
/**
* Returns the current value of acf_modus for use in plugins or themes
*
* @since 1.0.0
*/
public static function acf_modus() {
return self::$acf_modus;
}
}
Call this class in the function load_dependencies() like this:
/**
* The class for integrating ACF
*/
if ( !class_exists( 'ss_acf_integrate ' ) ) {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-ss_acf_integrate.php';
new ss_acf_integrate( $this->get_plugin_name(), $this->get_version() );
}
I decided to have a little switch to enable/disable the ACF admin. In your wp-config.php add
// ACF Admin
define ('ACF_SHOW_ADMIN', true);
where obviously true means show the ACF admin and false means hide it.
Thanks again for your fantastic starting point.
Thank you for your patience, John. Sometimes one is simply too blind to see. I tried to replicate a function that is allready there. So all my matching stuff became obsolete at once. Thanks again for your help.
Did I ever mention that ACF became absolutely indespensable for me? I use it in every single project I work on.
Just checked the Version number again, it is 5.5.3. Sorry.
Well, it seems, the images aren’t coming through. So here are the direct links to the uploaded images:
Screen 1: https://postimg.org/image/mu19froax/
Screen 2: https://postimg.org/image/nktzljqo9/
Screen 3: https://postimg.org/image/zb7x2xjgp/
Screen 4: https://postimg.org/image/gulgge2gz/
No, I indeed use ACF 5, Version 5.4.6 to be exact. There is no controllers golder in the core folder.
To illustrate my problem better, I took four screenshots. The first one shows the rules that I use for the field group:
Next one is the add new portfolio screen. See the checked portfolio category CD Covers? The red arrow points to the missing meta box.
The third screenshot ist after editing a saved portfolio entry. Again notice the checked portfolio category.
The last screenshot shows the same saved portfolio after I unchecked the portfolio category. The Metabox remains in place.
Of course there aren’t any JS errors showing up in my Firebug Console. I hope the screenshots could explain my problem. MAybe I am simply too stupid, or I ran into a little issue.
Any Idea about that?
Again you brought me on the trail of something π acf_form redirects to the page where it was called on with a $_GET parameter of ?updated=true by default. To handle this on my own, I had to explicit set ‘return’ => ” in the options array of the acf_form.
The longer I work with acf, the deeper I get in. And my combined form works like a charm now.
Thanks for the support!
It’s a sort of a workaround, but it works. Thank you for your help. Could you probably tell me how to add another query variable to the acf redirect on the fly? I need some user feedback regarding the non acf fields. Something like
$url = add_query_arg( 'ss_login_msg', 'update_emailempty', $url );
But before the acf redirect kicks in. Ideas about that?
I tried to change the form method to get and my check of the $_POST to $_REQUEST. In this case, only the content of my normal form is updated, but not the acf_form fields. At first glance, I have a redirect or priority problem. Help anyone?
I’m trying to get all fields of a specific group for a very long time now. What I need is to get all fields assigned to a specific custom post type, or if not possible, all fields inside a field group, either by the title or by the post id of the group. Or at least anyhow.
Could anyone please provide a few bits of code for this? I am almost at wits end. Please?
I can’t believe that there is no internal function for such a common task. Imagine to create a post from the front end, how to fill the appropriate acf fields? Or imagine a plugin setting, that defines something around this post creation, like which fields are visible? All these tasks need to know which fields are associated.
Every helping hand is truly appreciated.I searched through the documentation and the forums for several times, and I almost googled myself to death.
Ah, I see. I allready thought that this would be redundant. But I think I try it the other way round: Add an additional Admin visible only profile field to the user profile via the hook edit_user profile, outside of the acf scope to assign the id of the cpt to the user.
I allready use a self coded frontend login/register process plugin that has hooks inside the edit form and the update user process, should be the right ones to display the additional user editable fields via acf_form nd to update the cpt fields.
I will post my progress soon hopefully π Thank you for your help so far.
@marvinlerouge: The enhanced database query searchs for post meta keys, that have the searchphrase as a value. As far as I can see, this would work with repeater fields as well:
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
let’s assume you have a repeater named ‘my_field’. In the Database, you will find entries like my_field_0_repeaterfieldname, my_field_1_repeaterfieldname, etc. So if you add either ‘my_field’ or ‘repeaterfieldname’ to the $list_searcheable_acf array, the above query will resolve i.e. to:
$where .= " (meta_key LIKE '%myfield%' AND meta_value LIKE '%$searchphrase%') ";
But as I stated before, I am by no means a real coder π
I tried another approach by looping through the acf groups first, but without any luck. Check this one:
$the_query = new WP_Query( array( 'post_type' => 'acf-field-group') );
// The Group Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$group_ID = get_the_ID();
$name = get_the_title();
echo '<p>Group ID: '.$group_ID.', Group name: '.$name.'</p>';
$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
if( $fields )
{
foreach( $fields as $field )
{
$value = get_field( $field['name'] );
echo '<dl>';
echo '<dt>' . $field['label'] . '</dt>';
echo '<dd>' .$value . '</dd>';
echo '</dl>';
}
}
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
No matter what, $fields is always empty π
Seems that this doesn’t work for me. I tried the following:
$the_query = new WP_Query( array( 'post_type' => 'acf-field-group') );
// The Group Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$group_ID = get_the_ID();
$name = get_the_title();
echo '<p>Group ID: '.$group_ID.', Group name: '.$name.'</p>';
#$content = get_the_content();
#quick(unserialize($content));
$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
if( $fields )
{
foreach( $fields as $field )
{
$value = get_field( $field['name'] );
echo '<dl>';
echo '<dt>' . $field['label'] . '</dt>';
echo '<dd>' .$value . '</dd>';
echo '</dl>';
}
}
echo '<hr>';
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
But $fields is allways empty. Any Idea?
… sometimes one is too blind to see. Found the solution in the
docs. Thanks anyway π
Hi,
first of all thank you for taking the time to answer my question. But I think, I did not explain myself well enough. The problem is not to get the file somewhere in the theme, after its saved, repeater or not. I need to hook into the moment when the file was uploaded, but BEFORE the data of the field set is stored into the database.
In other words: I create a post, populate the acf fields and click the save post button. That’s where the mumbo jumbo should happen: Create and store a preview file, move the uploaded file to another location, where it is not accessible by the public, give the file path of the newly created preview mp3 as the new value to the former file upload field and finally save the post and all it’s meta data.
Does that makes it somewhat clearer? Is there any hook, that I could make use of?
Would be great if we could have conditional logic on other fields as well, at least something like if textfield != ”
Please have look at the attached screenshot a few messages before. I can not use the admin screen of the fields propperly.
Thanks @ mediawerk, that solves the german language problem. But: Does anybody have the same issue in the admin like in my Screenshot?
Bad luck, there must be another error somehow, look at the attached Screenshot. I deactivated all other plugins except ACF Pro, so there is no other plugin involved.
Howewer, there is no JS Error showing up in Firebug.
In addition to this:
$locale = apply_filters( 'plugin_locale', get_locale(), $domain ); //theme_locale or plugin_locale, whether it is used in a theme or plugin
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.