Home › Forums › General Issues › Update 'Message' field manually in PHP
Hi,
Just wondering if anyone has any ideas for how to update the ACF ‘Message’ field in the admin area manually in PHP.
I’d like to be able to update this field manually with statistical information and for it to not be a Text Area/WYSIWYG field (for formatting), and instead have it as flat data that user can’t edit.
I noticed that using the Field Key in conjunction with ‘update_field’ didn’t seem to have any effect on the message field value (whereas it does work with the other field types mention above).
Any ideas?
I’ve cobbled together a quick solution to do this.
Anyone have a better idea let me know!
update_message_field('field_53f8663f25abd', '<b>My message</b>');
function update_message_field($field_key='', $message='')
{
global $wpdb;
$table = $wpdb->prefix.'postmeta';
$field = $wpdb->get_results("SELECT * FROM $table WHERE meta_key = '$field_key'");
if($field)
{
$meta = unserialize($field[0]->meta_value);
$meta['message'] = $message;
$wpdb->update(
$table,
array(
'meta_value'=>serialize($meta)
),
array('meta_key'=>$field_key),
array('%s')
);
}
}
Missed the ACF Pro update. So I see its not using postmeta anymore…
Here’s the updated code for anyone thats looking.
function update_message_field($field_key='', $message='')
{
global $wpdb;
$table = $wpdb->prefix.'posts';
$field = $wpdb->get_results("SELECT * FROM $table WHERE post_name = '$field_key' AND post_type='acf-field'");
if($field)
{
$meta = unserialize($field[0]->post_content);
$meta['message'] = $message;
$wpdb->update(
$table,
array(
'post_content'=>serialize($meta)
),
array('post_name'=>$field_key, 'post_type'=>'acf-field'),
array('%s')
);
}
}
Why so complicated, are you really need this ‘Message’ field?
function stat_meta_box( $post_type, $post ) {
add_meta_box(
'stat_box',
'Summary',
function(){global $post;var_dump($post);}, // just for example
array( ), // blank or list of your post_types
'advanced' // or 'side'
);
}
add_action( 'add_meta_boxes', 'stat_meta_box', 10, 2 );
no extra queries or function chains but the same result
or my favorite way… clien-side scripts =)
function stat_box(){
if (get_post_type()=='YourPostTypeSlug'){
$out = '<script type="text/javascript">';
$out .= 'var myMessage = jQuery(\'.field_type-message:contains("[stat_message_anchor]")\');if (myMessage) myMessage.html(myMessage.html().replace("[stat_message_anchor]","Lorem ipsum goes here..."))'; //changing content
$out .= '</script>';
echo $out;
}
}
add_action('in_admin_footer', 'stat_box');
and just put the text “[stat_message_anchor]” in your message
The topic ‘Update 'Message' field manually in PHP’ is closed to new replies.
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.