Unable to save custom metabox data from metabox plugin - php

I am quite new to php and wordpress but have programming knowledge.
i am attempting to save data from these metaboxes in metabox plugin by creating a new custom metabox. this is my code
$tour_rates = get_post_meta( $post->ID, $id, true ) ? maybe_unserialize(get_post_meta( $post->ID, $id, true )) : false;
if ($tour_rates){
foreach ( $tour_rates as $options => $option ) {
$key = 1
$html .= '<tr class="rate-line">';
$html .= '<td>';
$html .= '<span>Opt ' . ($options+1) . '</span>';
$html .= '<input type="hidden" name="' . $id . '[]" class="rwmb-text" size="30" value="">';
$html .= '</td>';
$html .= '<td><input type="text" name="pax_date_'.$key.'[]" class="rwmb-date" size="3" value="'.$option[$key-1].'"></td>';
$html .= '<td><input type="text" name="pax_price_'.$key.'[]" class="pax-price" size="3" value="'.$option[$key-1].'"></td>';
$html .= '</tr>';
}
Now in the save function i have this code. i am able to save only pax-price and not the date along with pax-price. need help and thanks in advance for all your help
static function save( $new, $old, $post_id, $field )
{
$name = $field['id'];
$tour_rates = array();
foreach ( $_POST[$name] as $k => $v ) {
$tour_rates[$k] = array(
$_POST['pax_price_1'][$k],
$_POST['pax_date_1'][$k],
);
}
$new = maybe_serialize( $tour_rates );
update_post_meta( $post_id, $name, $new );
}

Anyways I got the solution. i had made a mistake in the way i was displaying, just in case someone needs a way:
$tour_rates = get_post_meta( $post->ID, $id, true ) ? maybe_unserialize(get_post_meta( $post->ID, $id, true )) : false;
$html = '<div id="tour-rates-info">';
$html .= '<div class="inside">';
$html .= '<div class="rwmb-field">';
$html .= '<div class="rwmb-input">';
$html .= '<table>';
$html .= '<tr>';
$html .= '<th> </th>';
$html .= '<th>'."Departure Date".'</th>';
$html .= '<th>'."Price".'</th>';
$html .= '</tr>';
if ($tour_rates){
foreach ( $tour_rates as $k => $v ) {
$key = 1;
$html .= '<tr class="rate-line">';
$html .= '<td>';
$html .= '<span>Opt ' . ($k+1) . '</span>';
$html .= '<input type="hidden" name="' . $id . '[]" class="rwmb-text" size="30" value="">';
$html .= '</td>';
$html .= '<td><input type="text" name="pax_date_'.$key.'[]" class="rwmb-date" size="9" value="'.$v[0].'"></td>';
$html .= '<td><input type="text" name="pax_price_'.$key.'[]" class="pax-price" size="9" value="'.$v[1].'"></td>';
$html .= '</tr>';
}
Now the save function remains same
static function save( $new, $old, $post_id, $field )
{
$name = $field['id'];
$tour_rates = array();
$tour_rate = array();
foreach ( $_POST[$name] as $k => $v ) {
$tour_rates[$k] = array(
$_POST['pax_date_1'][$k],
$_POST['pax_price_1'][$k],
);
}
echo $tour_rates;
$new = maybe_serialize( $tour_rates );
update_post_meta( $post_id, $name, $new );
}

Related

JSON to TABLE in PHP

for anyone who can give a hand. Thanks a lot.
I can't get the JSON data to get into the table. I don't know if it's a problem with the data request path...
When I do // var_dump ($ results); I check that the results of the api (url) are loaded in the page but I don't know what error I made when trying to introduce them in the table
add_shortcode( 'external_data', 'callback_function_name');
function callback_function_name() {
$url = 'https:
$arguments = array(
'method' => 'GET',
);
$response = wp_remote_get( $url, $arguments );
$results = json_decode( wp_remote_retrieve_body( $response ) );
// var_dump($results);
$html = '';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>name</td>';
$html .= '<td>suite</td>';
$html .= '<td>display_name</td>';
$html .= '<td>id</td>';
$html .= '<td>conference</td>';
$html .= '<td>division</td>';
$html .= '</tr>';
foreach( $results as $result ) {
$html .= '<tr>';
$html .= '<td>' . $result->name . '</td>';
$html .= '<td>' . $result->nickname . '</td>';
$html .= '<td>' . $result->display_name . '</td>';
$html .= '<td>' . $result->id . '</td>';
$html .= '<td>' . $result->conference . '</td>';
$html .= '<td>' . $result->division . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
return $html;
}
fgYou can use file_get_contents
function callback_function_name() {
$data=file_get_contents('https://https://delivery.oddsandstats.co/team_list/NFL.JSON?api_key=your api key');
$data=(array)json_decode($data);
$result=(array)$data['results'];
$teams=$result['data']->team;
$html = '';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>name</td>';
$html .= '<td>suite</td>';
$html .= '<td>display_name</td>';
$html .= '<td>id</td>';
$html .= '<td>conference</td>';
$html .= '<td>division</td>';
$html .= '</tr>';
foreach( $teams as $result ) {
$html .= '<tr>';
$html .= '<td>' . $result->name . '</td>';
$html .= '<td>' . $result->nickname . '</td>';
$html .= '<td>' . $result->display_name . '</td>';
$html .= '<td>' . $result->id . '</td>';
$html .= '<td>' . $result->conference . '</td>';
$html .= '<td>' . $result->division . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
return $html;
}

how can I image (.png) hide when the metabox empty

I want the button hide when it is empty remain unfortunately only the HTML before, even though the button is already empty, how can I rewrite the code so the button is hidden? I posted more details maybe someone can help me me
function cspm_infobox_content(){
$post_id = esc_attr($_POST['post_id']);
$infobox_type = esc_attr($_POST['infobox_type']);
$map_id = esc_attr($_POST['map_id']);
$status = esc_attr($_POST['status']);
$carousel = esc_attr($_POST['carousel']);
$telnr = get_post_meta( $post_id, 'telnr', true );
$telnr1 = get_post_meta( $post_id, 'telnr1', true );
$email = get_post_meta( $post_id, 'email', true );
$hane = get_post_meta( $post_id, 'hane', true );
$marte = get_post_meta( $post_id, 'marte', true );
$merkure = get_post_meta( $post_id, 'merkure', true );
$enjte = get_post_meta( $post_id, 'enjte', true );
$premte = get_post_meta( $post_id, 'premte', true );
$shtune = get_post_meta( $post_id, 'shtune', true );
$djele = get_post_meta( $post_id, 'djele', true );
$shteti = get_post_meta( $post_id, 'shteti', true );
$qyteti = get_post_meta( $post_id, 'qyteti', true );
$rajoni = get_post_meta( $post_id, 'rajoni', true );
$rruganr = get_post_meta( $post_id, 'rruganr', true );
$no_title = array(); // Infoboxes to display with no title
$no_link = array(); // Infobox to display whit no link
$no_description = array('square_bubble', 'rounded_bubble', 'cspm_type2', 'cspm_type3', 'cspm_type4'); // Infoboxes to display with no description
$no_image = array('cspm_type4'); // Infoboxes to display with no image
if(!in_array($infobox_type, $no_title)) $item_title = apply_filters('cspm_custom_infobox_title', stripslashes_deep($this->cspm_items_title($post_id, $this->items_title)), $post_id);
if(!in_array($infobox_type, $no_description)) $item_description = apply_filters('cspm_custom_infobox_description', stripslashes_deep($this->cspm_items_details($post_id, $this->items_details)), $post_id);
if(!in_array($infobox_type, $no_link)) $the_permalink = $this->cspm_get_permalink($post_id);
if(!in_array($infobox_type, $no_image)){
if($infobox_type == 'square_bubble' || $infobox_type == 'rounded_bubble')
$parameter = array( 'style' => "width:50px; height:50px;" );
elseif($infobox_type == 'cspm_type1')
$parameter = array( 'style' => "width:100px; height:80px;" );
elseif($infobox_type == 'cspm_type2')
$parameter = array();
elseif($infobox_type == 'cspm_type3' || $infobox_type == 'cspm_type5')
$parameter = array( 'style' => "width:60px; height:60px;" );
elseif($infobox_type == 'cspm_type4')
$parameter = array();
if($infobox_type == 'square_bubble' || $infobox_type == 'rounded_bubble'){
$infobox_thumb = get_the_post_thumbnail($post_id, 'cspacing-marker-thumbnail', $parameter);
}elseif($infobox_type == 'cspm_type1'){
$infobox_thumb = get_the_post_thumbnail($post_id, 'cspacing-infobox1-thumbnail', $parameter);
}else $infobox_thumb = get_the_post_thumbnail($post_id, 'cspacing-horizontal-thumbnail', $parameter);
if(empty($infobox_thumb))
$infobox_thumb = get_the_post_thumbnail($post_id, 'cspacing-horizontal-thumbnail', $parameter);
}
$post_thumbnail = apply_filters('cspm_infobox_thumb', $infobox_thumb, $post_id, $infobox_type, $parameter);
$this->infobox_external_link = $this->cspm_get_setting('infoboxsettings', 'infobox_external_link', 'same_window');
$target = ($this->infobox_external_link == 'new_window') ? ' target="_blank"' : '';
$the_post_link = ($this->infobox_external_link == 'disable') ? $item_title : '<a href="'.$the_permalink.'" title="'.$item_title.'"'.$target.'>'.$item_title.'</a>';
$output = '';
$output .= '<div class="cspm_infobox_content_container '.$status.' infobox_'.$map_id.' '.$infobox_type.'" data-map-id="'.$map_id.'" data-post-id="'.$post_id.'" data-show-carousel="'.$carousel.'">';
if($infobox_type == 'square_bubble' || $infobox_type == 'rounded_bubble'){
$output .= '<div class="cspm_infobox_img">';
$output .= ($this->infobox_external_link != 'disable') ? '<a href="'.$the_permalink.'" title="'.$item_title.'"'.$target.'>'.$post_thumbnail.'</a>' : $post_thumbnail;
$output .= '</div>';
$output .= '<div class="cspm_arrow_down '.$infobox_type.'"></div>';
}elseif($infobox_type == 'cspm_type1'){
$output .= '<div class="cspm_infobox_img">'.$post_thumbnail.'</div>';
$output .= '<div class="cspm_infobox_content">';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '<div class="description">'.$item_description.'</div>';
$output .= '</div>';
$output .= '<div style="clear:both"></div>';
$output .= '<div class="cspm_arrow_down"></div>';
}elseif($infobox_type == 'cspm_type2'){
$output .= '<div class="cspm_infobox_img">'.$post_thumbnail.'</div>';
$output .= '<div class="cspm_infobox_content">';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '</div>';
$output .= '<div class="cspm_arrow_down"></div>';
}elseif($infobox_type == 'cspm_type3'){
$output .= '<div class="cspm_infobox_img">'.$post_thumbnail.'</div>';
$output .= '<div class="cspm_infobox_content">';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '</div>';
$output .= '<div class="cspm_arrow_down"></div>';
}elseif($infobox_type == 'cspm_type4'){
$output .= '<div class="cspm_infobox_content">';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '</div>';
$output .= '<div class="cspm_arrow_down"></div>';
/**
* #since 2.7 */
}elseif($infobox_type == 'cspm_type5'){
$output .= '<div class="cspm_infobox_content">';
$output .= '<div>';
$output .= '<div class="cspm_infobox_img">'.$post_thumbnail.'</div>';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '<div class="telnr"><a class="wm-button color-read" href="tel:'.$telnr.'"><img src="http://test.com/test/wp-content/uploads/2015/06/Phone-18-18-dimenzionet.png" height="18" width="18"></a>';
$output .= '<a class="wm-button color-read telnr1" style="left: 10px;" href="tel:'.$telnr1.'"><img src="http://test.com/test/wp-content/uploads/2015/06/Phone-18-18-dimenzionet.png" height="18" width="18"></a>';
$output .= '<a class="wm-button color-blue email" style="left: 20px;" href="mailto:'.$email.'?Subject=Un%20jam%20informuar%20për%20ju%20nga%20www.test.com "><img src="http://test.com/pinfo/wp-content/uploads/2015/06/email-ico.png" height="18" width="18"></a></div>';
if the value $ telnr1 is empty I want which is the whole HTML hidden below
<a class="wm-button color-read telnr1" style="left: 10px;" href="tel:'.$telnr1.'"><img src="../../wp-content/uploads/2015/06/Phone-18-18-dimenzionet.png" height="18" width="18"></a>

php date only show in first row

Good Day,
I have received this code to form a plugin for Ninja Forms. This code enables you to put the submissions of a form on the front end. Everything was working fine until we added a date function. This function is supposed to put the date in front of every row it creates. Unfortunately it only puts the date in front of the first row, the every row after that, the information moves one column to the left with no date.
<?php
function test_display_submissions( $args = array() ) {
// $args = array(
// 'form_id' => 1
// );
$form_id = $args['form_id'];
$columns = $args['cols'];
$columns = explode( ',', $columns );
$sub_results = ninja_forms_get_subs( array( 'form_id' => $form_id ) );
$plugin_settings = get_option("ninja_forms_settings");
if(isset($plugin_settings['date_format'])){
$date_format = $plugin_settings['date_format'];
} else {
$date_format = 'm/d/Y';
}
$content = '<table>
<thead>
<tr>';
$content .= '<th>Date</th>';
foreach ( $columns as $id ) {
$field = ninja_forms_get_field_by_id( $id );
if( isset( $field['data']['label'] ) ){
$label = $field['data']['label'];
} else {
$label = '';
}
$content .= '<th>' . $label . '</th>';
}
$content .= '</tr>
</thead>
<tbody>';
$content .= '<td>' . date($date_format, strtotime($sub['date_updated'])) . '</td>';
foreach ( $sub_results as $sub ) {
$fields = $sub['data'];
echo '<tr>';
foreach ( $fields as $field ) {
$field_id = $field['field_id'];
$user_value = $field['user_value'];
if ( in_array( $field_id, $columns ) ) {
$content .= '<td>' . $user_value . '</td>';
}
}
$content .= '</tr>';
}
$content .= '</tbody>
</table>';
return $content;
}
add_shortcode( 'display_subs', 'test_display_submissions' );
The problem is in the foreach loop. You need to place your date in the loop, not before :
foreach ( $sub_results as $sub ) {
// Display date
$fields = $sub['data'];
$content .= '<tr>';
$content .= '<td>' . date($date_format, strtotime($sub['date_updated'])) . '</td>';
foreach ( $fields as $field ) {
$field_id = $field['field_id'];
$user_value = $field['user_value'];
if ( in_array( $field_id, $columns ) ) {
$content .= '<td>' . $user_value . '</td>';
}
}
$content .= '</tr>';
}
$content .= '</tbody>
You have the beginning after you have the date added to the table.
It should be <tbody><tr><td>DATE STUFF</td><td>MORE CONTENT</td></tr><tbody>
in this format and your date column should be placed within your foreach loop.

How to filter featured products slider by category?

I'm trying to display a "featured products slider" on the Category Page of the WooCommerce shop in such a way that only 'featured' products of the current category (or a specific child category) will be displayed in the slider.
I've placed the code below in the functions.php file of my child-theme.
I tried adding this conditional in archive.php:
if(is_product_category('slug'){echo featured_products_slider_fun($atts, $content);}
I can't figure out how to combine this condition with the function below to essentially show 'featured products' of a certain category'.
===========
function featured_products_slider_func( $atts, $content ) {
extract( shortcode_atts( array(
'num' => 4
), $atts ) );
$return = '';
$first_product_title = "";
$first_product_excerpt = "";
function get_the_popular_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 190);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'. view product';
return $excerpt;
}
$post = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => $num
));
$return .= '<div class="featured-slider-outer box clearfix"><div class="featured-slider-inner">';
$return .= '<ul class="products featured-slider">';
$selected_class = " selected";
if ($post ->have_posts()) : while ($post ->have_posts()) : $post ->the_post();
global $product;
if( $selected_class != "" ) {
$first_product_title = get_the_title();
$first_product_excerpt = get_the_popular_excerpt();
}
$return .= '<li class="product' . $selected_class . '">';
$return .= get_the_post_thumbnail($post->ID, "blog-two-column");
if ($price_html = $product->get_price_html()) :
$return .= '<span class="price">';
$return .= $price_html;
$return .= '</span>';
endif;
$return .= "<div class='none'>";
$return .= "<h3 id='infos-title'>" . get_the_title() . "</h3>";
$return .= "<div id='infos-excerpt'>" . get_the_popular_excerpt() . "</div>";
$return .= "</div>";
$return .= '</li>';
$selected_class = "";
endwhile;
$return .= '</ul>';
$return .= '</div>';
$return .= '<div class="featured-slider-right">';
$return .= '<button class="btn-left"></button><button class="btn-right"></button><h2>' . $first_product_title . '</h2>';
$return .= '<div class="description">' . $first_product_excerpt . '</div>';
$return .= '</div>';
$return .= '</div>';
else:
$return = __("No featured products found!", "shopifiq");
endif;
return $return;
}
add_shortcode( 'featured_products_slider', 'featured_products_slider_func' );

Terms by vocabulary in node.tpl

I've created a variable in template.php that let's me print terms by vocabulary. The problem is that I want to be able to pass in a vocabulary id to select a specific vocabulary. My code looks like this:
function xnalaraart_classic_print_terms($node, $vocabularies){
foreach($vocabularies as $vocabulary){
if($terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid)){
$output .= '<div>';
$output .= '<ul class="links inline">';
foreach ($terms as $term){
$output .= '<li class="taxonomy_term_' . $term->tid . '">';
$output .= l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
}
return $output;
}
and in the preprocess_node function:
$vars['terms_split'] = xnalaraart_classic_print_terms($vars['node']);
How do I write it so that I can pass in an id to $vocabularies?
I think you made this more difficult on yourself than it really is. See below for final function.
function xnalaraart_classic_print_vocab_terms($node, $vid){
if($terms = taxonomy_node_get_terms_by_vocabulary($node, $vid)){
$output .= '<div>';
$output .= '<ul class="links inline">';
foreach ($terms as $term){
$output .= '<li class="taxonomy_term_' . $term->tid . '">';
$output .= l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
return $output;
}
And then call
$vars['terms_split'] = xnalaraart_classic_print_terms($vars['node'], 10); //Where 10 is the vocab ID

Categories