I am running a PHP script, and keep getting errors like:
Notice: Undefined variable: key in D:\0-MYBLOG\SERVER-MYBLOG\InstantWP_4.3.1\iwpserver\htdocs\wordpress\wp-content\plugins\sama-author-review\admin\metabox.php on line 310
Line 310 looks like this:
unset( $new['items_review'][$key] );
Here the code:
if ( $new['items_review'] && is_array( $new['items_review'] )) {
foreach ( $new['items_review'] as $review ) {
if ( empty( $review['label'] ) ) {
unset( $new['items_review'][$key] );
} else {
$review['value'] = absint( $review['value'] );
$review['label'] = esc_attr( $review['label'] );
if ( empty( $review['slug'] )) {
$review['slug'] = sanitize_title( $review['label'] );
} else {
$review['slug'] = sanitize_title( $review['slug'] );
}
$review['style'] = esc_attr( $review['style'] );
$items_review[$i] = $review;
$i++;
}
}
}
Is there a quick fix to resolve these error?
Really appreciate for any help
Thank you
I think I get what the problem is, you have forgotten to put the key parameter on your foreach statement.
I admit its a bit of a guess as I have no idea what is in $new but it seams a logical possibility.
if ( $new['items_review'] && is_array( $new['items_review'] )) {
// foreach ( $new['items_review'] as $review ) {
// replace foreach with this line
foreach ( $new['items_review'] as $key => $review ) {
if ( empty( $review['label'] ) ) {
unset( $new['items_review'][$key] );
} else {
$review['value'] = absint( $review['value'] );
$review['label'] = esc_attr( $review['label'] );
if ( empty( $review['slug'] )) {
$review['slug'] = sanitize_title( $review['label'] );
} else {
$review['slug'] = sanitize_title( $review['slug'] );
}
$review['style'] = esc_attr( $review['style'] );
$items_review[$i] = $review;
$i++;
}
}
}
Related
Getting a PHP warning for this section of code, whilst running PHP 8:
function wpsc_body_class( $classes ) {
global $wp_query, $wpsc_query;
$post_id = get_the_ID();
if ( $post_id ) {
$page_url = get_permalink( $post_id );
// If on a product or category page...
if ( get_option( 'product_list_url' ) == $page_url || get_post_type( $post_id ) === 'wpsc-product' ) {
$classes[] = 'wp-e-commerce';
if ( ! is_array( $wpsc_query->query ) ) {
$classes[] = 'wpsc-home';
}
if ( wpsc_is_single_product() ) {
$object = $wp_query->get_queried_object();
$classes[] = 'wpsc-single-product';
if ( absint( $object->ID ) > 0 ) {
$classes[] = 'wpsc-single-product-' . absint( $object->ID );
}
}
if ( wpsc_is_in_category() && ! wpsc_is_single_product() ) {
$classes[] = 'wpsc-category';
$tax_object = $wp_query->get_queried_object();
$classes[] = 'wpsc-category-' . esc_attr( $tax_object->slug );
}
if ( wpsc_is_in_tag() && ! wpsc_is_single_product() ) {
$classes[] = 'wpsc-tag';
$tax_object = $wp_query->get_queried_object();
$classes[] = 'wpsc-tag-' . esc_attr( $tax_object->slug );
}
}
$page_url = set_url_scheme( $page_url, 'relative' );
// If viewing the shopping cart...
if ( set_url_scheme( get_option( 'shopping_cart_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-shopping-cart';
}
// If viewing the transaction...
if ( set_url_scheme( get_option( 'transact_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-transaction-details';
}
// If viewing your account...
if ( set_url_scheme( get_option( 'user_account_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-user-account';
}
}
return $classes;
}
Error pointing to this line of code:
if ( ! is_array( $wpsc_query->query ) ) {
Looking for the best way of resolving this, and changing this code, to overcome the php 8 warning for attempt to read property "query" on null.
You should check if the query method exists in the first place and add that check to your if statement like so:
Edit: updated answer to check for null.
$queryMethodExists = method_exists($wpsc_query, 'query');
if (
$wpsc_query === null
|| ($queryMethodExists && ! is_array( $wpsc_query->query ))
) {
$classes[] = 'wpsc-home';
}
using PHP 7.4.21, wordpress core5.7.2. Found the two pieces of code the deprecation warning mentions, but why is there an issue and how to fix it?
The issue is with this line of code in the below lines of code:
$value = call_user_func_array( $the_['function'], $args );
Here's the snippet from class-wp-hook.php on line 292....
// Avoid the array_slice() if possible.
if ( 0 == $the_['accepted_args'] ) {
$value = call_user_func( $the_['function'] );
} elseif ( $the_['accepted_args'] >= $num_args ) {
$value = call_user_func_array( $the_['function'], $args );
} else {
$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
}
The RestrictAllowedPlugins plugin's filter function of issue is here:
if( !class_exists('RestrictAllowedPlugins') ) {
class RestrictAllowedPlugins {
function filter($plugins) {
if( current_user_can( 'manage_network_plugins' ) ) // Disable restrictions for network admins
return $plugins;
$allowed_plugins = get_site_option( 'allowedplugins' );
if( !is_array( $allowed_plugins ) )
return $plugins;
unset( $plugins[ plugin_basename(__FILE__) ] );
foreach( array_keys($plugins) as $key ) {
$plugin_key = str_replace( '/', ':', $key );
if( !isset( $allowed_plugins[ $plugin_key ] ) )
unset( $plugins[ $key ] );
}
return $plugins;
}
This is a filter with the callback:
add_filter( 'all_plugins', array('RestrictAllowedPlugins','filter') );
I use the following code to the database:
$attribute_R_0 = wc_get_product( $post_id );
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
but I want to use a condition to avoid creating a blank table in db, which one is correct? A or B?
A:
if ('custom_text_field_title_related') {
$attribute_R_0 = wc_get_product( $post_id );
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
}
B:
$attribute_R_0 = wc_get_product( $post_id );
$nui_1 = $attribute_14->get_meta( 'custom_text_field_title_related' );
if ($nui_1 ){
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
}
None of them… To avoid a blank entry in database table, you should use instead:
if ( isset( $_POST['custom_text_field_title_related'] ) && ! empty( $_POST['custom_text_field_title_related'] ) ) {
$attribute_R_0 = wc_get_product( $post_id );
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $_POST['custom_text_field_title_related'] ) );
$attribute_R_0->save();
}
or better:
$field_key_R_0 = 'custom_text_field_title_related';
if ( isset( $_POST[$field_key_R_0] ) && ! empty( $_POST[$field_key_R_0] ) ) {
$attribute_R_0 = wc_get_product( $post_id );
$attribute_R_0->update_meta_data( $field_key_R_0, sanitize_text_field( $_POST[$field_key_R_0] ) );
$attribute_R_0->save();
}
I've successfully been able to create one custom metabox for WordPress post types using the following code:
function bandcamp_metabox(){
$posttypes = Array(
'discos',
'Projetos'
);
add_meta_box('bandcamp_link', 'bandcamp link', 'bandcamp_link_meta_callback', $posttypes, 'side');
}
function bandcamp_link_meta_callback( $post ) {
wp_nonce_field('save_bandcamp_link', 'bancamp_meta_box_nonce');
$value = get_post_meta($post->ID,'_bandcamp_link_value_key', true);
echo '<label for="bancamp_link_field">';
echo '<input type="url" id="bancamp_link_field" name="bancamp_link_field" value="' . esc_attr ( $value ) . '" size="25" />';
}
function save_bandcamp_link ( $post_id ) {
if( ! isset( $_POST['bancamp_meta_box_nonce'] ) ){
return;
}
if ( ! wp_verify_nonce( $_POST['bancamp_meta_box_nonce'], 'save_bandcamp_link' ) ){
return;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
if( ! isset( $_POST['bancamp_link_field'] ) ) {
return;
}
$bandcamp_link = sanitize_text_field( $_POST['bancamp_link_field'] );
update_post_meta($post_id, '_bandcamp_link_value_key', $bandcamp_link);
}
add_action('add_meta_boxes', 'bandcamp_metabox');
add_action('save_post', 'save_bandcamp_link');
I was hoping (still kind of a noob) that through a 'foreach' function I'd be able to re-use the code to create multiple metaboxes at once like this:
$varsocial = Array(
'bandcamp',
'soundcloud',
'facebook',
'instagram',);
foreach($varsocial as $sociallink) {
function social_metabox(){
$posttypes = Array(
'discos',
'Projetos');
add_meta_box(''. $sociallink .'_link', ''. $sociallink .' link', 'social_link_meta_callback', $posttypes, 'side');
}
function social_link_meta_callback( $post ) {
wp_nonce_field('save_social_link', 'social_meta_box_nonce');
$value = get_post_meta($post->ID,'_'. $sociallink .'_link_value_key', true);
echo '<label for="'. $sociallink .'_link_field">';
echo '<input type="url" id="'. $sociallink .'_link_field" name="'. $sociallink .'_link_field" value="' . esc_attr ( $value ) . '" size="25" />';
}
function save_social_link ( $post_id ) {
if( ! isset( $_POST['social_meta_box_nonce'] ) ){
return;
}
if ( ! wp_verify_nonce( $_POST['social_meta_box_nonce'], 'save_social_link' ) ){
return;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
if( ! isset( $_POST[''. $sociallink .'_link_field'] ) ) {
return;
}
$social_link = sanitize_text_field( $_POST[''. $sociallink .'_link_field'] );
update_post_meta($post_id, '_'. $sociallink .'_link_value_key', $social_link);}
return add_action('add_meta_boxes', 'social_metabox');
return add_action('save_post', 'save_social_link');
}
But such is not working. What am I doing wrong?
I'm having issues formatting the breadcrumbs on my staging site. I had them set up with the correct categories before, but the parent category kept displaying the wrong category (which I did fix, but...). The fix caused the "home" option to disappear. I don't know any php and have been trying without success to figure out how to add a "Home /" option on each page before the category. Below is the code I'm using now. Anyone have any ideas?
if ( ! empty( $breadcrumb ) ) {
echo $wrap_before;
if ( is_single() && get_post_type() == 'product' ) {
echo $prepend;
if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {
$referer = wp_get_referer();
$printed = array();
foreach( $terms as $term){
if(in_array($term->id, $printed)) continue;
$referer_slug = (strpos($referer, '/'.$term->slug.'/'));
if(!$referer_slug==false){
$printed[] = $term->id;
$category_name = $term->name;
$ancestors = get_ancestors( $term->term_id, 'product_cat' );
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor ) {
$ancestor = get_term( $ancestor, 'product_cat' );
if ( ! is_wp_error( $ancestor ) && $ancestor )
echo $before . '' . $ancestor->name . '' . $after . $delimiter;
}
echo $before . '' . $category_name . '' . $after . $delimiter;
}
}
}
echo $before . get_the_title() . $after;
} else {
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
echo esc_html( $crumb[0] );
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
echo $delimiter;
}
}
}
echo $wrap_after;
}
Thanks for the help in advance!
I think you can achieve this by changing the following line echo $wrap_before; to this:
echo $wrap_before.'Home »';