Wordpress media upload extra field - wordpress

How do I make another field available in the wordpress media panel?
Currently you have all your necesary fields, although I need to add one extra to capture some extra details.
I am using a flash player that looks for a set of fields, ideally I would like to a field called video_url
Any ideas?

I'm not sure how to add an extra field inside the media panel... but you could use Magic Field to create a media uploader and an extra field which will contain your extra details... Learn more about Magic Field Plugin here : http://wordpress.org/extend/plugins/magic-fields/

Came up with this solution.
function rt_image_attachment_fields_to_edit($form_fields, $post) {
// $form_fields is a an array of fields to include in the attachment form
// $post is nothing but attachment record in the database
// $post->post_type == 'attachment'
// attachments are considered as posts in WordPress. So value of post_type in wp_posts table will be attachment
// now add our custom field to the $form_fields array
// input type="text" name/id="attachments[$attachment->ID][custom1]"
$form_fields["rt-image-link"] = array(
"label" => __("Video URL"),
"input" => "text", // this is default if "input" is omitted
"value" => get_post_meta($post->ID, "_rt-image-link", true),
"helps" => __(""),
);
$form_fields["rt-video-link"] = array(
"label" => __("Library Ref"),
"input" => "text", // this is default if "input" is omitted
"value" => get_post_meta($post->ID, "_rt-video-link", true),
"helps" => __(""),
);
return $form_fields;
}
// now attach our function to the hook
add_filter("attachment_fields_to_edit", "rt_image_attachment_fields_to_edit", null, 2);
function rt_image_attachment_fields_to_save($post, $attachment) {
// $attachment part of the form $_POST ($_POST[attachments][postID])
// $post['post_type'] == 'attachment'
if( isset($attachment['rt-image-link']) ){
// update_post_meta(postID, meta_key, meta_value);
update_post_meta($post['ID'], '_rt-image-link', $attachment['rt-image-link']);
}
if( isset($attachment['rt-video-link']) ){
// update_post_meta(postID, meta_key, meta_value);
update_post_meta($post['ID'], '_rt-video-link', $attachment['rt-video-link']);
}
return $post;
}
// now attach our function to the hook.
add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);

Related

Wordpress plugin for course registration

I'm building a WordPress Page for course registration. All I want the plugin to do is send the filled in form details to my email ID and send an email to the user that he/she has successfully registered for the course. I don't need users to signup with username and password.
I've tried my luck with WP Forms but it only seems to have the option to forward the email to me and not the user.
Any suggestion on which plugin I should use?
As #Hughes mentioned, you cant use wpcf7, and just hook on it to insert custom post on every query.
// Hook on wpcf7
add_filter( 'wpcf7_mail_components', 'do_on_cf7_submit', 50, 2 );
function do_on_cf7_submit($mail_params, $form = null) {
// Empty post content
$content = '';
// set post content if field not empty
if ($_POST['field-name'] != '') {
$content .= 'Field Name Label: '.$_POST['field-name'] ;
}
// insert post if content not epmty
if ($content != '') {
insertQueryPost($_POST['email'], $content);
}
// allow cf7 to do his stuff
return $mail_params;
}
// insert custom post type "query", don't forget to setup your custom post type first
function insertQueryPost($title, $content) {
// insted of proper post slug, just make a hashed slug, when setting custom post type, set it to not public and not search-able
$t = time();
$thash = md5($t);
$my_query = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $content,
'post_type' => 'query',
'post_name' => $thash,
'post_status' => 'publish',
'post_author' => 1
);
$data = wp_insert_post( $my_query );
}

Post meta to custom field with multiple rows! - Wordpress

I'm using the houzez theme and trying to develop a plugin to post from an API to a "property" post. "Property" is a custom post and one custom field (additional_feature) has multiple rows.
When I get the value from additional_feature using get_post_meta() in a sample post with the fields already filled we get:
Array ( [0] => Array ( [0] => Array ( [fave_additional_feature_title] => aaa
[fave_additional_feature_value] => 234 ) [1] => Array (
[fave_additional_feature_title] => bbb [fave_additional_feature_value] => 567
) [2] => Array ( [fave_additional_feature_title] => ccc
[fave_additional_feature_value] => 890 ) ) )
So, the custom field additional_feature has some more fields inside!! When I use update_post_meta it doesn't work. I tried setting up an array to pass the data exactly as the sample array looks like when called trough get_post_meta(). However, it doesn't work.
Any idea of how to update additional_feature?
*To output repeater meta (Is used - ACF custom field repeater).
$meta = get_post_meta($post->ID); //Get all post meta per one request
$repeater_count = $meta["additional_feature"][0]; //Count of iteration
for ($i=0; $i<$repeater_count; $i++) {
$feature_title = 'additional_feature_'.$i.'_title';
echo $meta[$feature_title][0]; //Output repeater meta
}
*To update repeater meta (Use ACF custom field repeater). Update on save post.
You can change the code for your needs
!!!Generate the working code better on the test site, because with erroneous names of the user fields, when saving the post, new fields will be created in the database table - post_meta.
Add to functions.php
add_filter('acf/save_post', 'main_meta_filter', 20);
function main_meta_filter($post_id) {
if ( $post_id != 7 ) //You can use post type if you need
return;
//Repeater
$number_rows = get_post_meta( $post_id, "additional_feature" );//Count of iteration
for ($i=0; $i<$number_rows[0]; $i++) {
$key = 'additional_feature_'.$i.'_title';
//To get old value use $old_value = get_post_meta($post_id, $key, false);
//and output use $old_value[0]
$new_value = 1;//You custom value
update_post_meta($post_id, $key, $new_value);
}
}
Something weird happened. I re-tried a method that didn't work but I had no more ideas. I used the following code:
$data = array(
array(
'fave_additional_feature_title' => 'Ax',
'fave_additional_feature_value' => 111,
),
);
update_post_meta($postid, 'additional_features', $data);
It worked this time. Very weird and I dont know how it didn't work before and now it works. However, now I can update the custom field.

Custom product attributes is blank even though I add it [duplicate]

The products in my clients website require certain attributes which I have added via Products -> Attributes in the Wordpress administration. In this import script I'm coding I need to use the function update_post_meta($post_id, $meta_key, $meta_value) to import the proper attributes and values.
Currently I have the function like so:
update_post_meta( $post_id, '_product_attributes', array());
However I'm not sure how to properly pass along the attributes and their values?
Right so it took me a while to figure it out myself but I finally managed to do this by writing the following function:
// #param int $post_id - The id of the post that you are setting the attributes for
// #param array[] $attributes - This needs to be an array containing ALL your attributes so it can insert them in one go
function wcproduct_set_attributes($post_id, $attributes) {
$i = 0;
// Loop through the attributes array
foreach ($attributes as $name => $value) {
$product_attributes[$i] = array (
'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
'value' => $value, // set attribute value
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
);
$i++;
}
// Now update the post with its new attributes
update_post_meta($post_id, '_product_attributes', $product_attributes);
}
// Example on using this function
// The attribute parameter that you pass along must contain all attributes for your product in one go
// so that the wcproduct_set_attributes function can insert them into the correct meta field.
$my_product_attributes = array('hdd_size' => $product->hdd_size, 'ram_size' => $product->ram_size);
// After inserting post
wcproduct_set_attributes($post_id, $my_product_attributes);
// Woohay done!
I hope this function will help other people if they need to import multiple attributes pro-grammatically in WooCommerce!
I tried Daniel's answer, and it didn't work for me. It might be that the Wordpress/Woocommerce code has changed since, or perhaps I didn't quite understand how to do it, but either way that code did nothing for me. After a lot of work using it as a base, however, I came up with this snippet of code and put it on my theme's functions.php:
function wcproduct_set_attributes($id) {
$material = get_the_terms( $id, 'pa_material');
$material = $material[0]->name;
// Now update the post with its new attributes
update_post_meta($id, '_material', $material);
}
// After inserting post
add_action( 'save_post_product', 'wcproduct_set_attributes', 10);
With this, I can take what I set as "material" on my WooCommerce install as a custom attribute and add it to the formal meta as _material. This in turn allows me to use another snippet of code so the WooCommerce search function extends to meta fields, meaning I can search for a material in the WooCommerce search field and have all items with that material appear.
I hope this is useful to somebody.
#Daniels's answer works, won't decide on right or wrong, however if you want to add the values as a taxonomy term under attributes you have to adapt the code as below (set is_taxonomy = 1). Otherwise Woocommerce sees it as custom meta field(?). It still adds the value under attributes. This will only work for strings. For values that are arrays the code has to be adapted.
Additionally it uses the wp_set_object_terms that #Anand suggests as well. I was using that, because all the documentation I could find led to believe that had to be used. However if one only uses the wp_set_object_terms then I couldn't see the attributes in the edit product screen. Using the information from both answers and reading on the subject resulted in the solution.
You will need to tweak the code for things such as product variations.
/*
* Save Woocommerce custom attributes
*/
function save_wc_custom_attributes($post_id, $custom_attributes) {
$i = 0;
// Loop through the attributes array
foreach ($custom_attributes as $name => $value) {
// Relate post to a custom attribute, add term if it does not exist
wp_set_object_terms($post_id, $value, $name, true);
// Create product attributes array
$product_attributes[$i] = array(
'name' => $name, // set attribute name
'value' => $value, // set attribute value
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 1
);
$i++;
}
// Now update the post with its new attributes
update_post_meta($post_id, '_product_attributes', $product_attributes);
}
Then call the function:
$custom_attributes = array('pa_name_1' => $value_1, 'pa_name_2' => $value_2, 'pa_name_3' => $value_3);
save_wc_custom_attributes($post_id, $custom_attributes);
Thank you for posting the code Daniel & Anand. It helped me a great deal.
Don't know if this is the "correct" way to do this... But I needed a function to add ACF repeater fields with a date value as a attribute on post save, so this was the function I came up with:
add_action( 'save_post', 'ed_save_post_function', 10, 3 );
function ed_save_post_function( $post_ID, $post, $update ) {
//print_r($post);
if($post->post_type == 'product')
{
$dates = get_field('course_dates', $post->ID);
//print_r($dates);
if($dates)
{
$date_arr = array();
$val = '';
$i = 0;
foreach($dates as $d)
{
if($i > 0)
{
$val .= ' | '.date('d-m-Y', strtotime($d['date']));
}
else{
$val .= date('d-m-Y', strtotime($d['date']));
}
$i++;
}
$entry = array(
'course-dates' => array(
'name' => 'Course Dates',
'value' => $val,
'position' => '0',
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
)
);
update_post_meta($post->ID, '_product_attributes', $entry);
}
}
}
Hope this helps someone.

Wordpress Bulk Edit deletes custom taxonomy value

I have a custom taxonomy that I am "auto" assigning a value whenever a post is saved. It is saving the first letter of the post_title so I can use in a custom A to Z list (similar to this: http://geekgirllife.com/alphabetical-index-of-posts-in-wordpress/)
I have registered the taxonomy in my theme's functions.php as well as the auto assign function. Everything is working, when create/edit/save a post it adds the first letter of the post title into this custom tax.
However when I bulk edited some posts from the default WP bulk actions functionality, it overwrites the custom taxonomy value for the posts being edited.
My question is, is there a way to exclude a custom taxonomy from the bulk edit action in WP? Or a way bulk edit action will treat a custom tax as "no change" rather than update it w/ empty value?
Register Taxonomy code:
function atoz_tax() {
register_taxonomy( 'atoz',array (
0 => 'page',
),
array( 'hierarchical' => false,
'label' => 'atoz',
'show_ui' => false,
'query_var' => true,
'show_admin_column' => false,
) );
}
add_action('init', 'atoz_tax');
Code that auto saves to taxonomy on save:
function atoz_save_first_letter( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
//Check permissions
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
// find and save the data
$taxonomy = 'atoz';
//set term as first letter of post title, lower case
wp_set_object_terms($post_id,strtolower(substr($_POST['post_title'], 0, 1)), $taxonomy);
}
add_action( 'save_post', 'atoz_save_first_letter' );

Getting random path hierarchy for CCK fields inside form array

I have created a custom entity and i'm using CCK fields. Each bundle has it's own fields. For example:
function MYMODULE_install() {
// Check if our field is not already created.
if (!field_info_field('field_myField')) {
$field = array(
'field_name' => 'date_field',
'type' => 'list_text',
);
field_create_field($field);
}
//Enable is executed only once.
function bundle_callback_enable() {
// Create the instance on the bundle.
$instance = array(
'field_name' => 'date_field',
'entity_type' => 'payment_method',
'label' => 'Expiration Date',
'bundle' => 'card',
'required' => TRUE,
'settings' => array();
field_create_instance($instance);
}
My bundles are created from individual modules, so in each install file i am creating the respective fields.
Yesterday i tried to add validation callback functions in those fields and i saw something weird inside form array. Fields with type="text" had the path:
$form[field_name]['und'][0][value] //<! expectable
but fields with type='list_text' had only the path:
$form[field_name]['und'] //<! unexpectable
I couldn't find any solution and i've solved it with this:
function &get_cck_path_value( $field_name, &$form_path) {
$field = null
if ( isset( $form_path[$field_name][LANGUAGE_NONE] ) ) {
$field = &$form_path[$field_name][LANGUAGE_NONE]
}elseif(isset($form_path[$field_name][LANGUAGE_NONE][0])) {
$field = &$form_path[$field_name][LANGUAGE_NONE][0]['value'];
}
return $field;
}
I don't like this approach. Is too hucky. Can you tell me if that is a cck feature or bug?
I can't understand when it decides where to put the values( All the process is fulfilled through the "field_attach_form( ... )" )?
Have you faced any problem like this?
Thanks in advance.
Thandem.
I believe that you are seeing the abbreviated form field in validation because the field had no value entered into it and no default value was defined for it. There is no value, so no array is present to store the value.

Resources