WordPress Plugin Footer Menu Error - php

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()

Related

Validating custom checkout fields in admin edit order form

I'm trying to add validation to some checkout fields when editing an order from admin panel, but I haven't found the hook for that. I want to prevent saving post if validation fails and also show a notice. I've tried woocommerce_process_shop_order_meta hook but post is always saved and notices are not shown.
This is the code I used in my functions.php file:
$meta_box_errors[] = array();
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );
public function save_order_custom_field_meta_data( $post_id, $post ){
$field_name = 'billing_nif';
$nif_value = '';
if( isset( $_POST[ '_' . $field_name ] ) && !empty($_POST[ '_' . $field_name ]) ) {
$nif_value = sanitize_text_field( $_POST[ '_' . $field_name ] );
if (!empty ($nif_value)) {
if (! validate_nif( $nif_value )) {
$meta_box_errors["invalid_nif"] = 'Invalid NIF';
}
} else {
$meta_box_errors["empty_nif"] = 'Empty NIF';
}
} else {
$meta_box_errors["empty_nif"] = 'Empty NIF';
}
if( ! empty( $this->meta_box_errors ) ) {
add_action( 'admin_notices', 'output_errors' );
}
}
public function validate_nif( $nif ){
$nif = strtoupper($nif);
if (strlen($nif) != 9) {
return false;
}
$nifExp = "^((\d{8})([A-Z]{1}))$^";
return preg_match($nifExp, $nif));
}
public function output_errors() {
echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
foreach ( $meta_box_errors as $error ) {
echo '<p>' . wp_kses_post( $error ) . '</p>';
}
echo '</div>';
unset($meta_box_errors);
}

Wordpress: invoke function in a page / post

I rephrase my question in this new post to which I have not had any answer. These two functions create a page on the user's account endpoint. A table is published on this page. I would like this table to be published in a new page / post of the site accessible to all. Any suggestions? Thank you
1 add_action( 'init', array(__CLASS__, 'wsc_add_my_account_endpoint') );
2 public static function wsc_add_my_account_endpoint() { add_rewrite_endpoint( 'wsc-share-cart', EP_ROOT | EP_PAGES ); }
3 The content that I would to print in a wordpress page:
if ( !isset($_GET['wc-wsc']) || !isset($_GET['nounce']) ) {
return;
}
if ( ! wp_verify_nonce( wc_clean($_GET['nounce']), 'wsc_save_share_cart' ) ) {
return;
}
$posted_fields = wc_clean($_POST);
$fields = get_option('wsc_form_fields');
$errors = array();
$form_submission = array();
$form_submission_html = '';
$customer_email = '';
if ( !empty($fields) ) {
$form_submission_html .= '<table>';
foreach ( $fields as $key => $field ) {
if ( true == $field['required'] && empty($posted_fields['wsc_form_field_' . esc_attr($key)]) ) {
$errors['wsc_form_field_' . esc_attr($key)] = wp_sprintf( '%s %s', esc_html__($field['label'], 'wc-wsc'), esc_html__('is required.', 'wc-wsc') );
}
$label = !empty($field['label']) ? $field['label'] : 'wsc_form_field_' . esc_attr($key);
$field_value = wc_clean($posted_fields['wsc_form_field_' . esc_attr($key)]);
if ( 'email' == $field['type'] ) {
if ( !is_email($field_value) ) {
$errors['wsc_form_field_' . esc_attr($key)] = wp_sprintf( '%s %s', esc_html__($field['label'], 'wc-wsc'), esc_html__('must be an email.', 'wc-wsc') );
}
$customer_email = $field_value;
}
if ( isset($posted_fields['wsc_form_field_' . esc_attr($key)]) ) {
$form_submission[$label] = $field_value;
}
$form_submission_html .= '<tr><th>' . esc_html($label) . '</th><td>' . esc_html($field_value) . '</td></tr>';
}
$form_submission_html .= '</table>';
} ```

Connecting a link to an image

I am using MediaWiki in combination with Joomla. As I want to add icons to some links I need to connect both. I know that this is possible through putting the img tag inside the a tag.
BUT the problem is that some links are generated throgh a function called makeListItem that MediaWiki uses for more than just these links. Now my question is, can I somehow connect the img to the a without putting it inside the a tag?
This calls the function to create the elements:
<?php $this->renderNavigation( 'PERSONAL' ); ?>
The actual function (shortened):
foreach ( $personalTools as $key => $item ) {
?>
<div class="searchbox" style="clear:both;">
<img src="<?php echo $icon[$key] ?>" alt="p-Icons" class="iconnav"/>
<?php
echo $this->makeListItem( $key, $item );
?>
</div>
<?php
}
?>
The src of the image is defined in a array that is declared just above the foreach.
Thanks in advance
You have to modify your makeListItem() function (BaseTemplate class).
function makeListItem( $key, $item, $options = array() ) {
if ( isset( $item['links'] ) ) {
$links = array();
foreach ( $item['links'] as $linkKey => $link ) {
$links[] = $this->makeLink( $linkKey, $link, $options );
}
$html = implode( ' ', $links );
} else {
$link = $item;
// These keys are used by makeListItem and shouldn't be passed on to the link
foreach ( array( 'id', 'class', 'active', 'tag', 'itemtitle' ) as $k ) {
unset( $link[$k] );
}
if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
// The id goes on the <li> not on the <a> for single links
// but makeSidebarLink still needs to know what id to use when
// generating tooltips and accesskeys.
$link['single-id'] = $item['id'];
}
$html = $this->makeLink( $key, $link, $options );
}
$attrs = array();
foreach ( array( 'id', 'class' ) as $attr ) {
if ( isset( $item[$attr] ) ) {
$attrs[$attr] = $item[$attr];
}
}
if ( isset( $item['active'] ) && $item['active'] ) {
if ( !isset( $attrs['class'] ) ) {
$attrs['class'] = '';
}
$attrs['class'] .= ' active';
$attrs['class'] = trim( $attrs['class'] );
}
if ( isset( $item['itemtitle'] ) ) {
$attrs['title'] = $item['itemtitle'];
}
return Html::rawElement( isset( $options['tag'] ) ? $options['tag'] : 'li', $attrs, $html );
}

Populate second dropdown using Selectize

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!!

How do I make default settings work for select fields with WordPress API?

std is the default value of an option if it is not set. I only need the default value for the select fields. I've tried many things but nothing seems to work. I really need help on this. I don't know where to start.
So here, I do my function to add the setting to my options page.
function nl_register_function_name() {
// Validation callback
register_setting( 'nl_theme_options', 'nl_theme_options', 'nl_validate_settings' );
// Add setting to section
add_settings_section( 'nl_styling_section', 'Styling', 'nl_display_styling_section', 'nl_theme_options.php' );
// Create selection field
$field_args = array(
'type' => 'select',
'id' => 'nl_function_name',
'name' => 'nl_function_name',
'desc' => 'Select border bottom size.',
'std' => 'Small',
'label_for' => 'nl_function_name',
'items' => array(
"None",
"Small",
"Medium",
"Large"
)
);
// Label
add_settings_field( 'label_function_name', 'function_name', 'nl_display_setting', 'nl_theme_options.php', 'nl_styling_section', $field_args );
}
// Registers the setting
add_action( 'admin_init', 'nl_register_function_name' );
And here, I create the HTML for the select field.
function nl_display_setting ( $args ) {
extract( $args );
$option_name = 'nl_theme_options';
$options = get_option( $option_name );
switch ( $args['type'] ) {
case 'select':
if( isset( $options[$id] ) ) {
$options[$id] = stripslashes( $options[$id] );
$options[$id] = esc_attr( $options[$id] );
echo "<select name='" . $option_name . "[$id]' value='$options[$id]'>";
foreach( $items as $item ) {
$selected = ($options[$id]==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
} elseif ( !isset( $options[$id] ) ) {
$std = $args['std'];
echo "<select name='" . $option_name . "[$id]' value='$std'>";
foreach( $items as $item ) {
echo "<option value='$item'>$item</option>";
}
}
echo "</select>";
echo ($desc != '') ? "<br><span class='description'>$desc</span>" : "";
break;
}
}
How do I make default settings work for select fields with WordPress API?
I fixed it! I'm going to post the solution here for others that might have the same problem in the future because there doesn't seem to be any solution online.
case 'select':
if( isset( $options[$id] ) ) {
$options[$id] = stripslashes( $options[$id] );
$options[$id] = esc_attr( $options[$id] );
echo "<select name='" . $option_name . "[$id]' value='$options[$id]'>";
foreach( $items as $item ) {
$selected = ($options[$id]==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
} elseif ( !isset( $options[$id] ) ) {
echo "<select name='" . $option_name . "[$id]' value='$std'>";
foreach( $items as $item ) {
$selected = ($std==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
}
echo "</select>";
echo ($desc != '') ? "<br><span class='description'>$desc</span>" : "";
break;

Categories