I was using the Chosen plugin to populate a second dropdown based on the selection of the first. I would like to change over to the Selectize plugin for performance reasons now, but am having trouble doing so. My setup is as follows:
1st Dropdown = Custom Post Type (type)
2nd Dropdown = Taxonomy (location)
In my functions.php file, I have the following:
// SECOND DROPDOWN
function my_dropdown_categories( $taxonomy, $current_selected = '', $include = null ) {
// Get all terms of the chosen taxonomy
$terms = get_terms($taxonomy, array('orderby' => 'name'));
// our content variable
$list_of_terms = '<select id="location" class="selectboxSingle" name="location">';
if ( ! is_wp_error( $terms ) ) foreach($terms as $term){
// If include array set, exclude unless in array.
if ( is_array( $include ) && ! in_array( $term->slug, $include ) ) continue;
$select = ($current_selected == $term->slug) ? "selected" : ""; // Note: ==
if ($term->parent == 0 ) {
// get children of current parent.
$tchildren = get_term_children($term->term_id, $taxonomy);
$children = array();
foreach ($tchildren as $child) {
$cterm = get_term_by( 'id', $child, $taxonomy );
// If include array set, exclude unless in array.
if ( is_array( $include ) && ! in_array( $cterm->slug, $include ) ) continue;
$children[$cterm->name] = $cterm;
}
ksort($children);
// OPTGROUP FOR PARENTS
if (count($children) > 0 ) {
// $list_of_terms .= '<optgroup label="'. $term->name .'">';
if ($term->count > 0)
$list_of_terms .= '<option class ="group-result" value="'.$term->slug.'" '.$select.'>' . $term->name .' </option>';
} else
$list_of_terms .= '<option value="'.$term->slug.'" '.$select.'>'. $term->name .' </option>';
//$i++;
// now the CHILDREN.
foreach($children as $child) {
$select = ($current_selected == $child->slug) ? "selected" : ""; // Note: child, not cterm
$list_of_terms .= '<option value="'.$child->slug.'" '.$select.'>'. $child->name.' </option>';
} //end foreach
if (count($children) > 0 ) {
$list_of_terms .= "</optgroup>";
}
}
}
$list_of_terms .= '</select>';
return $list_of_terms;
}
// FIRST DROPDOWN
add_action( 'wp_ajax_wpse158929_get_terms_for_cpt', 'wpse158929_get_terms_for_cpt' );
add_action( 'wp_ajax_nopriv_wpse158929_get_terms_for_cpt', 'wpse158929_get_terms_for_cpt' );
function wpse158929_get_terms_for_cpt() {
$ret = array( 'html' => '', 'error' => false );
if ( ! check_ajax_referer( 'wpse158929_get_terms_for_cpt_submit_', 'nonce', false /*die*/ ) ) {
$ret['error'] = __( 'Permission error', 'wpfm' );
} else {
$post_type = isset( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : '';
$taxonomy = isset( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : '';
$current_selected = isset( $_REQUEST['current_selected'] ) ? $_REQUEST['current_selected'] : '';
if ( ! $post_type || ! $taxonomy ) {
$ret['error'] = __( 'Params error', 'wpfm' );
} else {
global $wpdb;
$sql = $wpdb->prepare( 'SELECT t.slug FROM ' . $wpdb->terms . ' t'
. ' JOIN ' . $wpdb->term_taxonomy . ' AS tt ON tt.term_id = t.term_id'
. ' JOIN ' . $wpdb->term_relationships . ' AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id'
. ' JOIN ' . $wpdb->posts . ' AS p ON p.ID = tr.object_id'
. ' WHERE tt.taxonomy = %s AND p.post_type = %s AND p.post_status = %s'
. ' GROUP BY t.slug'
, $taxonomy, $post_type, 'publish' );
$include = $wpdb->get_col($sql);
$ret['html'] = preg_replace( '/<\/?select[^>]*>/', '', my_dropdown_categories( $taxonomy, $current_selected, $include ) );
}
}
wp_send_json( $ret );
}
And for the page with the dropdowns, I have:
<form action="<?php bloginfo('url'); ?>" method="get">
<?php
//$post_type = get_post_type_object( get_post_type($post) ); // GET SINGULAR NAME
$args = array(
'public' => true,
'_builtin' => false
);
$output = 'objects'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types($args, $output, $operator);
ksort($post_types);
echo '<select data-placeholder="I need a:" class="fade-in three selectboxSingle" name="post_type">';
foreach ( $post_types as $post_type ) {
$exclude = array('competition');
if(TRUE === in_array($post_type->name,$exclude)){
continue;
}
// Note: I think you need to use query_var here, rather than slug.
echo '<option value="'.$post_type->query_var.'">' . ucfirst($post_type->labels->singular_name) . '</option>';
}
echo "</select>";
?>
<?php
// Set your custom taxonomy
$taxonomy = "location";
// Factored out taxonomy dropdown into function my_dropdown_categories() in "functions.php".
echo my_dropdown_categories( $taxonomy );
?>
<div class="submit-button-blanket">
<button class="submit-button alt" type="submit">Search</button>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
var xhr;
var select_cpt, $select_cpt;
var select_location, $select_location;
$select_cpt = $(".selectboxSingle").selectize({
onChange: function(value) {
if (!value.length) return;
select_location.disable();
select_location.clearOptions();
select_location.load(function(callback) {
xhr && xhr.abort();
xhr = $.ajax({
url: "<?php echo admin_url('admin-ajax.php'); ?>",
success: function(results) {
select_location.enable();
callback(results);
},
error: function() {
callback();
}
})
});
}
});
$select_location = $('#location').selectize({
valueField: 'name',
labelField: 'name',
searchField: ['name']
});
select_location = $select_location[0].selectize;
select_cpt = $select_cpt[0].selectize;
select_location.disable();
});
I've gone a bit out of my depth here now, and any guidance would be greatly appreciated!!
Related
Based on Get product custom attributes to display them in WooCommerce product loop answer code.
I am displaying specific product attributes on the single product page with this:
add_action('woocommerce_single_product_summary', 'display_custom_attributes', 36 );
function display_custom_attributes() {
global $product;
$attributes_names = array('Brand', 'Color', 'Size');
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
if ( ! empty($attributes_data) ) {
echo '<h4>Details</h4><ul><li>' . implode( '</li><li>', $attributes_data ) . '</ul>';
}
Now I also need to add two custom meta fields ('Serial_Number' and 'MPN') to this list.
How can I add these?
To add two custom meta fields:
Serial_Number
MPN
to this list, you can use:
function action_woocommerce_single_product_summary() {
global $product;
$attributes_names = array( 'Brand', 'Color', 'Size' );
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
// NOT empty
if ( ! empty($attributes_data) ) {
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data );
}
// Get meta
$sn = $product->get_meta( 'Serial_Number' );
$mpn = $product->get_meta( 'MPN' );
// NOT empty
if ( ! empty ( $sn ) ) {
echo '<li>' . __( 'My label 1: ', 'woocommerce' ) . $sn . '</li>';
}
// NOT empty
if ( ! empty ( $mpn ) ) {
echo '<li>' . __( 'My label 2: ', 'woocommerce' ) . $mpn . '</li>';
}
echo '</ul>';
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 36, 0 );
Add this code on the function.php theme file
function call_product_meta_data()
{
global $post;
if ($post) {
$post_meta_value = get_post_meta($post->ID, 'post_meta_key', true);
if ($post_meta_value != null) {
echo "<div class='pr-meta'><p><strong>Meta Title</strong>: $post_meta_value</p></div>";
} else {
echo "<div class='pr-meta'><p><strong>Meta Title</strong>: N/A</p></div>";
}
}
}
add_action('woocommerce_single_variation', 'call_product_meta_data');
I have done a change in the class-wc-product-cat-list-walker.php file and following is the changes in the code
public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
$cat_id = intval( $cat->term_id );
$output .= '<li class="cat-item cat-item-' . $cat_id;
if ( $args['current_category'] === $cat_id ) {
$output .= ' current-cat';
}
if ( $args['has_children'] && $args['hierarchical'] && ( empty( $args['max_depth'] ) || $args['max_depth'] > $depth + 1 ) ) {
$output .= ' cat-parent';
}
if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat_id, $args['current_category_ancestors'], true ) ) {
$output .= ' current-cat-parent';
}
$pageurl = wp_get_referer();
$template = basename($pageurl);
$post_id = get_the_ID();
$catpage = get_field( 'catelog_page', $post_id );
$catalogpage = 'no';
if (isset($_GET['catalog'])) {
$catalogpage = 'yes';
}
if ( $catpage == TRUE || $catalogpage=='yes' ) {
$output .= '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '';
}
else {
$output .= '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '';
}
if ( $args['show_count'] ) {
$output .= ' <span class="count">(' . $cat->count . ')</span>';
}
}
I want to keep it in the child theme and make it functional. Is there any way? I have tried to copy the file and paste it in the child theme WooCommerce folder. but it is not working.
please help me to know the way to change it. Thanks in advance.
You can create your own custom class and try to override WC_Product_Cat_List_Walker.
So you can do something like this
class Custom_WC_Product_Cat_List_Walker extends WC_Product_Cat_List_Walker {
/**
* What the class handles.
*
* #var string
*/
public $tree_type = 'product_cat';
/**
* DB fields to use.
*
* #var array
*/
public $db_fields = array(
'parent' => 'parent',
'id' => 'term_id',
'slug' => 'slug',
);
public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
$cat_id = intval( $cat->term_id );
$output .= '<li class="cat-item cat-item-' . $cat_id;
if ( $args['current_category'] === $cat_id ) {
$output .= ' current-cat';
}
if ( $args['has_children'] && $args['hierarchical'] && ( empty( $args['max_depth'] ) || $args['max_depth'] > $depth + 1 ) ) {
$output .= ' cat-parent';
}
if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat_id, $args['current_category_ancestors'], true ) ) {
$output .= ' current-cat-parent';
}
$pageurl = wp_get_referer();
$template = basename($pageurl);
$post_id = get_the_ID();
$catpage = get_field( 'catelog_page', $post_id );
$catalogpage = 'no';
if (isset($_GET['catalog'])) {
$catalogpage = 'yes';
}
if ( $catpage == TRUE || $catalogpage=='yes' ) {
$output .= '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '';
}
else {
$output .= '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '';
}
if ( $args['show_count'] ) {
$output .= ' <span class="count">(' . $cat->count . ')</span>';
}
}
}
Then you can call that function like this
add_filter('woocommerce_product_categories_widget_args', 'custom_product_categories_widget_args', 10, 1);
function custom_product_categories_widget_args($args) {
$args['walker'] = new Custom_WC_Product_Cat_List_Walker;
return $args;
}
Just use this code in your active theme functions.php file and check.
This code shows how to display custom product attributes for without variations. I wish to display all the variations too in the typical dropdown select menu. How do I do that?
P/S: I can't post the code here because this site says I have too much code and too little details.
UPDATED:
/**
* Show all product attributes on the product page
*/
function isa_woocommerce_all_pa(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '';
foreach ( $attributes as $attribute ) {
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ($tax_object->labels->singular_name) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= $tax_label . ': ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
array_push( $tax_terms, $single_term );
}
$out .= implode(', ', $tax_terms) . '<br />';
} else {
$out .= $name . ': ';
$out .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
}
}
echo $out;
}
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);
I have a premium WordPress theme which has breadcrumbs by default, but it does not show Category (and Sub-Category) item when on post page: Home > Category > Sub-Category > Post Title
Instead it just displays like this: Home > Post Title
This is the function which handles breadcrumbs:
(I marked the part of the code that I think is important with this sign, to save you time: ==========>>)
// BREADCRUMBS
if ( ! function_exists( 'ishyoboy_get_breadcrumbs' ) ) {
function ishyoboy_get_breadcrumbs() {
global $ish_options, $ish_woo_id;
$return = '';
$return .= '<div class="ish-pb-breadcrumbs"><div><div>' . "\n";
if ( ! is_front_page() ) {
if ( function_exists('is_woocommerce') ) {
if ( !is_woocommerce() && !is_woocommerce_page() ){
$return .= '<a class="ish-pb-breadcrumbs-home" href="';
$return .= home_url();
$return .= '">';
$return .= '<span>' . __( 'Home', 'ishyoboy' ) . '</span>';
$return .= "</a> > ";
}
}
else{
$return .= '<a class="ish-pb-breadcrumbs-home" href="';
$return .= home_url();
$return .= '">';
$return .= '<span>' . __( 'Home', 'ishyoboy' ) . '</span>';
$return .= "</a> > ";
}
}
else {
$return .= '<span class="ish-pb-breadcrumbs-home">';
$return .= '<span>' . __( 'Home', 'ishyoboy' ) . '</span>';
$return .= "</span>";
}
if ( !is_front_page() && is_home() ) {
global $page;
$return .= $page->post_title;
}
if ( is_archive() && 'post' == get_post_type() && ! is_category() && ( !function_exists('is_woocommerce') || !is_woocommerce() ) ) {
$hpage = get_page( get_option( 'page_for_posts' ) );
if ( 'page' == get_option('show_on_front') && isset($hpage) && '' != $hpage ){
$return .= get_page_parents($hpage->ID, TRUE, ' > ', FALSE );
}
if ( is_day() ) :
$return .= sprintf( __( 'Daily Archives: %s', 'ishyoboy' ), '<span>' . get_the_date() . '</span>' );
elseif ( is_month() ) :
$return .= sprintf( __( 'Monthly Archives: %s', 'ishyoboy' ), '<span>' . get_the_date( _x( 'F Y', 'monthly archives date format', 'ishyoboy' ) ) . '</span>' );
elseif ( is_year() ) :
$return .= sprintf( __( 'Yearly Archives: %s', 'ishyoboy' ), '<span>' . get_the_date( _x( 'Y', 'yearly archives date format', 'ishyoboy' ) ) . '</span>' );
else :
$return .= __( 'Archives', 'ishyoboy' );
endif;
}
else if ( is_archive() && 'post' != get_post_type() && ( !function_exists('is_woocommerce') || !is_woocommerce() ) ) {
$type = get_post_type( get_the_ID() );
$obj = get_post_type_object( $type );
if ( is_object( $obj ) ){
$return .= $obj->labels->name;
}
}
if ( (is_category() || is_single()) && ( ( !function_exists('is_woocommerce_page') || !function_exists('is_woocommerce' ) ) || ( !is_woocommerce() && !is_woocommerce_page() ) ) ) {
$post_id = ish_get_the_ID();
$post_type = get_post_type();
switch ($post_type){
case 'portfolio-post' :
$terms = get_the_terms($post_id, 'portfolio-category' );
$term = ( ! empty( $terms ) ) ? array_pop($terms) : '';
if (isset($ish_options['page_for_custom_post_type_portfolio-post']) && '-1' != $ish_options['page_for_custom_post_type_portfolio-post']){
$portfolio_page = get_page($ish_options['page_for_custom_post_type_portfolio-post']);
$separator = " > ";
$return .= ''.$portfolio_page->post_title.'' . $separator;
}
if ( ! empty( $term ) ){
$return .= get_term_parents($term->term_id, 'portfolio-category', TRUE, ' > ', FALSE );
}
break;
// from here ==========>>
case 'post' :
if ( is_category() ){
global $cat;
$category = get_category($cat);
if ( $category->parent && ( $category->parent != $category->term_id ) ){
$return .= get_category_parents($category->parent, TRUE, ' > ', FALSE );
}
$return .= single_cat_title('', false);
}
else{
$category = get_the_category();
if ( is_object( $category ) ){
$ID = $category[0]->cat_ID;
$return .= get_category_parents($ID, TRUE, ' > ', FALSE );
}
}
break;
// to here ==========>>
default :
$type = get_post_type( get_the_ID() );
$obj = get_post_type_object( $type );
if ( is_object( $obj ) ){
$return .= '' . $obj->labels->name . ' > ';
}
}
}
else if ( ( function_exists('is_woocommerce_page') && function_exists('is_woocommerce') ) && (is_woocommerce() || is_woocommerce_page() )){
ob_start();
woocommerce_breadcrumb(array(
'delimiter' => ' > ',
'wrap_before' => '',
'wrap_after' => '',
'before' => '',
'after' => '',
'home' => '<span class="ish-pb-breadcrumbs-home"><span>' . _x( 'Home', 'breadcrumb', 'woocommerce' ) . '</span></span>',
));
$return .= ob_get_contents();
ob_end_clean();
ob_end_flush();
}
else if (is_tax()){
if (is_tax('portfolio-category')){
$current_term = get_queried_object();
if ( !empty($current_term) ){
//var_dump($current_term);
if (isset($ish_options['page_for_custom_post_type_portfolio-post']) && '-1' != $ish_options['page_for_custom_post_type_portfolio-post']){
$portfolio_page = get_page($ish_options['page_for_custom_post_type_portfolio-post']);
$separator = " > ";
$return .= ''.$portfolio_page->post_title.'' . $separator;
}
if ($current_term->parent != 0 ){
$return .= get_term_parents($current_term->parent, 'portfolio-category', TRUE, ' > ', FALSE );
}
$return .= $current_term->name;
}
}
}
else if (is_page()){
global $post;
if ($post->post_parent != 0 ){
$return .= get_page_parents($post->post_parent, TRUE, ' > ', FALSE );
}
}
if (!function_exists('is_woocommerce_page') || !is_woocommerce_page()){
if(is_single()) {$return .= get_the_title();}
if(is_page()) {
global $post;
$frontpage = get_option('page_on_front');
if ( $frontpage && $frontpage == $post->ID){
/*$return .= '<a class="home" href="';
$return .= home_url();
$return .= '">';
$return .= '<span>' . __( 'Home', 'ishyoboy' ) . '</span>';
$return .= "</a>";*/
} else {
$return .= get_the_title();
}
}
if(is_tag()){ $return .= __( 'Tag: ', 'ishyoboy' ) . single_tag_title('',FALSE); }
if(is_404()){ $return .= __( '404 - Page not Found', 'ishyoboy' ); }
if(is_search()){ $return .= __( 'Search', 'ishyoboy' ); }
if(is_year()){ $return .= get_the_time('Y'); };
}
$return .= '</div></div></div>';
return $return;
}
}
if ( !function_exists('the_post_thumbnail_caption') ) {
function the_post_thumbnail_caption(){
$thumb = get_post_thumbnail_id();
if ( ! empty( $thumb ) ){
$thumb_object = get_post( $thumb );
if ( ! empty( $thumb_object ) ) {
echo '<div class="wp-caption"><p class="wp-caption-text">' . $thumb_object->post_excerpt . '</p></div>';
}
}
}
}
if ( !function_exists('get_the_post_thumbnail_caption') ) {
function get_the_post_thumbnail_caption(){
$thumb = get_post_thumbnail_id();
if ( ! empty( $thumb ) )
return get_post( $thumb )->post_excerpt;
return null;
}
}
if ( ! function_exists( 'ishyoboy_activate_fancybox_on_blog_single' ) ) {
function ishyoboy_activate_fancybox_on_blog_single() {
if ( is_singular() && !is_singular( 'product' ) ){
?>
<script type="text/javascript">
jQuery(document).ready(function($){
var thumbnails = jQuery("a:has(img)").not(".nolightbox").filter( function() { return /\.(jpe?g|png|gif|bmp)$/i.test(jQuery(this).attr('href')) });
if ( thumbnails.length > 0){
thumbnails.addClass( 'openfancybox-image' ).attr( 'rel', 'fancybox-post-image-<?php the_ID() ?>');
}
});
</script>
<?php
}
}
}
add_action( 'wp_head', 'ishyoboy_activate_fancybox_on_blog_single' );
if ( ! function_exists( 'ishyoboy_dummy_functions' ) ) {
function ishyoboy_dummy_functions() {
if ( false ) {
posts_nav_link();
wp_link_pages();
$args = '';
add_theme_support( 'custom-header', $args );
add_theme_support( 'custom-background', $args );
}
}
}
And then I've made a change in code which generated the desired change - Category appeared in breadcrumbs: Home > Category > Sub-Category > Post Title
But on the Category archive page breadcrumbs look like this: Home > Category > Category
Live example: http://pigtelligent.com/science/
The change I've made in above-mentioned code:
case 'post' :
$terms = get_the_terms($post_id, 'category' );
$term = ( ! empty( $terms ) ) ? array_pop($terms) : '';
if ( ! empty( $term ) ){
$return .= get_term_parents($term->term_id, 'category', TRUE, ' > ', FALSE );
}
I know that for someone smarter this should be easy, but I've spent whole day trying to figure this out, so can you please help me? :)
Categories can get tricky fast as posts can have multiple categories, and category landing pages can access a lot of meta which can get misleading. Make sure you've reviewed the Template Hierarchy page on the codex and you've got a firm understanding of how WP handles taxonomy.
Use What The File to find out what template you're actually on as you might not be on a category page, causing is_category() to return false. You could be on a single (as the if statement wrapping the logic you pointed out is checking is_category() || is_single()), or you might need to add a check for is_archive().
My best suggestion is to figure out if you're on a category page first, then stop all that extra logic and just spit out the category's name. Not too much help, I know, but hopefully that'll point you in the right direction
function the_breadcrumb() {
global $post;
echo '<ul id="breadcrumbs">';
if (!is_home()) {
echo '<li><a href="';
echo get_option('home');
echo '">';
echo 'Home';
echo '</a></li><li class="separator"> / </li>';
if (is_category() || is_single()) {
echo '<li>';
the_category(' </li><li class="separator"> / </li><li> ');
if (is_single()) {
echo '</li><li class="separator"> / </li><li>';
the_title();
echo '</li>';
}
} elseif (is_page()) {
if($post->post_parent){
$anc = get_post_ancestors( $post->ID );
$title = get_the_title();
foreach ( $anc as $ancestor ) {
$output = '<li>'.get_the_title($ancestor).'</li> <li class="separator">/</li>';
}
echo $output;
echo '<strong title="'.$title.'"> '.$title.'</strong>';
} else {
echo '<li><strong> '.get_the_title().'</strong></li>';
}
}
}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
echo '</ul>';
}
?>
Get using: <?php the_breadcrumb(); ?>
I am getting an out of the box error for a plugin and I am wondering if there is a quick fix.
I have googled the issue and checked the creator of the plugin's website. No help.
The plugin needs:
<?php do_shortcode('[print_wp_footer]'); ?>
Added to Footer.php for the theme.
WordPress then gives an error of:
Warning: Invalid argument supplied for foreach() in /.../wp-content/plugins/wp-footer-menu/main.php on line 192
Here is the line by itself
foreach ($values as $attr=>$val) {
Here is the section of code with line "192" with arrows.
<?php
}
function wp_footer_menu_confirm_delete ($delete) {
$base_uri = wp_footer_menu_base_uri();
// Find out about this menu item.
$menu_items = get_option ( 'footer_menu_links' );
$item_to_delete = explode( ',', $menu_items[$delete] );
$item_title = $item_to_delete[0];
$item_address = $item_to_delete[1];
// Create form for user to confirm option.
echo '<h3>Confirm Delete</h3>';
echo '<p>Are you sure you want to delete this menu item?</p>';
echo '<p>Title: ' . $item_title . '</p>' ;
echo '<p>Address: ' . $item_address . '</p>';
echo '<form method="post" action="' . $base_uri . '">';
echo '<input type="hidden" name="delete_key" value="' . $delete . '" />';
echo wp_nonce_field( 'confirm_delete', 'delete' );
echo '<input type="submit" class="button-primary" value="Delete item" />';
echo '</form>';
}
function wp_footer_menu_process() {
if ( isset( $_GET[ 'delete' ] ) ) {
$nonce = $_GET ['nonce'];
if ( wp_verify_nonce( $nonce, 'footer_delete' ) ) {
wp_footer_menu_confirm_delete ( $_GET[ 'delete' ] );
}
return 0;
} else if ( isset( $_POST[ 'delete_key' ] ) && check_admin_referer ( 'confirm_delete', 'delete' ) ) {
$menu_items = get_option ( 'footer_menu_links' );
$key = $_POST['delete_key'];
unset ( $menu_items[$key] );
update_option ( 'footer_menu_links', $menu_items );
}
if ( isset( $_POST[ 'link_title' ] ) && check_admin_referer( 'footer_menu', 'add' ) ) {
$link_title = $_POST ['link_title'];
$link_address = $_POST ['link_address'];
$link_order = $_POST ['link_order'];
$new_link = $link_title . ',' . $link_address . ',' . $link_order;
$footer_links = get_option( 'footer_menu_links' );
if ($footer_links == '') {
$footer_links = array();
}
$new_links = array_push( $footer_links, $new_link );
update_option ( 'footer_menu_links', $footer_links );
}
if ( isset( $_POST[ 'font-size' ] ) && check_admin_referer( 'footer_menu', 'save' ) ) {
if (empty($_POST['auto-footer'])) {
$_POST['auto-footer'] = 'no';
}
if (empty($_POST['auto-sticky'])) {
$_POST['auto-sticky'] = 'no';
}
update_option('wp_footer_values', $_POST);
echo '<div class="wp_footer_info" style="margin:0 auto;margin-top:5px;text-align:center;">Customizations Saved</div>';
}
return 1;
}
function wp_footer_print_menu() {
$menu = wp_footer_get_menu();
$values = get_option('wp_footer_values');
--192--> foreach ($values as $attr=>$val) {
$menu = str_replace('%' . $attr . '%', stripslashes($val), $menu);
}
echo $menu;
if ($values['auto-sticky'] == 'yes') {
?>
<style type="text/css">
.wp_footer_sticky {
position:fixed;
bottom: 0;
width: 100%;
}
</style>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#wp_footer_menu').addClass('wp_footer_sticky');
});
</script>
<?php
It seems to depend to returned value of the following code :
$values = get_option('wp_footer_values');
Depending on the WordPress codex, if the option does not exist it return a boolean FALSE. To you will need to verify if that the $values variable is not empty. Check with the following code.
function wp_footer_print_menu() {
$menu = wp_footer_get_menu();
$values = get_option('wp_footer_values');
if (!empty($values)) { // <-- verify it's not empty
foreach ($values as $attr=>$val) {
$menu = str_replace('%' . $attr . '%', stripslashes($val), $menu);
}
echo $menu;
// [...}
} // <-- don't forget to close the if statement just after the end of the foreach statement
Hope that help.
Source : get_option()