Array of checkboxes not saving in WordPress widget - wordpress

This is my code for the widget:
function form( $instance ) {
$instance = wp_parse_args( (array)$instance, array(
'checkboxes' => array(
'Monday' => array('name' => 'Monday', 'value' => 'Monday', 'checked' => 1),
'Tuesday' => array('name' => 'Tuesday', 'value' => 'Tuesday', 'checked' => ''),
'Wednesday' => array('name' => 'Wednesday', 'value' => 'Wednesday', 'checked' => ''),
'Thursday' => array('name' => 'Thursday', 'value' => 'Thursday', 'checked' => ''),
'Friday' => array('name' => 'Friday', 'value' => 'Friday', 'checked' => ''),
'Saturday' => array('name' => 'Saturday', 'value' => 'Saturday', 'checked' => ''),
'Sunday' => array('name' => 'Sunday', 'value' => 'Sunday', 'checked' => '')
),
'title' => 'Workdays'
));
include( plugin_dir_path(__FILE__) . '/views/admin.php' );
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['checkboxes'] = strip_tags($new_instance['checkboxes']);
return $instance;
}
This is the code for the view:
<div class='ws-business-info'>
<div class='form-group'>
<?php foreach($instance['checkboxes'] as $day ) : ?>
<div class='checkbox'>
<label>
<input type="checkbox"
name="<?php echo $day['name']; ?>"
class="form-control"
id="<?php echo $this->get_field_id($day['name']); ?>"
value="<?php echo $day['value']; ?>"
<?php checked('1', $day['checked']); ?>/>
<?php echo $day['name']; ?>
</label>
</div>
<?php endforeach; ?>
</div>
The widget displays the checkboxes as expected but the state will not save. Dumping the $old_instance variable in the update function gives a null value.

In the view file you defined input's id corectly, but you did not the same with name attribute.
Define the name attribute like this:
<input type="checkbox"
name="<?php echo $this->get_field_name( 'checkboxes' ), '[', esc_attr( $day['name'] ), ']'; ?>"
class="form-control"
id="<?php echo $this->get_field_id($day['name']); ?>"
value="<?php echo $day['value']; ?>"
<?php checked('1', $day['checked']); ?>/>
<?php echo $day['name']; ?>

public function widget( $args, $instance ) {
// outputs the content of the widget
}
This function has to be added along with the default constructor. Else the widget will not work.

Not exactly what you are looking for but this is how I saved a checkbox in metabox, you may get some hint from this...
Code used to display html
function html_rtsocial_metabox()
{
global $post;
$custom = get_post_custom($post->ID);
$suppress = $custom['_suppress_rtsocial'][0];
$checked = ($suppress=='yes')?'checked':'';
$value = ($suppress=='yes')?'yes':'no';
$html = '<br><input type="checkbox" name="_suppress_rtsocial" value="'.$value.'" '.$checked.' id="_suppress_rtsocial" /> Hide Social Icons ???';
$html .= '<br/><br/>';
echo $html;
}
While saving
update_post_meta($post_id,'_suppress_rtsocial',$_POST['_suppress_rtsocial']);
Added js for admin interface
function checkbox_helper(){
var ele =jQuery('#_suppress_rtsocial'),value;
ele.click(function(){
value = ele.is(':checked')?'yes':'no';
ele.val(value);
}
);
}

Related

filter by custom field WordPress

I want to search posts by custom field type.
I have custom field "country "with a custom post type umrahpackage now I want if a visitor search for a country "abc" its should show all posts with abc country.
here is my code but not working.
`
add_shortcode('user_search','My_User_search');
function My_User_search($atts = null)
{
$out = user_search_form();
$args = array(
'post_type' => 'umrahpackage',
'meta_query' => array(
array(
'key' => 'country',
'value' => 'pakistan',
'compare' => 'LIKE',
))
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
$out .= '<li>' . get_the_title() . '</li>';
endwhile;
endif;
return $out;
}
//function to display user search form
function user_search_form(){
$metavalue = $metakey = '';
if (isset($_GET['search_by'])){
$metakey = $_GET['search_by'];
}
if (isset($_GET['s_value'])){
$metavalue = $_GET['s_value'];
}
$re = '<div class="user_search"><form action="" name="user_s" method="get">
<label for="search_by">Search by:</label>
<select id="search_by" name="search_by">';
if ($metakey != ''){
$re.= '"';
$re.= ($metakey == "country") ;
}else{
$re .= '
<option value="country">Comapny Name</option>';
}
$re .= '
</select>
<label>Company Name</label>
<input id="s_value" name="s_value" type="text" value="'.$metavalue.'"/>
<input type="hidden" id="user_search" name="post_type" value="umrahpackage" />
<input id="submit" type="submit" value="Search" />
</form></div>';
return $re;
}`
I solved it I share it if its help some one else
add_shortcode('user_search','My_User_search');
function My_User_search($atts = null){
$out = user_search_form();
$args = array('post_type' => 'umrahpackage','order' => 'asc',
'meta_query' => array(
array(
'key' => 'country',
'value' => $_GET['s_value'],
'compare' => 'Like',
)
)
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if($_GET['s_value']==''){
//before search hide the posts
}
else {
$out .= '<li>' . get_the_title() . '</li>';
}
endwhile;
endif;
return $out;
}
function user_search_form(){
$re = '<div class="user_search"><form action="" name="user_s" method="get">
<label for="search_by">Search by:</label>
<div id="search_by" name="search_by">';
$re .= '
<label>Company Name</label>
<input id="s_value" name="s_value" type="text" value="'.$metavalue.'"/>
<input name="user_search" id="user_search" type="hidden" value="umrahpackage"/>
<input id="submit" type="submit" value="Search" />
</form></div>';
return $re;
}
?>

Wordpress: How to save values from repeater field?

I am using this repeater plugin with a custom wordpress template https://github.com/DubFriend
/jquery.repeater
<div class="repeater">
<div data-repeater-list="category-group">
<div data-repeater-item>
<input type="hidden" name="id" id="cat-id"/>
<input type="text" name="cat-title" />
<input type="text" name="cat-slug" />
<input data-repeater-delete type="button" value="Delete"/>
</div>
</div>
<input data-repeater-create type="button" value="Add"/>
</div>
but values not saving in database
In save post function i have
add_shortcode('album','ty_front_end_form');
function ty_save_post_data() {
if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') ) {
print 'Sorry, your nonce did not verify.';
exit;
} else {
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
exit;
}
$release= $_POST['custom_release'];
$artist = $_POST['custom_arname'];
$lang = $_POST['custom_lang'];
$id = $_POST['id'];
$catt = $_POST['cat-title'];
$cats = $_POST['cat-slug'];
$post = array(
'post_title' => wp_strip_all_tags( $title ),
'post_status' => 'publish',
'post_type' => 'album',
'meta_input' => array(
'custom_release' => $release,
'custom_arname' => $artist,
'custom_lang' => $lang,
'id' => $id,
'cat-title' => $catt,
'cat-slug' => $cats
)
);
$post_id = wp_insert_post($post);
$location = home_url();
echo "<meta http-equiv='refresh' content='0;url=$location' />";
exit;
} // end IF
}
and this is a shortcode for frontend user posting which is showing on a template page

ACF Custom user field in search query

I have created some field in user section, Now i want to search user by custom field. Like Phone of City Name. While search by first name and last name is working fine by could not get user by phone or city name.
My custom user field is "phone" and "pin_code"
<?php if( $_GET['s'] ) {
$search_term = sanitize_text_field( stripslashes( $_GET['s']));
// WP_User_Query arguments
$args = array (
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*' . esc_attr( $search_term ) . '*',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => $_phone,
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if ( ! empty( $authors ) ) {
// loop through each author
foreach ( $authors as $author ) {
// get all the user's data
$author_info = $author->ID; ?>
<div ><?php
$phone = get_field('phone', 'user_'. $author_info );
$adderess = get_field('adderess', 'user_'. $author_info );
$adderess_2 = get_field('adderess_2', 'user_'. $author_info );
$zip = get_field('pin_code', 'user_'. $author_info );
$bio = get_field('coach_bio', 'user_'. $author_info );
$category = get_field('category', 'user_'. $author_info );
if( get_field('avatar', 'user_'. $author_info ) ) {
$avatar = get_field('avatar', 'user_'. $author_info );
}
else {
$avatar = 'http://0.gravatar.com/avatar/0c12cfa22b6d6f53d7701858ecc3b67e?s=96&d=mm&r=g';
}
?>
</div>
<div class="member">
<h2 class="member-name"><?php echo $author->display_name; ?></h2>
<div class="user">
<?php $user = get_userdata( $author_info );
$user_roles = $user->roles;
if ( in_array( 'pms_subscription_plan_11', $user_roles, true ) ) {
echo '<img src="http://lifecoachnearme.co.uk/wp-content/uploads/2020/01/policy-badge.png" class="badget">';
}
?>
<div class="article_left">
<?php echo '<img src="'.$avatar.'" >'; ?>
</div>
<div class="article_right">
<div class="profile-display-address">
<div class="col-6"><?php echo $adderess; ?></div>
<div class="col-6"><?php echo $adderess_2; ?></div>
<div class="col-6"><?php echo $zip; ?></div>
</div>
<div class="contact-form contact-links">Contact me</div>
</div>
<div class="button-profile"><a class="button whitebg" href="<?php echo get_author_posts_url($author_info); ?>">View profile</a></div>
</div>
<?php }
echo '</ul>';
}
else {
echo 'No authors found';
}
}
?>
I really done know how to pass custom user field in query arg.
Also i want to give a dropdown next to the search input that user can select there area of expertise. Like they are teacher or student. So result should be relative

Custom wordpress latest posts plugin will not limit posts

Have been fiddling around with the while loop as well as the query params but can't seem to figure out what is causing the following custom code to not limit posts:
/**
* Plugin Name: Latest posts
*/
add_action( 'widgets_init', 'weart_latest_posts' );
function weart_latest_posts() {
register_widget( 'weart_latest_posts' );
}
class weart_latest_posts extends WP_Widget {
//widget setup
function weart_latest_posts() {
$widget_ops = array( 'classname' => 'weart_latest_posts', 'description' => esc_html__('A widget that displays the latest posts.', 'custom') );
$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'weart_latest_posts' );
$this->__construct( 'weart_latest_posts', esc_html__('Custom: Latest Posts Widget', 'custom'), $widget_ops, $control_ops );
}
//display the widget
function widget( $args, $instance ) {
extract( $args );
global $post;
$title = apply_filters('widget_title', $instance['title'] );
$number = $instance['number'];
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;
?>
<?php
$args1 = array(
'cat' => 'RECIPES',
'nopaging' => false,
'posts_per_page' => '5',
'found_posts' => '5',
'max_num_pages' => '5',
'post_count' => '5',
'order' => 'DESC',
'orderby' => 'ID',
);
$query = new WP_Query( $args1 );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
;?>
<a href="<?php the_permalink(); ?>" class="block cf item border-top">
<?php if ( has_post_thumbnail()) : ?>
<div class="featured-image col col-one-third img" data-mh="latest_posts_height">
<div class=""><div class="bg lazy" data-src="<?php the_post_thumbnail_url( 'weart-related-thumb'); ?>"></div></div>
</div>
<?php endif; ?>
<div class="text col <?php if ( has_post_thumbnail()) : ?>col-two-third<?php endif; ?>" data-mh="latest_posts_height">
<div class="<?php if ( has_post_thumbnail()) : ?>inner-left text-vertical<?php endif; ?>">
<h3 class="title h4"><?php the_title(); ?></h3>
<div class="meta c-grey link-grey meta-meta"><time datetime="<?php echo esc_attr(get_the_date('Y-m-d')); ?>"> <?php echo esc_attr(get_the_date()); ?></time></div>
</div>
</div>
</a>
<?php }
} else {
} wp_reset_postdata();
?>
<?php
/* After widget (defined by themes). */
echo $after_widget;
}
//update widget
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = '5';
return $instance;
}
//form for update
function form( $instance ) {
//defaults
$defaults = array( 'title' => esc_html__('Latest Recipes', 'custom'), 'number' => 5, 'popular_days' => 30 );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p><!-- title -->
<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>">Title:</label>
<input id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'title' )); ?>" value="<?php echo esc_attr($instance['title']); ?>" style="width:90%;" />
</p><!-- title -->
<p><!-- posts num -->
<label for="<?php echo esc_attr($this->get_field_id( 'number' )); ?>">Number of posts to display:</label>
<input id="<?php echo esc_attr($this->get_field_id( 'number' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number' )); ?>" value="<?php echo esc_attr($instance['number']); ?>" size="3" />
</p><!-- posts num -->
<?php
}
}
?>
I thought the query params would lead to what I needed and played around with having them both as a variable and also directly in the statement.
Try to remove these:
'nopaging' => false,
'found_posts' => '5',
'max_num_pages' => '5',
'post_count' => '5',
And pass a category ID to this:
'cat' =>

Save Selected Option in Meta Box

i am going to build my own theme
I try to add select to post_type meta box, but every update post my select not on selected option but show the first option (blank value)
here is my code
function mhs_data() {
global $post;
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$nisn = get_post_meta($post->ID, '_nisn', true);
$rel = get_post_meta($post->ID, '_rel', true);
}
echo '<p>NISN</p>';
echo '<input type="text" name="_nisn" value="' . $nisn . '" class="widefat" />';
echo '<p>Relationship</p>'; ?>
<select name="_rel" id="_rel">
<option value="">Relationship</option>
<option value="Single" <?php selected( $rel, 'Single' ); ?>>Single</option>
<option value="Marry" <?php selected( $rel, 'Marry' ); ?>>Marry</option>
</select>
<?php
}
function mhs_data_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
$events_meta['_nisn'] = $_POST['_nisn'];
$events_meta['_rel'] = $_POST['_rel'];
foreach ($events_meta as $key => $value) {
if( $post->post_type == 'revision' ) return;
$value = implode(',', (array)$value);
if(get_post_meta($post->ID, $key, FALSE)) {
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key);
}
}
add_action('save_post', 'mhs_data_meta', 1, 2);
Please help me to correct my code
Using Codestar Framework its very simple tu add metaboxes to your custom post, create configuration page, plugins, taxonomies and use the customizer if you prefer use it instead of a configuration page. see the documentation here for more information.
in your example, to add a select metabox shuld use a code similar to:
load the framework on your functions.php
require_once __DIR__ . '/'.$FRAMEWORK_PATH.'/cs-framework/cs-framework.php';
edit the cs-framekork.php configuration file to active only the features you need:
defined('CS_ACTIVE_FRAMEWORK') or define('CS_ACTIVE_FRAMEWORK', false); // if you need it for plugin or configuration page
defined('CS_ACTIVE_METABOX') or define('CS_ACTIVE_METABOX', true); // if you only need it for metabox
defined('CS_ACTIVE_TAXONOMY') or define('CS_ACTIVE_TAXONOMY', false);
defined('CS_ACTIVE_SHORTCODE') or define('CS_ACTIVE_SHORTCODE', false);
defined('CS_ACTIVE_CUSTOMIZE') or define('CS_ACTIVE_CUSTOMIZE', false);
and then in your function.php or as I prefer on another file included in your function.php register your metabox
function register_this_metabox($options)
{
$options = array(); // this will clean the default cs-framework configuration
$options[] = array(
'id' => 'the_metabox',
'title' => 'Meta Box title',
'post_type' => 'post', // the post type where the metabox appears
'context' => 'side', // metabox position
'priority' => 'high',
'sections' => array( // metabox fields, see the documentation
array(
'name' => 'the_metabox_fields',
'fields' => array(
array(
'id' => 'the_metabox_select_field',
'type' => 'select',
'title' => 'Select Field',
'options' => array(
'opt1' => 'Option 1',
'opt2' => 'Option 2',
'opt3' => 'Option 3',
),
'default_option' => 'Select a option',
),
),
),
),
);
return $options;
}
add_filter('cs_metabox_options', 'register_this_metabox');
And now you only have to get the values in your theme:
$post_metabox = get_post_meta($post->ID, 'the_metabox', true);
$selected_option = $post_metabox['the_metabox_select_field']

Resources