I am using the following filter to add some more Schema related information to my product.
But for some German character , it is converted to Numeric code
add_filter( 'woocommerce_structured_data_product', 'filter__woocommerce_structured_data_product', 20, 2 );
function filter__woocommerce_structured_data_product( $schema, $product ) {
global $post;
$shipping_val = utf8_decode( get_post_meta( $post->ID, 'pro_shipping', true ) );
// Not working : CHF 10.–
$colour_val = utf8_decode( get_post_meta( $post->ID, 'pro_color', true ) );
// Not working : Aussen: rosa und blau (metallisch), Bordüre: grün, Innen: lila
$schema['shipping'] = array(
'#type' => 'Shipping',
'name' => $shipping_val ,
);
$schema['colour'] = array(
'#type' => 'Colour',
'name' => $colour_val ,
);
return $schema;
}
I have added comment , in that form actual data are stored in custom fields.
and I have added screen-shot , which convert actual data in numeric form
How do I solve this ?
Not sure why you've run utf8_decode function on those values, but you could use iconv function to convert them back to utf8.
So when you run utf8_decode it will convert your values to ISO-8859-1 characters.
utf8_decode: Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1
We could use ISO-8859-1 in iconv function.
iconvDocs
For example, if you run the following code:
$german_word = "Bordüre: grün";
$utf8__decode_german_word = utf8_decode($german_word);
$utf8_german_word_main = iconv("ISO-8859-1", "utf-8//TRANSLIT//IGNORE", $utf8__decode_german_word);
echo $german_word . "<br>";
echo $utf8__decode_german_word . "<br>";
echo $utf8_german_word_main . "<br>";
Which will output this:
// $german_word result
Bordüre: grün
// $utf8__decode_german_word result
Bord�re: gr�n
// $utf8_german_word_main result
Bordüre: grün
So your entire code would be something like this:
add_filter("woocommerce_structured_data_product", "filter__woocommerce_structured_data_product", 20, 2);
function filter__woocommerce_structured_data_product($schema, $product)
{
global $post;
$shipping_val = utf8_decode(get_post_meta($post->ID, "pro_shipping", true));
$colour_val = utf8_decode(get_post_meta($post->ID, "pro_color", true));
$schema["shipping"] = array(
"#type" => "Shipping",
"name" => iconv("ISO-8859-1", "utf-8//TRANSLIT//IGNORE", $shipping_val),
);
$schema["colour"] = array(
"#type" => "Colour",
"name" => iconv("ISO-8859-1", "utf-8//TRANSLIT//IGNORE", $colour_val),
);
return $schema;
}
Let me know if you could get it to work.
Related
I'm creating a user and I want to add a metadata "Age" numeric.
The problem is that when I add it, it adds it as string
$user_id = wp_insert_user( array(
'user_login' => 'janedoe',
'user_pass' => 'passwordgoeshere',
));
add_user_meta($user_id, 'Age' , 20);
wp_set_current_user($user_id);
var_dump(get_user_meta( get_current_user_id(),"Age"));
This is the result
Array (1) {
[0] =>
String (2) "20"
}
Why do not you pick it up as int?
Thanks
add_user_meta( int $user_id, string $meta_key, mixed $meta_value, bool $unique = false )
$meta_value is mixed type to save all variable types, it doesn't know when the value is an integer or a string. So basically, it will convert/sanitize all variables to string.
You can find out more at https://developer.wordpress.org/reference/functions/add_user_meta/
Hyy
Try this,
$user_id = wp_insert_user( array(
'user_login' => 'janedoe',
'user_pass' => 'passwordgoeshere',
));
add_user_meta($user_id, 'Age' , 20);
wp_set_current_user($user_id);
$usertest = get_user_meta( get_current_user_id(),"Age", true);
$int = (int)$usertest;
var_dump($int);
I've got a question - is it possible to add one more column (which will present data from custom field) to csv export file in woocommerce? all necessary data is stored for each order as meta_key: 'authors_income'. I have no idea how to make it. I've tried to adapt below code, but it doesnt work:
function wpg_add_columns($cols) {
$cols['wc_settings_tab_payment_method'] = __('Payment Method', 'mytheme');
return $cols;
}
add_filter('wpg_order_columns', 'wpg_add_columns');
This should work. Let me know this goes.
<?php
//Step 1: Add field to column
function wpg_add_columns($cols) {
$cols['wc_settings_tab_authors_income'] = __('Authors Income', 'mytheme');
return $cols;
}
add_filter('wpg_order_columns', 'wpg_add_columns');
//Step 2: Add same column to settings, so that it will appear in settings page.
function wpg_add_fields($settings) {
$settings['authors_income'] = array(
'name' => __( 'Authors Income', 'woocommerce-simply-order-export' ),
'type' => 'checkbox',
'desc' => __( 'Authors Income', 'woocommerce-simply-order-export' ),
'id' => 'wc_settings_tab_authors_income'
);
return $settings;
}
add_filter('wc_settings_tab_order_export', 'wpg_add_fields');
//Note: Notice that in above step, id attribute is identical to that of array key in step 1. ( wc_settings_tab_authors_income )
//Step 3: Add it to csv.
function csv_write( &$csv, $od, $fields ) {
if( !empty( $fields['wc_settings_tab_authors_income'] ) && $fields['wc_settings_tab_authors_income'] === true ){
$authors_income = get_post_meta( $od->id, 'authors_income', true );
array_push( $csv, $authors_income );
}
}
add_action('wpg_add_values_to_csv', 'csv_write', 10, 3);
I want to upload file with Chinese character to a WordPress website. However, the name of the file is gibberish. I have searched the question and found some solutions. One is to modify the //wp-admin/includes/file.php
function wp_handle_upload( &$file, $overrides = false, $time = null ) {
//….
// Move the file to the uploads dir
//$new_file = $uploads['path'] . “/$filename”;
$new_file = $uploads['path'] . “/” . iconv(“UTF-8″,”GB2312″,$filename);
//…
//return apply_filters( ‘wp_handle_upload’, array( ‘file’ => $new_file, ‘url’ => $url, ‘type’ => $type ), ‘upload’ );
return apply_filters( ‘wp_handle_upload’, array( ‘file’ => $uploads['path'] . “/$filename”, ‘url’ => $url, ‘type’ => $type ) , ‘upload’);
}
As I know, it changes the encoding of the filename. But I want to put it in the function.php in the theme folder. What can I do ?
Problems with WordPress update_post_meta.
I have a function that should be updating _gravity_form_data. It works, but it is adding extra data for some reason. Here's the function:
Here is the function:
function wpufe_gravity_custom_video( $post_id ) {
if (isset( $_POST['custom_video'])) {
$custom_video = 'a:13:{s:2:"id";s:1:"2";s:13:"display_title";b:0;s:19:"display_description";b:0;s:25:"disable_woocommerce_price";s:2:"no";s:12:"price_before";s:0:"";s:11:"price_after";s:0:"";s:20:"disable_calculations";s:3:"yes";s:22:"disable_label_subtotal";s:2:"no";s:21:"disable_label_options";s:2:"no";s:19:"disable_label_total";s:2:"no";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";}';
update_post_meta( $post_id, '_gravity_form_data', $custom_video );
}
}
add_action( 'wpuf_add_post_after_insert', 'wpufe_gravity_custom_video' );
add_action( 'wpuf_edit_post_after_update', 'wpufe_gravity_custom_video' );
It SHOULD be making _gravity_form_data have this content:
a:13:{s:2:"id";s:1:"2";s:13:"display_title";b:0;s:19:"display_description";b:0;s:25:"disable_woocommerce_price";s:2:"no";s:12:"price_before";s:0:"";s:11:"price_after";s:0:"";s:20:"disable_calculations";s:3:"yes";s:22:"disable_label_subtotal";s:2:"no";s:21:"disable_label_options";s:2:"no";s:19:"disable_label_total";s:2:"no";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";}
Yet when it updates the _gravity_form_data field, its output is:
s:429:"a:13:{s:2:"id";s:1:"2";s:13:"display_title";b:0;s:19:"display_description";b:0;s:25:"disable_woocommerce_price";s:2:"no";s:12:"price_before";s:0:"";s:11:"price_after";s:0:"";s:20:"disable_calculations";s:3:"yes";s:22:"disable_label_subtotal";s:2:"no";s:21:"disable_label_options";s:2:"no";s:19:"disable_label_total";s:2:"no";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";}";
Why is it adding the s:429:" in the beginning and then the extra "; at the end? And most importantly, how do I fix it? :)
Thanks so much!
UPDATE: Was suggested to serialize the data, so I did:
function wpufe_gravity_custom_video( $post_id ) {
if (isset( $_POST['custom_video'])) {
$gubcustom_video = array('id' => 2, 'display_title' => '', 'display_description' => '', 'disable_woocommerce_price' => 'no', 'price_before' => '', 'price_after' => '', 'disable_calculations' => 'yes', 'disable_label_subtotal' => 'no', 'disable_label_option' => 'no', 'disable_label_total' => 'no', 'label_subtotal' => 'Subtotal', 'label_options' => 'Options', 'label_total' => 'Total');
$gubvideodata = serialize($gubcustom_video);
update_post_meta( $post_id, '_gravity_form_data', $gubvideodata );
}
}
add_action( 'wpuf_add_post_after_insert', 'wpufe_gravity_custom_video' );
add_action( 'wpuf_edit_post_after_update', 'wpufe_gravity_custom_video' );
...and still get the incorrect data posted to mysql.
Is there a way to just insert that string I need to insert? That's all I want to do, is update the database with that text.
I can do this in phpMySql easily, just copy and paste the text and it works like a champ. That's the solution I'm hoping for. Thanks again!
It looks like a serialized array that you are trying to insert manually but you should insert it by serializing it using serialize function, for example:
$custom_video = array('id' => 2, 'display_title' => '');
$data = serialize($custom_video);
update_post_meta( $post_id, '_gravity_form_data', $data );
Also, try using following hook:
add_action( 'save_post', 'save_meta_data' );
function save_meta_data($post_id)
{
$custom_video = array('id' => 2, 'display_title' => '');
$data = serialize($custom_video);
update_post_meta( $post_id, '_gravity_form_data', $data );
}
Problem solved. WP User Frontend Pro was setting the meta key with its own value of "1", causing it to re-serialize the serialized string. Problem solved by removing the WPUF meta key and just leaving the field.
I spent so much time to figure this out. How do automatically fill defaults values on the field or inserted into database upon installing the plugin.
I have tried these following codes but nothing works:
register_activation_hook(__FILE__, 'just_a_handler');
function just_a_handler($plugin_options) {
$defaults = array(
'youtube_keyword' => 'keyword here',
'youtube_author' => 'author here',
'youtube_content' => 'by_keyword',
'youtube_width' => '500',
'youtube_height' => '350',
'youtube_number_of_videos' => '5',
'youtube_preview' => '',
);
$plugin_options = wp_parse_args(get_option('youtube_plugin_options'), $defaults);
}
and this one:
register_activation_hook(__FILE__, 'just_a_handler');
function just_a_handler() {
add_option("youtube_keyword", 'keyword here', '', 'yes');
add_option("youtube_author", 'author here', '', 'yes');
}
To automatically fill an option with some defaults you can do something like the following. Depending on when you execute this code, I think it's a good idea to check that the option doesn't already exist before filling it with the default data. Also keep in mind that if you're storing an array, you need to serialize your data before adding it to the database. Databases can only store numbers, text, and dates. Serialization takes an array and turns it into a serialized string.
function init_options() {
$retrieved_options = array();
$defaults = array(
'youtube_keyword' => 'keyword here',
'youtube_author' => 'author here',
'youtube_content' => 'by_keyword',
'youtube_width' => '500',
'youtube_height' => '350',
'youtube_number_of_videos' => '5',
'youtube_preview' => '',
);
// Check to see if the option exists
$retrieved_options = maybe_unserialize( get_option( 'youtube_plugin_options' ) );
if ( $retrieved_options == '' ) {
// There are no options set
add_option( 'youtube_plugin_options', serialize( $defaults ) );
} elseif ( count( $retrieved_options ) == 0 ) {
// All options are blank
update_option( 'youtube_plugin_options', serialize( $defaults ) );
}
}
register_activation_hook( __FILE__, 'init_options' );