I have an edit profile page with a form POST, in which there's an input file, that upload image via ajax on the server and in the media library and return the filename in a textarea.
I can't update field with the url of the image that I uploaded via ajax. The Image appears in the media library and in server folder, but my acf field doesn't update.
The field is set to return img link.
This is my code:
$wp_upload_dir = wp_upload_dir();
$filename_path = $wp_upload_dir['path'] . "/$img"; //img is the value of POST of the textarea and return the filename "ARCW8764.JPG"
$filename_url = $wp_upload_dir['url'] ."/$img";
$wp_filetype = wp_check_filetype(basename($filename_path), null );
$attachment = array(
'guid'=> $wp_upload_dir['url'] . '/' . basename( $filename_path ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => $img,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment);
update_field('foto-agente',$filename_url,$user);
What's wrong?
Given that everything else is correct. Probably you are not targeting the field correctly. You need to go to the database in the table that is stored. I am guessing either wp_postmeta or wp_usermeta and check the name of your field. It is probably something like something_foto-agente. This is how ACF is storing nested fields, ex: parent_child_grandchild_name-of-field.
/* if the field has no record, you will not find it. Ensure to store something from the admin first, then see the name of the field in DB and then try it from your code. Let me know, good luck!
Related
I have an odd request, i'm not sure if this is even possible. But i'll try to work out the process below, and if anyone can help me work this out that would be amazing!
Ideally the process is as follows:
Admin goes to parent options page, within the options page there is a repeater field, called add new company. This will just be a field with a title.
Admin fills in field and presses save. This will generate a sub options page with that name, within the options field, there will be a set of fields like logo, a colour picker and some text fields (these could be a set of fields from within ACF if thats possible).
Also when this original Repeater Field is made/saved a set of pages is generated from a set of templates. Essentially using the name from the repeater field to be the main page title for the top level page and all the sub pages below are just dynamically generated. They don't need to have anything different about them, they just need to generate from a set of page templates. It needs to be able to associate with the newly generated company bits from the sub options field.
This will then essentially give the admin a new set of pages which will use the new options logo / colours etc. It would almost need to generate a new set of templates based off the master templates to dynamically make sure it picked up the correct information from the sub options page.
I'm not sure if this is possible, I have seen it work elsewhere on another job I have worked on (not exactly the same as the above but similar), but I can't work out the process to make it work sadly, as I have a horrid feeling that there is some complex bits within the database going on to do the duplication dynamically.
My other option is to run everything as a WordPress Multisite but I was trying to avoid that if possible on this occasion, but I may have to use Multisite to achieve the above.
If anyone can help me work this out that would be amazing!
Thanks in advance for any help :)
You should be able to plug into the save_post action and create new subpages from there.
add_action( 'save_post', 'create_sub_pages' ); //Plug into save_post action
//Function create sub_pages
function create_sub_pages($post_ID) {
//Repeater field name
$repeater_field_array = get_field('repeater_field_name');
//Loops through all of the items in the repeater field
foreach($repeater_field_array as $key => $value) {
//Check to see if there is already a sub page with that post name
$child_pages = get_pages(array( 'child_of' => $post_ID ));
$child_page_exists = false;
foreach($child_pages as $pages) {
if ($pages->post_title === $key) {
$child_page_exists = true;
}
}
//If not, set up the creation of the new post
if ($child_page_exists === false) {
$new_page_title = esc_html__( $key );
$new_page_content = '';
$new_page = array(
'post_type' => 'page',
'post_date' => esc_attr( date('Y-m-d H:i:s', time()) ),
'post_date_gmt' => esc_attr( date('Y-m-d H:i:s', time()) ),
'post_title' => esc_attr( $new_page_title ),
'post_name' => sanitize_title( $new_page_title ), //This could from the sub_field in the repeater
'post_content' => $new_page_content,
'post_status' => 'publish',
'post_parent' => $post_ID,
'menu_order' => $new_page_order
);
$new_page_id = wp_insert_post( $new_page );
update_post_meta( $new_page_id, '_wp_page_template', $value );
}
}
}
Again, this is just a spitball since there was not much code to review, but it could help you get going in the right direction.
I have a gravity form utilizing an add-on upload field that post-submission does some background work to send a video file to YouTube, then return the YouTube video ID.
I am trying to set a transient value on gform submission that stores the post ID, so another hook related to this YouTube upload process can take the YouTube ID it gets, then update a custom field for the aforementioned post.
Setting the transient works and I can see it in wp_options, but when I try to get the transient, it continually comes back empty despite the key values being identical.
function action_prso_gform_youtube_uploader_pre_update_meta( $field_values, $this_data ) {
//vars
$gforms_entry_title = strtolower(preg_replace('/-+/', '-', preg_replace('/[^\wáéíóú]/', '-', $this_data['gforms_entry'][1])));
//Get new post id so we can update it's meta
$transient_post_id = NULL;
$key = 'adv_' . $gforms_entry_title;
$transient_post_id = get_transient($key);
//You have the post id, save the youtube id as post meta
$yt_video_id = $field_values[6][0]['video_id'];
update_post_meta( $transient_post_id, 'video_testimonial_youtube_data', $yt_video_id );
}
add_action( 'prso_gform_youtube_uploader_pre_update_meta', 'action_prso_gform_youtube_uploader_pre_update_meta', 10, 2 );
add_action("gform_after_submission_19", "submit_video_testimonial_from_submission", 5, 2);
function submit_video_testimonial_from_submission($entry, $form) {
// Create a new post under the "video-testimonial" post-type
$new_video_testimonal = array(
'post_title' => ucwords($entry[1]),
'post_status' => 'draft',
'post_date' => date('Y-m-d H:i:s'),
'post_type' => 'video-testimonial'
);
$gforms_entry_title = strtolower(preg_replace('/-+/', '-', preg_replace('/[^\wáéíóú]/', '-', $entry[1])));
$theId = wp_insert_post($new_video_testimonal);
// Now we add the meta to the custom fields
$thePrefix = 'video_testimonial_';
update_post_meta($theId, $thePrefix.'title', ucwords($entry[1]));
update_post_meta($theId, $thePrefix.'description', ucwords($entry[2]));
update_post_meta($theId, $thePrefix.'name', ucwords($entry[3]));
update_post_meta($theId, $thePrefix.'location', ucwords($entry[5]));
// Set a transient to pass post ID for YouTube api function
set_transient('adv_'.$gforms_entry_title, $theId, 500);
}
I've set error_log on every field, including $transient_post_id and; it records nothing.
audio_list(get_the_content())
function audio_list($data){
$content = strip_tags($data);
$audiolist = explode("[/audio]", $content);
$audiolist = array_filter($audiolist);
return $audiolist;
}
Right now am using a custom function that strip the audio url and split the file name from the url and showing the real file name as the name of the audio in my site. I need to change the real name as media title. How can i do this ?? In word press embedded player media id is not available. In bulk upload case media is not associated with any posts so parent post attachment concept also not working. Do have any other method to get the media title ??
Get the uploaded audio files as an array first
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'audio',
'numberposts' => -1
);
$audiofiles = get_posts($args);
create a if condition that matching the file url's
foreach ($audiofiles as $file)
{
$urls = wp_get_attachment_url($file->ID);
if($urls == 'url from the content'){
$title = $file->post_title;
}else {
$title = 'file real name';
}
}
I'm developing a social network with Buddypress, I created a RSS plugin to pull the RSS feed from the specified websites.
Everything is working, except when the RSS is posted to the activity stream. When I create the activity content to print a link, I set the link target to "_new" to open it in a new page.
Here's the code:
function wprss_add_to_activity_feed($item, $inserted_ID) {
$permalink = $item->get_permalink();
$title = $item->get_title();
$admin = get_user_by('login', 'admin');
# Generates the link
$activity_action = sprintf( __( '%s published a new RSS link: %s - ', 'buddypress'), bp_core_get_userlink( $admin->ID ), '' . attribute_escape( wprss_limit_rss_title_chars($title) ) . '');
/* Record this in activity streams */
bp_activity_add( array(
'user_id' => $admin->ID,
'item_id' => $inserted_ID,
'action' => $activity_action,
'component' => 'rss',
'primary_link' => $permalink,
'type' => 'activity_update',
'hide_sitewide' => false
));
}
It should come up with something like that:
Test
But it prints like that:
Test
Why is this happening?
The 'target' attribute is probably getting stripped by BuddyPress's implementation of the kses filters. You can whitelist the attribute as follows:
function se16329156_whitelist_target_in_activity_action( $allowedtags ) {
$allowedtags['a']['target'] = array();
return $allowedtags;
}
add_filter( 'bp_activity_allowed_tags', 'se16329156_whitelist_target_in_activity_action' );
This probably won't retroactively fix the issue for existing activity items - it's likely that they had the offending attribute stripped before being stored in the database. But it should help for future items.
I'm programmatically uploading images to my Wordpress media gallery and setting them as my post's featured image, which is working quite well.
I'd like to generate thumbnails of these featured images, in the same way that Wordpress handles creating resized thumbnails when an image is added through the Insert Media pane, where it creates multiple files, each with the new dimensions specified in the file name.
Unfortunately, I'm having a lot of trouble locating anything that explains how this would be done properly.
Below is the relevant code. It successfully creates the post and sets the featured image to the media file, but doesn't generate thumbnails to go with it.
Any help would be greatly appreciated!
// If the post was created
if($post_id) {
// Add meta/custom field data
add_post_meta($post_id, 'gif_random_id', $randomId);
// Add uploaded file to the actual Wordpress image library
global $uploadURL;
$filename = $uploadURL . $randomId . '.' . $fileExtension;
$wp_filetype = wp_check_filetype(basename($filename), null);
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'publish'
);
$attach_id = wp_insert_attachment($attachment, $filename, $post_id);
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
// Now set the attachement as the featured image
update_post_meta($post_id, '_thumbnail_id', $attach_id);
}
You should use another - rather unknown and misunderstood function- media_sideload_image()
$attach_id = media_sideload_image($filename, $post_id);
This function will handle the "upload" process and will follow the normal workflow of uploading images to wordpress which includes thumbs generation as defined in your custom image sizes
Kaput, your code is perfectly fine, but seems your initial image is not in the uploads folder. See what the documentation says:
Location of the file on the server. Use absolute path and not the URI
of the file. The file MUST be on the uploads directory. See wp_upload_dir().
Just make sure you upload the image in the uploads directory first.