Add foreach to function - php

I am trying to edit a wordpress php file with the following function :
add_action('userpro_after_profile_head','userpro_sc_bar', 99);
function userpro_sc_bar( $args ) {
global $userpro, $userpro_social;
extract($args);
( display content buttons here )
}
I am trying to add a button to this function from a different plugin and I need to add the following global rules :
foreach ( $sellers['users'] as $seller ) {
$store_info = get_store_info( $seller->ID );
$store_url = get_store_url( $seller->ID );
}
When I add the two lines $store_info and $store_url to the global rules, it works, but I'm not getting the $seller ID that is defined with the foreach argument
When I add the whole thing into the global rules like this :
add_action('userpro_after_profile_head','userpro_sc_bar', 99);
function userpro_sc_bar( $args ) {
global $userpro, $userpro_social;
extract($args);
foreach ( $sellers['users'] as $seller ) {
$store_info = get_store_info( $seller->ID );
$store_url = get_store_url( $seller->ID );
}
( display content buttons here )
}
It is no longer working.
It there a proper to combine these 2 together ??

I am not too familiar with wordpress...
but could this be an issue with the $sellers variable?
I think the $sellers variable is not accessible from within your function.
Assuming $sellers is a global variable you could try:
add_action('userpro_after_profile_head','userpro_sc_bar', 99);
function userpro_sc_bar( $args ) {
global $userpro, $userpro_social, $sellers; // <-- Added $sellers
extract($args);
foreach ( $sellers['users'] as $seller ) {
$store_info = get_store_info( $seller->ID );
$store_url = get_store_url( $seller->ID );
}
( display content buttons here )
}

Ok, got is ,this works for me :
add_action('userpro_after_profile_head','userpro_sc_bar', 99);
function userpro_sc_bar( $args ) {
global $userpro, $userpro_social, $sellers; // <-- Added $sellers
extract($args);
$sellers = get_sellers(); // <-- Get $sellers
foreach ( $sellers['users'] as $seller ) {
$store_info = get_store_info( $seller->ID );
$store_url = get_store_url( $seller->ID );
}
( display content buttons here )
}

Related

Why can't I add a second term to my function (add taxonomy to body class)

I need to see the categories in the body to call certain CSS. I tried to add a second category term to the allready excisting working code. But it won't work for the newly added category (portfolio_category). What am I doing wrong?
//add taxonomy to body class
add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
// Add taxonomy terms name to body class
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$taxonomy_terms = get_the_terms($post->ID, 'arbocategory', 'portfolio_category' );
if ( $taxonomy_terms ) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'tax_' . $taxonomy_term->slug;
}
}
}
return $classes;
}
https://developer.wordpress.org/reference/functions/get_the_terms/
get_the_terms() accepst two paramters $post, $taxonomy, you can't just dump multiple taconomies to the function and expect it to work. you need to run get_the_terms() twice.
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$terms = array('arbocategory', 'portfolio_category');
foreach ($terms as $t) {
$taxonomy_terms = get_the_terms($post->ID, $t );
if (is_array($taxonomy_terms) && count($taxonomy_terms)>0) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'tax_' . $taxonomy_term->slug;
}
}
}
}
return $classes;
}

String with special character not adding in database WordPress

I am getting an array from order metadata and just taking two values from that array and adding them again in the database with new meta_key
function custom_order_meta_tracking($order_id) {
global $woocommerce, $post, $wpdb;
$orderid = new WC_Order($post->ID);
$order_id = trim(str_replace('#', '', $orderid->get_order_number()));
if ( metadata_exists( 'post', $order_id, '_wc_shipment_tracking_items' )) {
$gettracking_array = get_post_meta($order_id,'_wc_shipment_tracking_items', true);
foreach ($gettracking_array as $key => $item ) {
$tracking_number = $gettracking_array[$key]['tracking_number'];
$tracking_provider = $gettracking_array[$key]['tracking_provider'];
$tracking_providers = $wpdb->_real_escape( $tracking_provider );
update_post_meta($order_id, '_tracking_number', $tracking_number, $prev_value);
update_post_meta($order_id, '_tracking_provider', $tracking_providers, $prev_value);
}
}
}
add_action( 'woocommerce_order_status_completed', 'custom_order_meta_tracking', 10, 3 );
now the issue I am facing is that $tracking_providers have value with special characters like "after.ship" and in this case it did not add any value in database.
I tried "wpdb::_real_escape" but not working.
please help
thanks

How can I retrieve a variable from a function?

I know there are a few items out on the internet over how to achieve this, but I can't get it to work...
Can anyone please point me in the right direction?
I currently have two functions. I want to use the variable ($new_data) from function1 inside function2.
Function 1:
function manipulate_form_submission( $record, $ajax_handler ) {
$form_name = $record->get_form_settings( 'form_name' );
$form_data = $record->get( 'fields' );
//change the names of fields before we send them somewhere
$new_data = array(
'email' => isset( $form_data['email']['value'] ) ? $form_data['email']['value'] : '',
'url' => isset( $form_data['url']['value'] ) ? $form_data['url']['value'] : ''
)
}
Function 2:
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){
$order = wc_get_order( $order_id );
$response = wp_remote_post( 'https://mywebsite.com/insert_values.php', array( 'body' => $new_data) );
}
Thanks in advance!
Just initialize the variable outside the two functions like this:
<?php
$new_data = false;
function manipulate_form_submission( $record, $ajax_handler ) {
global $new_data;
$new_data = "New value";
}
function so_payment_complete( $order_id ){
// The variable is accessible here
global $new_data;
echo $new_data;
}
?>

Change status of woocommerce products by php function

I have a function which help me to make a redirect of a product if that product doesn't exist anymore in my affiliate xml.
What I want now is to put the product on draft but to redirect his link to a "This products doesnt exist anymore page" - for seo purposes
I didn't try something because I dont know what.
I saw something here and I think my answear is here but i don't know how to apply it:
Change product status if prices are updated in Woocommerce 3
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );
I expect that my code to remove the product from my website feed and redirect his link to a page of my website.
For seo purposes I cannot delete the product for all I just need to hide it from my shop but keep the link for google (also the pictures).
You can use WordPress function to update the status of the product where your logic of product doesn't exist anymore in my affiliate xml. exists.
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
/* Start Here*/
$my_product = array(
'ID' => $import->id,
'post_status' => 'draft',
);
wp_update_post( $my_product );
/* End Here*/
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );

How to exclude specific post from wp functions.php code?

I used this code to show responsive ad from adsense after 5th paragraph.
Adding Ads After First And Second Paragraph of WordPress Post
This is the code I use on my site:
function prefix_insert_after_paragraph2( $ads, $content ) {
if ( ! is_array( $ads ) ) {
return $content;
}
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
$n = $index + 1;
if ( isset( $ads[ $n ] ) ) {
$paragraphs[$index] .= $ads[ $n ];
}
}
return implode( '', $paragraphs );
}
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph2( array(
// The format is: '{PARAGRAPH_NUMBER}' => 'AD_CODE',
'5' => '<div>Ad code after FIRST paragraph goes here</div>',
), $content );
}
return $content;
}
I would like to exclude this code only for specific posts. How can I add the proper code to be able to exclude this function for post id=280?
Thank you.
Why do you want to exclude this function inside functions.php?
I think it's much easier to do it inside the post loop.
For example, to exclude the POST ID 280, if you're inside the page.php, or similar you can do this:
global $post;
if ($post->ID == 280){ remove_filter( 'the_content', 'prefix_insert_post_ads' ); }
This way you have the 'add_filter' on your functions.php file, and if you find the exception inside the post (ID=280), you remove the filter.
Just U have to check the current post_id. E.g. :
function prefix_insert_post_ads( $content ) {
global $post;
$post_id = $post->ID;
$post_ids_excluded = [280,....]; // excluded posts ids
if ( in_array($post_id,$post_ids_excluded) ){
return $content;
}
/*
... the same code .....
*/
}

Categories