Preserving the user line breaks in a custom text area in woocommerce - function

I have managed to add a custom text area to the woocommerce product data where my client can add in a kit list that is output to the packing list for the shop floor but the line breaks don't save and the output is a flow of text making it less easy on the eye for the guys picking
This is what I have but where and how do I make it preserve the line breaks added in the admin suite to output on the packing slip:
//General Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields',10,3
);
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save',10,2 );
//variable data
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3);
add_action('woocommerce_save_product_variation','save_variation_settings_fields',10, 2);
//data tab
add_action( 'woocommerce_product_data_panels', 'add_my_custom_product_data_fields',10,3 );
add_action( 'woocommerce_process_product_meta','woocommerce_process_product_meta_fields_save',10,2 );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea[' . $post->ID . ']',
'label' => __( 'Kit List', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the kit list here.', 'woocommerce' ),
'value' => get_post_meta( $post->ID, '_textarea', true ),
)
);
}
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$textarea = $_POST['_textarea'][ $post_id ];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
}
}
function variation_settings_fields($loop, $variation_data, $variation){
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea[' . $variation->ID . ']',
'label' => __( 'Kit List', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the kit list here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea', true ),
)
);
}
function save_variation_settings_fields($post_id){
// Textarea
$textarea = $_POST['_textarea'][ $post_id ];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
}
}
add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_product_custom_field', 10, 3 );
function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) {
if ( $template_type == 'packing-slip' ) {
// check if product exists first
if (empty($item['product'])) return;
// replace 'Location' with your custom field name!
$field_name = '_textarea';
$textarea = method_exists($item['product'], 'get_meta') ? $item['product']
->get_meta($field_name,true,'edit') : get_post_meta( $item['product']->id, $field_name, true );
if (!empty($textarea)) {
echo nl2br('Kit List: '.$textarea.'');
}
}
}

That trouble in function wc_clean - it used to get value and always use _sanitize_text_fields( $str, false ), so regardless of field type, woo removes trailing characters :( See trace below:
In may situation (Need additional field in address), I found it in form edit address.
#/wp-content/plugins/woocommerce/templates/myaccount/form-edit-address.php:38
woocommerce_form_field( $key, $field, wc_get_post_data_by_key( $key, $field['value'] ) );
where wc_get_post_data_by_key function return cleaned data by wc_clean
#/wp-content/plugins/woocommerce/includes/wc-core-functions.php:2005
return wc_clean( wp_unslash( wc_get_var( $_POST[ $key ], $default ) ) ); // #codingStandardsIgnoreLine
that use satinize text
#/wp-content/plugins/woocommerce/includes/wc-formatting-functions.php:392
return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
where not keep_newlines set (by default it is false):
#WordPress/wp-includes/formatting.php:5244
$filtered = _sanitize_text_fields( $str, false );
Thus, there are no line breaks in all data of any type received by the function wc_get_post_data_by_key()
#WordPress/wp-includes/formatting.php:5317
if ( ! $keep_newlines ) {
$filtered = preg_replace( '/[\r\n\t ]+/', ' ', $filtered );
}
Solution (?)
the solution is cardinal - leave line breaks for all text fields similar to sanitize_textarea_field ():
add_filter( 'sanitize_text_field', function ( $filtered, $str ) {
return _sanitize_text_fields( $str, true );
}
, 10, 2 );

Related

Extract sub_array value from wordpress array in the database and save it back

I have data stored in a custom field in array as such
a:5:{i:0;a:1:{s:12:"product_desc";s:44:"Value1";}i:1;a:1:{s:12:"product_desc";s:24:"Value2";}i:2;a:1:{s:12:"product_desc";s:31:"Value3";}i:3;a:1:{s:12:"product_desc";s:41:"Value4";}i:4;a:1:{s:12:"product_desc";s:39:"Value5";}}
I want to extract the values of the sub_array product_desc and update the custom field meta_value.
I know wordpress have maybe_unserialize to unserialize the data but what to do next to extract the sub_array values?
add_action( 'init', function() {
if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
$query = new WP_Query( [
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'any',
] );
if ( ! $query->have_posts() ) {
return;
}
while ( $query->have_posts() ) {
$query->the_post();
$field_id_1 = 'podu';
$field_value_1 = get_post_meta( get_the_ID(), $field_id_1, true );
$data = maybe_unserialize( $field_value_1 );
//How to extract sub_array values from $data?
}
} );
your $data contains an array, so you can loop over it:
foreach($data as $singleData){
$desc[] = $singleData['product_desc'];
... now you have your $desc
}

Unserialize Custom Field & Save as Multiple Rows in Wordpress Database

I have a custom field in post_meta table of wordpress database. It's called 'combined'. I want to seperate the values based on key and save them as multiple rows if there are multiple values for the key.
My meta_key:
Result after I run my code:
What I want:
My Code:
add_action( 'init', function() {
if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
$query = new WP_Query( [
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'any',
] );
if ( ! $query->have_posts() ) {
return;
}
while ( $query->have_posts() ) {
$query->the_post();
$data = get_post_meta( get_the_ID(), 'combined', true );
foreach($data as $singleData => $value ){
update_post_meta( get_the_ID(), $singleData, $value );
}
}
} );
I have found the way to get the values as I want them to be in the database. Changing the loop part in the above code with code did the trick.
$data = get_post_meta( get_the_ID(), 'combined', true );
foreach($data as $singleData => $value ){
$thisArray = $data[$singleData];
foreach ($thisArray as $key2 => $value){
add_post_meta( get_the_ID(), $singleData, $thisArray[$key2], $unique = false);
}
}

Custom Wordpress menu replaces current header menu

I'm trying to add an additional custom menu to a Wordpress theme. I've registered the menu and location in the functions.php file, created a new page template and added the menu. However whenever I add items to the menu it replaces everything in the main header but also adds it to where I need it to be.
Any help would be greatly appreciated.
Thanks
Jack
Below is my functions.php file. The menu I am trying to add is right
at the bottom called about-menu
<?php
function cc_mime_types( $mimes ){
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );
?>
<?php add_theme_support( 'post-thumbnails' ); ?>
<?php
add_filter('next_posts_link_attributes', 'posts_link_next');
add_filter('previous_posts_link_attributes', 'posts_link_prev');
function posts_link_next() {
return 'class="next-button"';
}
function posts_link_prev() {
return 'class="prev-button"';
}; ?>
<?php add_filter('show_admin_bar', '__return_false'); ?>
<?php
/**
* Register our sidebars and widgetized areas.
*
*/
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'index-sidebar',
'id' => 'index-sidebar-widget',
'before_widget' => '<div class="sidebar-widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widget-sidebar">',
'after_title' => '</h2>',
) );
register_sidebar( array(
'name' => 'announcement',
'id' => 'announcement',
'before_widget' => '<div class="announcement">',
'after_widget' => '</div>',
'before_title' => '<h2 class="announcement-title">',
'after_title' => '</h2>',
) );
register_sidebar( array(
'name' => 'tweets',
'id' => 'tweets',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
register_sidebar( array(
'name' => 'events',
'id' => 'widget-events',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
/*
* Alters event's archive titles
*/
function tribe_alter_event_archive_titles ( $original_recipe_title, $depth ) {
// Modify the titles here
// Some of these include %1$s and %2$s, these will be replaced with relevant dates
$title_upcoming = 'Upcoming Events'; // List View: Upcoming events
$title_past = 'Past Events'; // List view: Past events
$title_range = 'Events for %1$s - %2$s'; // List view: range of dates being viewed
$title_month = 'Events for %1$s'; // Month View, %1$s = the name of the month
$title_day = 'Events for %1$s'; // Day View, %1$s = the day
$title_all = 'All events for %s'; // showing all recurrences of an event, %s = event title
$title_week = 'Events for week of %s'; // Week view
// Don't modify anything below this unless you know what it does
global $wp_query;
$tribe_ecp = Tribe__Events__Main::instance();
$date_format = apply_filters( 'tribe_events_pro_page_title_date_format', tribe_get_date_format( true ) );
// Default Title
$title = $title_upcoming;
// If there's a date selected in the tribe bar, show the date range of the currently showing events
if ( isset( $_REQUEST['tribe-bar-date'] ) && $wp_query->have_posts() ) {
if ( $wp_query->get( 'paged' ) > 1 ) {
// if we're on page 1, show the selected tribe-bar-date as the first date in the range
$first_event_date = tribe_get_start_date( $wp_query->posts[0], false );
} else {
//otherwise show the start date of the first event in the results
$first_event_date = tribe_event_format_date( $_REQUEST['tribe-bar-date'], false );
}
$last_event_date = tribe_get_end_date( $wp_query->posts[ count( $wp_query->posts ) - 1 ], false );
$title = sprintf( $title_range, $first_event_date, $last_event_date );
} elseif ( tribe_is_past() ) {
$title = $title_past;
}
// Month view title
if ( tribe_is_month() ) {
$title = sprintf(
$title_month,
date_i18n( tribe_get_option( 'monthAndYearFormat', 'F Y' ), strtotime( tribe_get_month_view_date() ) )
);
}
// Day view title
if ( tribe_is_day() ) {
$title = sprintf(
$title_day,
date_i18n( tribe_get_date_format( true ), strtotime( $wp_query->get( 'start_date' ) ) )
);
}
// All recurrences of an event
if ( function_exists('tribe_is_showing_all') && tribe_is_showing_all() ) {
$title = sprintf( $title_all, get_the_title() );
}
// Week view title
if ( function_exists('tribe_is_week') && tribe_is_week() ) {
$title = sprintf(
$title_week,
date_i18n( $date_format, strtotime( tribe_get_first_week_day( $wp_query->get( 'start_date' ) ) ) )
);
}
if ( is_tax( $tribe_ecp->get_event_taxonomy() ) && $depth ) {
$cat = get_queried_object();
$title = '' . $title . '';
$title .= ' › ' . $cat->name;
}
return $title;
}
add_filter( 'tribe_get_events_title', 'tribe_alter_event_archive_titles', 11, 2 );
?>
<?php
add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2);
function ad_filter_menu($sorted_menu_objects, $args) {
// check for the right menu to filter
// here we check for the menu with name 'Posts Menu'
// given that you call it in you theme with:
// wp_nav_menu( array( 'menu' => 'Posts Menu' ) );
// if you call the menu using theme_location, eg:
// wp_nav_menu( array( 'theme_location' => 'top_manu' ) );
// check for:
// if ($args->theme_location != 'top_menu')
if ($args->menu != 'Parents Menu')
return $sorted_menu_objects;
// edit the menu objects
foreach ($sorted_menu_objects as $menu_object) {
// searching for menu items linking to posts or pages
// can add as many post types to the array
if ( in_array($menu_object->object, array('post', 'page', 'any_post_type')) ) {
// set the title to the post_thumbnail if available
// thumbnail size is the second parameter of get_the_post_thumbnail()
$menu_object->title = has_post_thumbnail($menu_object->object_id) ? get_the_post_thumbnail($menu_object->object_id, 'full') . '<span class="outer"><span>' . $menu_object->title . "</span></span>": $menu_object->title;
}
}
return $sorted_menu_objects;
};
?>
<?php
function register_my_menu() {
register_nav_menu('about-menu',__( 'About Menu' ));
}
add_action( 'init', 'register_my_menu' );
?>
The next code snippet is from my page template that I want to add the
menu to.
<?php
/*
Template Name: About Us Page
*/
?>
<?php get_header();?>
<div class="main">
<div class="page-top-banner" id="page-four-banner">
<h1><span>About</span></h1>
</div>
<div class="parental-inner">
<?wp_nav_menu( array( 'theme_location' => 'about-menu', 'container_class' => 'about_menu_class' ) ); ?>
</div>
<?php get_footer();?>
Remove your register_my_menu function from functions php, and replace with the following:
Remove this:
function register_my_menu() {
register_nav_menu('about-menu',__( 'About Menu' ));
}
add_action( 'init', 'register_my_menu' );
Replace with this:
//nav
register_nav_menus(array(
'main-menu' => __('Main Menu'),
'about-menu' => __('About Menu'),
));
Does that output correctly on the page template?

How to use fontawesome in param visual composer?

I code element with visual composer. I want to use fontawesome in element.
Code here show list font-awesome in param visual composer
<code>
array(
'type' => 'iconpicker',
'heading' => esc_html__('Fontawesome', 'interior'),
'param_name' => 'fontawesome_icon',
'settings' => array(
'type' => 'fontawesome'
),
'description' => esc_html__( 'Fontawesome list. Pickup your choice.', 'interior'
),
'dependency' => array(
'element' => 'icon_type',
'value' => array( 'fontawesome-icon' )
)
</code>
I showed list icon in element but i chosen it don't saved and i don't know get value font-awsome display html.
Help me!!!
I have made a code only for Font Awesome Icon... You can try this
below code. it's tested and definitely works. here I have used
"Facebook" in title...Let me know via comment if you found any issue
in code or having any trouble for this.
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if( !class_exists( 'RN_FA_Icon_list' ) ) {
class RN_FA_Icon_list {
private $shortcode;
function __construct() {
/* shortcode base */
$this->shortcode = 'rn_fa_icon_list';
add_action( 'init', array( $this, 'rn_map_shortcode' ) );
add_shortcode( $this->shortcode, array( $this, 'rn_create_shortcode' ) );
}
function rn_map_shortcode( $atts, $content = NULL ) {
if( function_exists( 'vc_map' ) ) {
vc_map(
array(
'name' => esc_html__( 'Font Awesome Icon', 'rn_shortcodes' ),
//'icon' => RN_SHORTCODES_URL . '/admin/img/vc_icons/fancy-list.png',
'base' => $this->shortcode,
'category' => 'Structual',
'class' => 'rn-vc-icon-module rn-structual-module',
'content_element' => true,
'params' => array(
array(
'type' => 'textfield',
'heading' => esc_html__( 'Description', 'rn_shortcodes' ),
'description' => esc_html__( 'Only for internal use. This adds a label to Visual Composer for an easier element identification.', 'rn_shortcodes' ),
'param_name' => 'list_description',
'admin_label' => true,
'group' => 'General'
),
array(
'type' => 'iconpicker',
'heading' => __( 'Icon', 'js_composer' ),
'param_name' => 'icon_fontawesome',
'value' => 'fa fa-adjust',
'group' => 'General',
'settings' => array(
'emptyIcon' => false,
'type' => 'fontawesome',
'iconsPerPage' => 4000,
),
'dependency' => array(
'element' => 'type',
'value' => 'fontawesome',
),
'description' => __( 'Select icon from library.', 'js_composer' ),
),
array(
'type' => 'css_editor',
'param_name' => 'css',
'group' => esc_html__( 'Design Options', 'rn_shortcodes' ),
),
)
)
); /* end mapping */
}
}
function rn_create_shortcode( $atts, $content = NULL ) {
extract( shortcode_atts( array (
'icon_fontawesome' => '',
'css' => ''
), $atts ) );
/* extract list items */
if( function_exists('vc_param_group_parse_atts') && !empty( $values ) ) {
$values = vc_param_group_parse_atts( $values );
}
/* unique listz ID */
$id = uniqid("rn_fa_");
$output = '';
$output .= '<div id="' . esc_attr( $id ) . '">';
$output .= '<i class="' . $icon_fontawesome . '"></i> Facebook';
$output .= '</div>';
return '<div class="wpb_content_element ' . apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, vc_shortcode_custom_css_class( $css, ' ' ), $this->shortcode, $atts ) . '">' . $output . '</div>';
}
}
}
new RN_FA_Icon_list;
?>

Save an object in wordpress database

it is possibe to save an object in prefix_options in wordpress database like this one:
$arr_params = array( 'cat' => $display_category, 'product' => $single_post_ID );
Thanks
edit:
after make some changes, the code can't add an new array in exsisting array in the database:
$item= array(
'name' => $name ,
'prename' => $prename
);
print_r($item);
$options = get_option( 'options' );
if (empty($options['items'])) {
$options['items']=array();
add_option( 'options', $options );
$options = get_option( 'options' );
$options['items'] = array_push($options['items'], "$item");
update_option( 'options', $options );
}
else{
$options = get_option( 'options' );
$options['items'] = array_push($options['items'], "$item");
update_option( 'options', $options );
}
yes you can,
$arr_params = array( 'cat' => $display_category, 'product' => $single_post_ID );
if( get_option("_arr_params") === false ) {
add_option("_arr_params", $arr_params);
}
else {
// holds : array( 'cat' => $display_category, 'product' => $single_post_ID );
$my_param = get_option("_arr_params");
}
According to Edit Part: array_push() on works to add one or more elements not the array, you can use array_merge() in place of it, or second option is i already used in below codes.
$options['wphyper_orders'][] = $order_detail;
helpful link : get_option()