Custom wysiwyg textarea - html text format - wordpress

I created a custom field. Saving to db is OK. But when I save a page, the text is not saved as formatted but with html tags.
function sidebar_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function sidebar_add_meta_box() {
add_meta_box(
'sidebar-sidebar',
__( 'sidebar', 'sidebar' ),
'sidebar_html',
'page',
'advanced',
'default'
);
}
add_action( 'add_meta_boxes', 'sidebar_add_meta_box' );
function sidebar_html( $post) {
wp_nonce_field( '_sidebar_nonce', 'sidebar_nonce' ); ?>
<p>bočný stlpec ak by bolo treba</p>
<?php
$content = sidebar_get_meta( 'sidebar_sidebar' );
$editor_id = 'sidebar_sidebar';
$settings = array(
'media_buttons' => true,
'quicktags' => true,
'tinymce' => true,
'textarea_rows' => 5
);
wp_editor( $content, $editor_id, $settings );
}
function sidebar_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['sidebar_nonce'] ) || ! wp_verify_nonce( $_POST['sidebar_nonce'], '_sidebar_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( isset( $_POST['sidebar_sidebar'] ) )
update_post_meta( $post_id, 'sidebar_sidebar', esc_attr( $_POST['sidebar_sidebar'] ) );
}
add_action( 'save_post', 'sidebar_save' );
When I save the content, it displays it as follows:
<strong>Lorem ipsum</strong> dolor sit amet...
It should display it without html tags. Lorem ipsum must be bold.

You can use following code
function sidebar_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function sidebar_add_meta_box() {
add_meta_box(
'sidebar-sidebar',
__( 'sidebar', 'sidebar' ),
'sidebar_html',
'page',
'advanced',
'default'
);
}
add_action( 'add_meta_boxes', 'sidebar_add_meta_box' );
function sidebar_html( $post) {
wp_nonce_field( '_sidebar_nonce', 'sidebar_nonce' ); ?>
<p>bočný stlpec ak by bolo treba</p>
<?php
$content = sidebar_get_meta( 'sidebar_sidebar' );
$editor_id = 'sidebar_sidebar';
$settings = array(
'media_buttons' => true,
'quicktags' => true,
'tinymce' => true,
'textarea_rows' => 5
);
wp_editor( $content, $editor_id, $settings );
}
function sidebar_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['sidebar_nonce'] ) || ! wp_verify_nonce( $_POST['sidebar_nonce'], '_sidebar_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( isset( $_POST['sidebar_sidebar'] ) )
update_post_meta( $post_id, 'sidebar_sidebar', stripslashes( $_POST['sidebar_sidebar'] ) );
}
add_action( 'save_post', 'sidebar_save' );

Related

Woocommerce - add field to Check pay method

I'd like to add a field on Check Payment that allows the order processor to insert a check number...please help! Check payment orders will only be entered by staff, not the public.
Add Field Here
I've searched all over and I'm stumped.
As per my understanding of your question, I have created this snippet which will help you with your requirement.
// add a field on the check payment type
add_filter( 'woocommerce_gateway_description', 'custom_check_payment_field', 10, 2 );
function custom_check_payment_field( $description, $payment_id ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$current_user_role = (array) $user->roles;
if ( 'cheque' === $payment_id && $current_user_role[0] == 'administrator') {
ob_start();
echo '<div class="cheque-field">';
woocommerce_form_field(
'customer_cheque_number',
array(
'type' => 'text',
'label' => __( 'Enter cheque number', 'woocommerce' ),
'class' => array( 'form-row-wide' ),
'required' => true,
),
''
);
echo '<div>';
$description .= ob_get_clean();
}
}
return $description;
}
// save check number in meta/database
add_action( 'woocommerce_checkout_create_order', 'save_customer_cheque_number_order_meta', 10, 2 );
function save_customer_cheque_number_order_meta( $order, $data ) {
if ( isset( $_POST['customer_cheque_number'] ) && ! empty( $_POST['customer_cheque_number'] ) ) {
$order->update_meta_data( '_customer_cheque_number', esc_attr( $_POST['customer_cheque_number'] ) );
}
}
// show check number on orders page (OPTONAL)
add_action( 'woocommerce_get_order_item_totals', 'display_customer_cheque_number_on_order_totals', 10, 3 );
function display_customer_cheque_number_on_order_totals( $total_rows, $order, $tax_display ) {
if ( $order->get_payment_method() === 'cheque' && $customer_cheque_number = $order->get_meta( '_customer_cheque_number' ) ) {
$sorted_total_rows = array();
foreach ( $total_rows as $key_row => $total_row ) {
$sorted_total_rows[ $key_row ] = $total_row;
if ( $key_row === 'payment_method' ) {
$sorted_total_rows['customer_cheque_number'] = array(
'label' => __( 'Cheque number', 'woocommerce' ),
'value' => esc_html( $customer_cheque_number ),
);
}
}
$total_rows = $sorted_total_rows;
}
return $total_rows;
}
// add meta box on orders on woocommerce backend
add_action( 'add_meta_boxes', 'add_meta_boxes_callback' );
if ( ! function_exists( 'add_meta_boxes_callback' ) ) {
function add_meta_boxes_callback() {
add_meta_box( 'shop_order_fields', __( 'Cheque number', 'woocommerce' ), 'orders_cheque_number_custom_meta', 'shop_order', 'side', 'core' );
}
}
// show meta field with entered check number
if ( ! function_exists( 'orders_cheque_number_custom_meta' ) ) {
function orders_cheque_number_custom_meta() {
global $post;
$check_number = get_post_meta( $post->ID, '_customer_cheque_number', true ) ? get_post_meta( $post->ID, '_customer_cheque_number', true ) : '';
?>
<input type="text" style="width:250px;margin-top:15px" readonly name="readonly_check_number" value="<?php echo $check_number; ?>"></p>
<?php
}
}

How do i Replace function from wordpress plugins?

I am starting to study action and filter in wordpress. Lets say i wanna add DIV wrap to the output of printif at the bottom of code. Can i just copy all this code into my functions.php and edit some of its codes?
defined( 'YITH_WOOCOMPARE' ) || exit; // Exit if accessed directly.
if ( ! class_exists( 'YITH_Woocompare_Frontend' ) ) {
/**
* YITH Custom Login Frontend
*
* #since 1.0.0
*/
class YITH_Woocompare_Frontend {
public function __construct() {
// Add link or button in the products list.
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_product_page', 'yes' ) ) {
add_action( 'woocommerce_single_product_summary', array( $this, 'add_compare_link' ), 35 );
}
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_products_list', 'no' ) ) {
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_compare_link' ), 20 );
}
return $this;
}
public function add_compare_link( $product_id = false, $args = array() ) {
extract( $args ); // phpcs:ignore
if ( ! $product_id ) {
global $product;
$product_id = ! is_null( $product ) ? $product->get_id() : 0;
}
// Return if product doesn't exist.
if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) ) {
return;
}
$is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button', 'button' ) : $button_or_link;
if ( ! isset( $button_text ) || 'default' === $button_text ) {
$button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
do_action( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
$button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
}
printf( '%s', $this->add_product_url( $product_id ), 'compare' . ( 'button' === $is_button ? ' button' : '' ), $product_id, $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}

Add extra filed in custom post type

This is my code
// Our custom post type function
function create_posttype_ticket() {
register_post_type( 'ticket',
array(
'labels' => array(
'name' => __( 'Ticket' ),
'singular_name' => __( 'Ticket' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'thumbnail', 'author'),
'rewrite' => array('slug' => 'ticket'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype_ticket' );
I want add some more label/text box in custom post type
Movie : Release Date
Movie : Cast
any idea
Add the below code in function.php or in your plugin file.
This code will create some custom fields in your custom post type.
function movie_detail_meta_box(){
add_meta_box(
'movie_detail_box_id', // Unique ID
'Movie Details', // Box title
'render_movie_detail_meta_box_html',
'ticket', // Post type
'normal',
'low'
);
}
add_action('add_meta_boxes', 'movie_detail_meta_box');
function render_movie_detail_meta_box_html($post){
$meta = get_post_meta( $post->ID );
wp_nonce_field( 'movie_detail_metabox', 'movie_detail_metabox_nonce' );
$val_movie_name = ( isset( $meta['movie_name'][0] ) && '' !== $meta['movie_name'][0] ) ? $meta['movie_name'][0] : '';
$val_movie_release = ( isset( $meta['movie_release'][0] ) && '' !== $meta['movie_release'][0] ) ? $meta['movie_release'][0] : '';
$val_movie_cast = ( isset( $meta['movie_cast'][0] ) && '' !== $meta['movie_cast'][0] ) ? $meta['movie_cast'][0] : '';
// print_r("<p>val_autoplayTime-".$val_autoplayTime."<p/>");
echo "<div class='movie_meta_fields_container'>";
echo '<p><label for="movie_name"><b>Movie Name: </b></label><input type="text" name="movie_name" id="movie_name" style="width:100%;" value="'. esc_attr( $val_movie_name ) .'"></p>';
echo '<p><label for="movie_release"><b>Movie Release: </b></label><input type="text" name="movie_release" id="movie_release" style="width:100%;" value="'. esc_attr( $val_movie_release ) .'"></p>';
echo '<p><label for="movie_cast"><b>Movie Cast: </b></label><input type="text" name="movie_cast" id="movie_cast" style="width:100%;" value="'. esc_attr( $val_movie_cast ) .'"></p>';
echo "</div>";
}
function movie_detail_save_metadata($post_id){
// Check if our nonce is set.
if ( ! isset( $_POST['movie_detail_metabox_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['movie_detail_metabox_nonce'] ), 'movie_detail_metabox' ) ) { // Input var okay.
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// OK, it's safe for us to save the data now.
// Make sure that it is set.
if ( !isset($_POST['movie_name']) || !isset($_POST['movie_release']) || !isset($_POST['movie_cast']) ) {
return;
}
$fields = ['movie_name','movie_release','movie_cast'];
foreach ($fields as $field) {
if (array_key_exists($field, $_POST)){
update_post_meta($post_id, $field, sanitize_text_field($_POST[$field]));
}
}
}
add_action('save_post', 'movie_detail_save_metadata');
Feel free to ask, if you got stuck anywhere.

Can not save multiple product variation fields

When I save the first product variation fields they are saved, but when I try to save the second, third, forth and further they do not get saved.
I am filling out the SKU for every product variation as well, so it should work. But obv it is something I am missing here? This is the latest WordPress and latest WooCommerce version.
//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 3 );
//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 1 );
/**
* Create new fields for variations
*
*/
function variable_fields( $loop, $variation_data, $variation ) {
?>
<tr>
<td>
<?php
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea['.$loop.']',
'label' => __( 'Contains', 'woocommerce' ),
'placeholder' => '',
'description' => __( '<br />Contains', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea', true ),
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea_2['.$loop.']',
'label' => __( 'Observation message', 'woocommerce' ),
'placeholder' => '',
'description' => __( '<br />Observation message', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea_2', true ),
)
);
?>
</td>
</tr>
<?php
}
/**
* Save new fields for variations
*
*/
function save_variable_fields( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
// Textarea
$_textarea = $_POST['_textarea'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_textarea[$i] ) ) {
update_post_meta( $variation_id, '_textarea', stripslashes( $_textarea[$i] ) );
}
endfor;
// Textarea
$_textarea_2 = $_POST['_textarea_2'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_textarea_2[$i] ) ) {
update_post_meta( $variation_id, '_textarea_2', stripslashes( $_textarea_2[$i] ) );
}
endfor;
endif;
}
I've modified action & function for saving:
//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 2 );
/**
* Save new fields for variations
*
*/
function save_variable_fields( $variation_id, $i ) {
if ( empty( $variation_id ) ) return;
if ( isset( $_POST['_textarea'][$i] ) ) {
update_post_meta( $variation_id, '_textarea', stripslashes( $_POST['_textarea'][$i] ) );
}
if ( isset( $_POST['_textarea_2'][$i] ) ) {
update_post_meta( $variation_id, '_textarea_2', stripslashes( $_POST['_textarea_2'][$i] ) );
}
}

How to add custom filed and save it in wordpress?

Following code I tried out But it is not save value of custom Fileds.
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'product', 'side', 'high' );
}
In above I have add code it is display when posttype is product
function cd_meta_box_cb( $product)
{
$values = get_post_custom( $product->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : ”;
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
In above code it will added the metabox with textbox and if value in metabox then it is display
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
}
I want to save data in a wp_postmeta table. Above code i have tried out. I am beginner in wordpress.Can any give me suggestion ?
Use This code for creating custom fields for post.
class Rational_Meta_Box {
private $screens = array(
'post',
);
private $fields = array(
array(
'id' => 'custom-field-1',
'label' => 'custom field 1',
'type' => 'text',
),
array(
'id' => 'custom-field-2',
'label' => 'custom field 2',
'type' => 'text',
),
);
/**
* Class construct method. Adds actions to their respective WordPress hooks.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
}
/**
* Hooks into WordPress' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
add_meta_box(
'my-custom-fields',
__( 'my custom fields', 'wordpress' ),
array( $this, 'add_meta_box_callback' ),
$screen,
'advanced',
'high'
);
}
}
/**
* Generates the HTML for the meta box
*
* #param object $post WordPress post object
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( 'my_custom_fields_data', 'my_custom_fields_nonce' );
echo 'its for custom fields for post typle';
$this->generate_fields( $post );
}
/**
* Generates the field's HTML for the meta box.
*/
public function generate_fields( $post ) {
$output = '';
foreach ( $this->fields as $field ) {
$label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
$db_value = get_post_meta( $post->ID, 'my_custom_fields_' . $field['id'], true );
switch ( $field['type'] ) {
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s">',
$field['type'] !== 'color' ? 'class="regular-text"' : '',
$field['id'],
$field['id'],
$field['type'],
$db_value
);
}
$output .= $this->row_format( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}
/**
* Generates the HTML for table rows.
*/
public function row_format( $label, $input ) {
return sprintf(
'<tr><th scope="row">%s</th><td>%s</td></tr>',
$label,
$input
);
}
/**
* Hooks into WordPress' save_post function
*/
public function save_post( $post_id ) {
if ( ! isset( $_POST['my_custom_fields_nonce'] ) )
return $post_id;
$nonce = $_POST['my_custom_fields_nonce'];
if ( !wp_verify_nonce( $nonce, 'my_custom_fields_data' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->fields as $field ) {
if ( isset( $_POST[ $field['id'] ] ) ) {
switch ( $field['type'] ) {
case 'email':
$_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
break;
case 'text':
$_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
break;
}
update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], $_POST[ $field['id'] ] );
} else if ( $field['type'] === 'checkbox' ) {
update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], '0' );
}
}
}
}
new Rational_Meta_Box;
So it will create custom fields in your post type and also saved it too.
follow this for the references https://developer.wordpress.org/plugins/metadata/creating-custom-meta-boxes/
Just replace your add_action( 'save_post', 'cd_meta_box_save' ); function and put below code.
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
}
}
Full code like ( it's working fine and also save custom field in postmeta table)
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'side', 'high' );
}
function cd_meta_box_cb( $product)
{
$values = get_post_custom( $product->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : '';
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
}
}
Take a look at Pods.
It is a very powerful plugin for customization.
http://pods.io/

Resources