WooCommerce: Name of short description meta field - php

I want to populate the short description field of a WooCommerce product with Gravity Forms.
Unfortunately I couldn't figure out the name of the meta field(?).
Every other meta field works fine. I've checked the code and some docs to find the correct name but I couldn't find it.
I tried the following: excerpt, postexcerpt, post_excerpt.
In the backend code the field's name is excerpt. To use the field's name works for every other meta field.

This snippet will make the short description / excerpt show up as a mappable field:
add_filter( 'gform_advancedpostcreation_excerpt', 'enable_excerpt', 10, 1 );
function enable_excerpt( $enable_excerpt ){
return true;
}
https://docs.gravityforms.com/gform_advancedpostcreation_excerpt/#examples

The great support over at Gravity Forms pushed me in the right direction.
You have to use a custom snippet to map the form field with short description / excerpt:
add_action( 'gform_advancedpostcreation_post_after_creation', 'update_product_information', 10, 4 );
function update_product_information( $post_id, $feed, $entry, $form ){
//update the excerpt
$the_post = array(
'ID' => $post_id,//the ID of the Post
'post_excerpt' => $entry['60'],
);
wp_update_post( $the_post );
}
Here you can find some more informations: https://docs.gravityforms.com/advanced-post-creation-add-on-using-third-party-post-types/#handling-fields-unable-to-be-mapped-2

Related

Auto tag custom posts in Wordpress

I am looking for a way to auto tag custom posts in Wordpress without using a plugin.
I have a custom post type 'tech-video' and I want to auto tag it with the video tag every time a post gets published of that type.
I tried this code snippet but it doesn't work:
/* Auto Tag Tech Videos */
add_action('publish_tech_video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post){
wp_set_post_terms( $post_id, 'tech-video', 'video', true );
}
I'm not skilled with either PHP or Wordpress hooks so any help is appreciated.
Thank you,
You're close; You just got the hook name wrong.
Whenever a post is saved, the following is run:
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
To leverage this hook you can run:
add_action('save_post_tech_video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post){
// check the term has not already been added.
$terms = wp_get_post_terms($post->ID, 'video');
$term_names = array_map(function($term){return $term->name;},$terms);
if(!in_array('tech-video',$term_names){
wp_set_post_terms( $post_id, 'tech-video', 'video', true );
}
}
But note: Since the "save_post" hook is run every time the post is saved, you need to check that the term has not already been added.
Note that the signature for wp_set_post_terms is:
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false );
So this assumes that you have a registered taxonomy named "video", and the taxonomy is linked to the "tech_video" post type.
After doing some more digging into the Wordpress Codex I was able to figure out a more elegant solution that works great:
// Call when the post gets created
add_action('save_post_tech-video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post) {
// If the video tag doesn't exist, add it
if (!has_tag('video', $post->ID)) {
wp_set_post_tags( $post_id, 'video', true );
}
}
Note that I had to change tech_video to 'tech-video' to make it match the name defined by the Custom Post Type (and thus call properly).
I like this method because it's cleaner.
Thanks #andrew for pointing me in the right direction at least!

How to take custom field value (ACF plugin) to WordPress save_post hook?

I use Advance Custom Fields plugin to create some custom fields.
While creating new post or update post, I need to use api (vimeo) to take values to other custom field.
Example:
1 field: ID of video
2 field: duration
3 field: plays count
I enter value in first field, press "Publish" and use this hook:
add_action( 'save_post', 'vimeo_api', 10, 2 );
function vimeo_api( $post_id, $post ) {
// request to vimeo with video ID
update_post_meta( $post_id, 'video-duration', $vimeo_single['body']['duration']);
update_post_meta( $post_id, 'video-plays', $vimeo_single['body']['stats']['plays'] );
}
If I hard-code vimeo ID - it works!
But I can't get value from field 1.
For example I can get value of post title like this:
$title = $post->post_type;
But it doesn't works with ACF field.
In developer tools I see this in "headers" tab, form data:
_wpnonce:83ab5bcf5f
_wp_http_referer:/wp-admin/post.php?post=37&action=edit&message=1
user_ID:1
action:editpost
originalaction:editpost
post_author:1
post_type:video
...
fields[field_5423b0bb92209]:
fields[field_5423aff492207]:103222207
fields[field_5423b04192208]:
fields[field_5424dd92c4f3d]:
This return error Warning: Illegal string offset:
$vimeo_id = $post->fields['field_5423aff492207'];
Solved with acf/save_post hook
function get_video_info_from_vimeo ($post_id) {
$vimeo_id = get_field('field_5423aff492207', $post_id); // get id
// use api and get $duration and $plays
// unhook this function so it doesn't loop infinitely
remove_action('acf/save_post', 'get_video_info_from_vimeo');
// update ACF Price Field
update_field( 'field_5423b04192208', $duration, $post_id );
update_field( 'field_5424dd92c4f3d', $plays, $post_id );
// re-hook this function
add_action('acf/save_post', 'get_video_info_from_vimeo');
}
add_action( 'acf/save_post', 'get_video_info_from_vimeo' );

Advanced Custom Fields - front-end form, save title from another field?

I am using WordPress's Advanced Custom Fields 5 plugin to create a front-end form. I would like to save the post title as one of the fields on my form. For example, one of my form fields is 'name' so I would like the post title to be 'John Smith'.
Looking at the ACF documentation it gives example code (copied below) where pre_save_post can be hooked into achieve this. Hoewver, I have included this function and the title still fails to save.
Any ideas what I am doing wrong?
Here is the code:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => $_POST['fields']['field_123'] ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
I am using exactly the same method and it works fine.
Just a note that field_123 in your example is not the user defined field name in the wp back end but its actually the field name assigned in the meta_key value in the wp_postmeta table of you db.

How to get the image attachement custom link with PHP - Wordpress

This will return the link to the attachment:
$link=wp_get_attachment_link($image->ID);
However, I can't find a way to get the LINK TO value from the ATTACHMENT DISPLAY SETTINGS of an image. See screenshot below.
As yoavmatchulsky wrote, this field is dynamically filed by ~wp-includes/js/media-views.js after you manually choose an image
but if you have an id of attachment, use wp_get_attachment_link( $id, $size);
as size use 'full'
full ref. in codex
Or, if you are trying to use a custom link, the method described here might help.
Basically, in your functions.php, you could add a code similar to this:
// Adds a custom url field to your attachment
function attachment_custom_url( $form_fields, $post ) {
$form_fields['video-url'] = array(
'label' => 'CustomURL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'custom_url', true ),
'helps' => 'Add custom URL, if applicable',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'attachment_custom_url', 10, 2 );
function attachment_custom_url_save( $post, $attachment ) {
if( isset( $attachment['custom-url'] ) )
update_post_meta( $post['ID'], 'custom_url', esc_url( $attachment['custom-url'] ) );
return $post;
}
add_filter( 'attachment_fields_to_save', 'attachment_custom_url_save', 10, 2 );
And then, you could call it in your php like so:
<?php
Custom Link;
?>
I know this is an old thread, but I solve this by getting the post meta of the attachment, which was easier to me.
In my installation, custom URL input is shown like this:
<input type="text" class="text" id="attachments-140443-foogallery_custom_url" name="attachments[140443][foogallery_custom_url]" value="https://mycustomurl.com">
So, I assumed that if the first brackets contains the post ID, the second one is a meta key to save this value on wp_postmeta table. And there it was, just starting with the underscore character so it would be a hidden meta data. Therefore, the easier way to get this value is like this:
get_post_meta( get_post_thumbnail_id( get_the_ID() ), '_foogallery_custom_url', true);
Of course you need to check if the post does have a thumbnail, but that's easy to adapt.

Get custom fields values in filter on wp_insert_post_data

Hi all, thanks for reading.
Environment :
Wordpress + Advanced Custom Fields plugin
Problem :
I have searched for hours now and I can't seem to find the correct syntax to do the following:
When posting a new post, get custom field value in order to automatically replace the title of the post by that value. Example: I create a post and set '10am' in my 'time' custom field. The title of the post is automatically replaced by '10am'.
Example:
So I'm adding a filter with the following :
add_filter('wp_insert_post_data', 'change_title') ;
function change_title($data)
{
$time = XXX ; // How should I get this custom field value ?
$new_title = 'Topic created at'.$time ;
$data['post_title'] = $time ;
return $data;
}
It must be very simple but I have tried every function available on both WP and the plugin's documentations. I would be very thankful if anyone passing by gave me the solution.
Thanks in advance !
Tweak to Riadh's accepted answer (would add as a comment but haven't got enough rep yet):
As documented in the WordPress Codex wp_update_post includes the save_post hook so calling wp_update_post() inside the save_post hook creates an infinite loop. To avoid this, unhook then rehook your function like so:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
You can actually access the global $_POST variable for your field value , but i guess you can do it in a cleaner way by using the save_post action to update your post's title, eg:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
assuming that your ACF fieldname is "time".
Edit: Updated the answer as per Mark Chitty's answer.
You may try this
add_filter( 'wp_insert_post_data', 'change_title', '99', 2 );
function change_title($data , $postarr){
$custom_field = 'custom_filed_name';
$post_id = $postarr['ID'];
$time = get_post_meta( $post_id, $custom_field, true );
// Now you have the value, do whatever you want
}
Advanced Custom Fields creates a 'field key' for each custom field that is created. I was able to refer to this key value when trying to access the custom fields. The field key value can be found by viewing page source when viewing the post type within the Wordpress admin section.
Look for data-field-key. You will see a value similar to data-field-key="field_5847b00820f13" in the page source. Use this value when accessing the value in the $postarr argument in the wp_insert_post_data filter. The custom fields will be in a nested array named fields within the $postarr argument.
Alternatively, the field key value can be located by navigating to the Advanced Custom Fields / Export option from within the admin section. Once you are on the export page for Advanced Custom Fields, select the export to PHP option and you will see the value in the resulting PHP code.
In the example below, I am concatenating two Advanced Custom Fields and updating the post_title in the $data array returned from the function.
The result is that the post_title value will be saved to the database via the built in Wordpress save post logic.
add_filter('wp_insert_post_data', 'slb_set_title', '99', 2);
function slb_set_title ($data, $postarr){
if($data['post_type']==='slb_subscriber'){
$data['post_title'] = $postarr['fields']['field_5847b00820f13'] .' '.
$postarr['fields']['field_5847b03f20f14'];
}
return $data;
}

Categories