Wordpress, create json file when a term is updated/deleted/created - wordpress

I have to create a json file when a term is created/updated/deleted from a taxonomy "product_cat", below i attempt to register a simple json file but without success :
add_action( 'create_term', 'wpse_create_term', 10, 3 );
add_action( 'created_term', 'wpse_created_term', 10, 3 );
add_action( 'edited_term', 'wpse_edited_term', 10, 3 );
add_action( 'delete_term', 'wpse_delete_term', 10, 5 );
function wpse_create_term(){
createJsonFile();
}
function wpse_created_term(){
createJsonFile();
}
function wpse_edited_term(){
createJsonFile();
}
function wpse_delete_term(){
createJsonFile();
}
function createJsonFile(){
echo "begin write to json file to " . dirname(__FILE__);
$data = array("a","b","c","d");
//format the data
$formattedData = json_encode($data);
//set the filename
$filename = 'members.json';
//open or create the file
$handle = fopen($filename,'w+');
//write the data into the file
fwrite($handle,$formattedData);
//close the file
fclose($handle);
}
Firstly, why those hooks never fired ( the echo is not printed when updating a term ) ? and how should i specify destination folder for the final json file?
Thanks for your help.

You cant echo something from the function file you might want to use the debug.log for it
Secondly you need to specify a location where to create the json file

Related

Get parameter from url and pass it in add_filter not returning dynamic value wordpress

I am facing a weird problem. I am trying to override value in a function using apply_filter.
Here is the code which I am using in the theme's functions.php file:
function customized_params_6e9668( $attrs ) {
$subfolder = isset($_GET["sub"]) ? '/'.$_GET["sub"] : '';
$attrs["folder"] = $attrs["folder"].$subfolder; // if getting the value from the url then not working in this case
//$attrs["folder"] = $attrs["folder"]."/testing"; // if I use static name then working in this case
echo $attrs["folder"];
return $attrs;
}
add_filter( "customized_params_6e9668", "customized_params_6e9668");
Here is the function where I am using apply_filter to override the values in the plugin's file.
function getFolderData(){
global $wpdb;
$folder_data = $wpdb->get_row(
$wpdb->prepare(
'SELECT * FROM ' . $wpdb->prefix . 'folders WHERE key=%s',
trim(sanitize_text_field($_REQUEST["data_key"]))
)
);
if(!empty($folder_data)){
$folder_data = apply_filters( 'customized_params_'.$folder_data->key, $folder_data );
print_r($folder_data);
}
}
This function is getting the list of data from the database. overriding the value of the folder using add_filter.
Please correct me where I am doing wrong.
Thanks in advance.

Use ACF load_value hook to pre-populate the Upload File field

I'm trying to pre-populate the File upload field with files already submitted previously, using ACF PRO and ACF Frontend Form.
So far, I am able to retrieve a list of files that was previously uploaded but can't seem to figure out a way to set it in the file upload frontend.
The File upload field lives inside a Repeater group.
In the functions.php:
I added a load_value with key set to the repeater group:
add_filter('acf/load_value/key=field_5e80ed8f4516b', 'my_acf_set_file_list', 10, 3);
Then in the function I attempted to set the $value to be an array of file urls.
'field_5e80edf14516c' is the key for the File field.
function my_acf_set_file_list( $value, $post_id, $field ){
$order_id = $_GET['order'];
$value = array();
// get existing files
$existing_files = get_post_meta( $order_id, 'file', true);
if ( !empty( $existing_files ) ) {
$i = 0;
foreach ( $existing_files as $file ) {
$value[$i]['field_5e80edf14516c'] = $file;
// also tried
// $value[$i]['field_5e80edf14516c'] = $file['file'];
}
}
return $value;
}
The problem is in the frontend form, there seems to be no change at all. No file was pre-populated.
I'm pretty sure I didn't get the array structure correct, but I've searched all over and cannot find any related information.
The post data for when I upload existing files seem to indicate the attachment ID is used. But I'm not sure what is the row-0 and other id 5ebf042bba99f?
acf[field_5e80ed8f4516b]:
acf[field_5e80ed8f4516b][row-0][field_5e80edf14516c]: 381
acf[field_5e80ed8f4516b][5ebf042bba99f][field_5e80edf14516c]: 381
acf[field_5e80ed8f4516b][5ebf042cba9a0][field_5e80edf14516c]: 401
Thanks in advance!

Generate Contact File (.vcf) from WordPress Post Data?

After creating new post I need to generate and store the .vcf file in storage. I don't know how I can do this after post save. Didn't find help to develop such function. Please help!
Try this :
On "save_post" hook you can write function. That will create .vcf file with the name of post_title at specified directory.
function my_project_create_vCard( $post_id ) {
$vpost = get_post($post->ID);
$filename = strtolower(str_replace(" ","-",$vpost->post_title)).".vcf";
header('Content-type: text/x-vcard; charset=utf-8');
header("Content-Disposition: attachment; filename=".$filename);
$data=null;
$data.="BEGIN:VCARD\n";
$data.="VERSION:2.1\n";
$data.="FN:".$vpost->post_title."\n"; // get post title
$data.="EMAIL:" .get_field('email',$vpost->ID)."\n"; // get acf field value
$data.="END:VCARD";
$filePath = get_template_directory()."/".$filename; // you can specify path here where you want to store file.
$file = fopen($filePath,"w");
fwrite($file,$data);
fclose($file);
}
add_action( 'save_post', 'my_project_create_vCard' );

Get file duration uploaded from custom field wordpress

I want to get the duration of audio file when uploaded through custom field and save it in the post meta.
WordPress has built in audio functions using the ID3 library that will help you achieve this.
First you will hook into ACF using the acf/save_post hook. Then you will use the WP function wp_read_audio_metadata() to get the meta data of the audio file. Lastly you will use the update_post_meta() function to save the data to the post. Something like this:
function save_audio_duration($post_id) {
// Get the WP Uploads Directory (where ACF saves files)
$uploads = wp_upload_dir();
$uploads_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
// Get the file name from ACF & create the file string
$file_obj = get_field('audio_file', $post_id);
$file = $uploads_dir . '/' . $file_obj['filename'];
// Use the wp_read_audio_metadata() function to get data
$metadata = wp_read_audio_metadata( $file );
// Save the file length to the post meta
update_post_meta($post_id, 'audio_length', $metadata['length']);
}
// Will execute AFTER post has been saved (change "20" to "1" to execute before)
add_action('acf/save_post', 'save_audio_duration', 20);
Note: $metadata['length'] will return the time in seconds while $metadata['length_formatted'] will return the time in a formatted string.
Note x2: If you change the "20" to "1" in the action to execute this BEFORE the fields are saved to the post you will need to change the get_field() function to $_POST['audio_file'] as the function will be executed before ACF saves the fields to the DB.
I change a little your good code for vĂ­deos:
function save_video_duration($post_id) {
// Get the file name from ACF & create the file string
$file_obj = get_field('video_file', $post_id);
// Get the WP Uploads Directory (where ACF saves files)
$file = get_attached_file( attachment_url_to_postid( get_field('video_file', $post_id) ) );
// Use the wp_read_audio_metadata() function to get data
$metadata = wp_read_video_metadata($file);
// Save the file length to the post meta
update_post_meta($post_id, 'video_file_length', $metadata['length']);
}
// Will execute AFTER post has been saved (change "20" to "1" to execute before)
add_action('acf/save_post', 'save_video_duration', 20);
Thanks

File path without domain name from wp_get_attachment_url()

wp_get_attachment_url() process full file path like
http://example.com/wp-content/uploads/2014/12/aura.mp3
I want the url without http://example.com/
So, I want above example as wp-content/uploads/2014/12/aura.mp3 instead of http://example.com/wp-content/uploads/2014/12/aura.mp3. How to do it?
You can really easily explode it by / and then take the part with index 3. Example
$url = wp_get_attachment_url(id); //id is file's id
$urllocal = explode(site_url(), $url)[1]; //output local path
Here is the WordPress way using WordPress functions (avoid hacking):
$fullsize_path = get_attached_file( $attachment_id ); // Full path
$filename_only = basename( get_attached_file( $attachment_id ) ); // Just the file name
WordPress has tons of functions, so first try to find the function on the docs: https://developer.wordpress.org/reference/functions/get_attached_file/
You can use PHP's function explode.
Here is the code:
<?php
$image_url = wp_get_attachment_url( 9 ); //ID of your attachment
$my_image_url = explode('/',$image_url,4);
echo $my_image_url[3];
?>
You can implode your entire url on / and array_slice from the end, then implode it back in on /.
$url = wp_get_attachment_url($item->ID); //id is file's id
$url_relative = implode(array_slice(explode('/', $url),-3,3),'/');
//Returns: 2019/08/image.jpg
That way if your WordPress is on a subdomain or localhost or the images are on S3 it won't crash.

Resources