Meta box only saving the first of multiple input fields - wordpress

To keep it short: I built a meta box with 3 fields, but it's only saving the first field. What could I be doing wrong?
// --- METABOX: CAREER ... CONTENTS --- //
function career_meta(){
global $post;
$career1 = get_post_meta( $post->ID, 'career1', true );
$career2 = get_post_meta( $post->ID, 'career2', true );
$career3 = get_post_meta( $post->ID, 'career3', true );
?>
<label for="career_subtitle">Subtitle</label><input type="text" class="widefat" id="career-subtitle" name="career_subtitle" value="<?php echo $career1; ?>" />
<label for="career_text_1">Left Column</label><textarea class="widefat" id="career-text-1" name="career_text_1" value="<?php echo $career2; ?>"></textarea>
<label for="career_text_2">Right Column</label><textarea class="widefat" id="career-text-2" name="career_text_2" value="<?php echo $career3; ?>"></textarea>
<?php }
// --- METABOX: CAREER ... SAVE --- //
add_action('save_post', 'save_career');
function save_career(){
global $post;
update_post_meta($post->ID, "career1", $_POST["career_subtitle"]);
update_post_meta($post->ID, "career2", $_POST["career_text_1"]);
update_post_meta($post->ID, "career3", $_POST["career_text_2"]);
}
add_action('save_post','function_save_var');
function function_save_var()
{
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
}
That's what I have right now. It saves the first field (career_subtitle) but not the 2 other ones. Fixing this is quite important so I would really appreciate an effective answer :)

This maynot be the solution to your question but are you sure that textarea has a value attribute????
This could solve your problem:-
<label for="career_text_1">Left Column</label><textarea class="widefat" id="career-text-1" name="career_text_1"><?php echo $career2; ?></textarea>
<label for="career_text_2">Right Column</label><textarea class="widefat" id="career-text-2" name="career_text_2"><?php echo $career3; ?></textarea>

Related

Entering detail information on the created order

I want to create a field where the customer can write the reason for canceling a created order. The code below appears in other orders as well, due to the customer's own knowledge. I made several attempts to register the order, but I could not get the result I wanted. I thought about "update_post_meta" and "order_id" but I guess I failed. Also, I couldn't decide where it would make sense for this area to appear on the admin page. Thanks.
// Display user custom field
add_action( 'woocommerce_order_details_before_order_table', 'add_user_custom_url_field_to_order' );
function add_user_custom_url_field_to_order( $order ) {
global $current_user;
$custom_url = get_user_meta( $current_user->ID, 'custom_URL', true );
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo $custom_url; ?>" />
</p>
<input type="submit" name="submit-custom_URL" value="<?php _e('RUN', 'woocommerce'); ?>" /><br/>
</form>
<?php
}
// Save the field as custom user data
add_action( 'template_redirect', 'save_user_custom_url_field_from_order' );
function save_user_custom_url_field_from_order() {
global $current_user;
if( isset($_POST['custom_URL']) ){
update_user_meta( $current_user->ID, 'custom_URL', sanitize_url( $_POST['custom_URL'] ) );
wc_add_notice( __("Submitted data has been saved", "woocommerce") );
}
}
Code source: https://stackoverflow.com/a/62777930/14597323

How to create WordPress widget using custom menu select

I'm creating a custom widget for WordPress but am wanting to use the same select menu as the default Custom Menu widget does. So far what I have works except the selected menu isn't being saved or displayed on the frontend. Any help or direction with this is appreciated. Thanks.
For brevity, I've removed extra widget options, so this is basically going to look very close to the Custom Menu widget:
class kedc_mini_sitemap extends WP_Widget {
/*constructor*/
function kedc_mini_sitemap() {
parent::WP_Widget(false, $name = 'Mini Sitemap');
}
/**/
function widget($args, $instance) {
extract( $args );
// widget options
$title = apply_filters('widget_title', $instance['title']);
$nav_menu1 = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
$checkbox = $instance['checkbox'];
echo $before_widget;
if ($title) {
echo $before_title . $title . $after_title;
}
if ($nav_menu1) {
echo wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu1 ) );
}
if ($checkbox == true) {
echo 'This message is displayed if our checkbox is checked.';
}
echo $after_widget;
}
/* saves options chosen from the widgets panel */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['nav_menu'] = (int) $new_instance['nav_menu'];
$instance['checkbox'] = strip_tags($new_instance['checkbox']);
return $instance;
}
/* display widget in widgets panel */
function form($instance) {
$title = esc_attr($instance['title']);
$nav_menu1 = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
$checkbox = esc_attr($instance['checkbox']);
$menus1 = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
<select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
<?php
foreach ( $menus1 as $menu1 ) {
echo '<option value="' . $menu1->term_id . '"'
. selected( $nav_menu, $menu1->term_id, false )
. '>'. $menu1->name . '</option>';
}
?>
</select>
</p>
<p>
<input id="<?php echo $this->get_field_id('checkbox'); ?>" name="<?php echo $this->get_field_name('checkbox'); ?>" type="checkbox" value="1" <?php checked( '1', $checkbox ); ?>/>
<label for="<?php echo $this->get_field_id('checkbox'); ?>"><?php _e('Do you want this to display as 2 columns?'); ?></label>
</p>
<?php
}
}
// register widget
add_action('widgets_init', create_function('', 'return register_widget("kedc_mini_sitemap");'));
You have a typo in the form() function: $nav_menu1 and $nav_menu.
Use more meaningful, strict and descriptive names for your variables. For example, $menus1 could be called $get_nav_menus.
Use a good IDE, like NetBeans or similar, and check the WordPress Coding Standards Handbook.
Oh, yes, your code is dumping PHP Notices, always develop with WP_DEBUG enabled.

Wordpress how to set default values for widgets?

Im trying to make my own widget. But i found out that this is not so easy. I'm trying to set default values but i have no idea how to do this.
Yes, i have Googled a lot. I'm almost trying for 1 week to get my own options page and custom widgets, but im not succeeding.
so back to my question, this is my code now:
class Superdeal_Widget extends WP_Widget {
public function __construct()
{
parent::__construct(
'Superdeal-widget',
'Superdeal Widget',
array(
'description' => 'Superdeal widget'
),
array (
'width' => 400,
'height' => 350
)
);
}
public function widget( $args, $instance )
{
// basic output just for this example
echo '<p>'.$instance['titel'].' '.$instance['superdeal'].'</p>';
}
public function form( $instance )
{
// removed the for loop, you can create new instances of the widget instead
?>
<p>
<label for="<?php echo $this->get_field_id('titel'); ?>">Titel</label><br />
<input type="text" name="<?php echo $this->get_field_name('titel'); ?>" id="<?php echo $this->get_field_id('titel'); ?>-title" value="<?php echo $instance['titel']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">Url</label><br />
<input type="text" name="<?php echo $this->get_field_name('url'); ?>" id="<?php echo $this->get_field_id('url'); ?>-url" value="<?php echo $instance['url']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('superdeal'); ?>">Superdeal tekst</label><br />
<input type="text" name="<?php echo $this->get_field_name('superdeal'); ?>" id="<?php echo $this->get_field_id('superdeal'); ?>-title" value="<?php echo $instance['superdeal']; ?>" class="widefat" />
</p>
<?php
}
}
// end class
// init the widget
add_action( 'widgets_init', create_function('', 'return register_widget("Superdeal_Widget");') );
PS: if you know a good tutorial which shows how to make you own options page / declare properties / makeing custom widgets, i would love to hear from you. Because all the tutorials i have followd are old, obscure or way too complicated.
Thank you guys!
This should help you → widget tutorial
Note on this line from form() function:
$instance = wp_parse_args( (array) $instance, array( 'title' => 'DEFAULT_VALUE_HERE' ) );
You can add several instances like:
array( 'title' => 'DEFAULT_VALUE_HERE', 'name' => 'DEFAULT_VALUE_HERE')
Hope this would help you going.

How to create dynamic field names in a wordpress widget

I'm writing a WordPress widget plugin with a settings form of HTML checkboxes. Each checkbox corresponds to a different post_type. This is my code:
function form($instance) {
$defaults = array( 'num_posts' => 5 );
$instance = wp_parse_args( (array) $instance, $defaults );
$num_posts = $instance['num_posts'];
$post_types = get_post_types();
$instance['post_types'] = $post_types;
?>
<p>Number of posts to show: <input class="widefat" name="<?php echo $this->get_field_name( 'num_posts' ); ?>" type="number" value="<?php echo esc_attr( $num_posts ); ?>" /></p>
<p>Filter by Post Type: <ul>
<?php foreach ($post_types as $post_type) { ?>
<li><input name="<?php echo $post_type; ?>" type="checkbox" <?php checked( $post_types, 'on' ); ?> /><label for="<?php echo $post_type; ?>" /><?php echo $post_type; ?></label></li>
<?php
} ?>
</ul></p>
My question is, how do I generate dynamic $instance names for the checkboxes in the loop? I realize I should be able to do something like $this->get_field_name( 'myname' );, but how to make that dynamic?
NOTE: The above code sample simply outputs the post_type for the name property instead of using get_field_name; this is not a solution, just a wall I hit.
Thanks!
EDIT:
I narrowed the issue to the widget update function, which doesn't seem to work when using a foreach loop. Another unanswered issue on stackoverflow describes something similar. PHP - Wordpress - Plugin Widget Update Function - Update array values [Foreach Loop not Working]
Here's my code (slightly revised) including the update function.
// build the widget settings form
function form( $instance ) {
$defaults = array( 'num_posts' => 5 );
$instance = wp_parse_args( (array) $instance, $defaults );
$num_posts = $instance['num_posts'];
foreach ( get_post_types() as $post_type ) {
$post_types_array[$post_type] = $post_type;
}
$instance['post_types'] = $post_types_array;
$post_types = $instance['post_types'];
echo '<pre>';
print_r($instance);
echo '</pre>';
?>
<p>Number of posts to show: <input class="widefat" name="<?php echo $this->get_field_name( 'num_posts' ); ?>" type="number" value="<?php echo esc_attr( $num_posts ); ?>" /></p>
<p>Filter by Post Type: <ul>
<?php foreach ( $post_types as $post_type ) { ?>
<li><input name="<?php echo $this->get_field_name( $post_type ); ?>" type="checkbox" <?php checked( $this->get_field_name( $post_type ) ); ?> /><label for="<?php echo $this->get_field_name( $post_type ); ?>" /><?php echo $post_type; ?></label></li>
<?php
} ?>
</ul></p>
<?php
}
// save the widget settings
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['num_posts'] = strip_tags( $new_instance['num_posts'] );
foreach ( $instance['post_types'] as $post_type ) {
$instance['post_types'][$post_type] = strip_tags( $new_instance['post_types'][$post_type] );
}
return $instance;
}

Multiple editors (TinyMCE) in custom post types

I have a custom post type for products. I have two TinyMCE editors (the standard and one for a summary field) loading correctly in the Dashboard. From the Dashboard side of things, everything is working correctly. Adds, updates. etc...
On the site though, output is loosing the line breaks (paragraph tags). Here is an example:
http://keg.brettatkin.com/products/complete-consulting-skills-learning-system/
I'm using the wp_editor function for this. (http://codex.wordpress.org/Function_Reference/wp_editor)
Here is my code:
<?php
function keg_product_fields (){
global $post;
$custom = get_post_custom($post->ID);
$keg_product_price = $custom["keg_product_price"][0];
$keg_product_link = $custom["keg_product_link"][0];
$keg_product_type = $custom["keg_product_type"][0];
$keg_product_featured = $custom["keg_product_featured"][0];
$keg_product_summary = $custom["keg_product_summary"][0];
$editor_id = "kegprodsummary"
?>
<p>
<label>Summary:</label><br />
<?php wp_editor( $keg_product_summary, $editor_id, $settings = array('textarea_name' => 'keg_product_summary') ); ?>
</p>
<p>
<label>Price:</label><br />
<input size="10" name="keg_product_price" value="<?php echo $keg_product_price; ?>" />
</p>
<p>
<label>Type:</label><br />
<select name="keg_product_type">
<option value="<?php echo $keg_product_type; ?>" selected="selected"><?php echo $keg_product_type; ?></option>
<option value="Book">Book</option>
<option value="CD">CD</option>
<option value="Downloadable">Downloadable</option>
<option value="Multimedia">Multimedia</option>
<option value="Virtual">Virtual</option>
</select>
</p>
<p>
<label>Link:</label><br />
<input size="65" maxlength="200" name="keg_product_link" value="<?php echo $keg_product_link; ?>" />
</p>
<p>
<input type="checkbox" name="keg_product_featured" value="Yes" <?php if (!(strcmp("$keg_product_featured","Yes"))) {echo "checked=\"checked\"";} ?>/>
<label>Featured Product</label>
</p>
<?php
}
function add_keg_product_box (){
add_meta_box(
"keg_product_info",
"Product Details",
"keg_product_fields",
"keg_products"
);
}
function save_keg_product_attributes ( $post_id )
{
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
global $post;
update_post_meta($post->ID, "keg_product_price", $_POST["keg_product_price"]);
update_post_meta($post->ID, "keg_product_link", $_POST["keg_product_link"]);
update_post_meta($post->ID, "keg_product_type", $_POST["keg_product_type"]);
update_post_meta($post->ID, "keg_product_featured", $_POST["keg_product_featured"]);
update_post_meta($post->ID, "keg_product_summary", $_POST["keg_product_summary"]);
}
add_action ('admin_init', 'add_keg_product_box' );
add_action ('save_post', 'save_keg_product_attributes');
add_action ('publish_post', 'save_keg_product_attributes');
?>
Any ideas here?
Thanks!
Brett
You have to apply the output filters with the apply_filters function in the page where you want to display your summary.
Example:
<?php
$summary = get_post_meta(...);
echo apply_filters('the_content', $summary);
?>
Try using wpautop() when saving the content or when showing it (I recommend you to use it when saving, for performance).
This function adds paragraphs throughout the text depending on the line breaks.

Resources