21 August 2021

How to get product categories in product loop in Woo-commerce?

Get product categories in product loop in Woo-commerce.


add_action('woocommerce_shop_loop_item_title', 'pendingcategory');    

function pendingcategory(){

global $product;

$id = $product->get_id();

$terms = wp_get_post_terms( $id, 'product_cat' );

foreach ( $terms as $term ) {

$categories[] = $term->slug;

}

if ( in_array( 'pending', $categories ) ) {  

echo 'Pending';

}

 

20 August 2021

How to get map location dynamically in PHP?

 Get map location dynamically in PHP?



<iframe width="100%" height="400" src="https://maps.google.com/maps?q=<?php echo $city; ?>&output=embed"></iframe>


12 August 2021

How to add "wp_editor" in custom form in Wordrpess?

Add "wp_editor" in custom form in Wordrpess.





if(isset($_POST['submit'])){

print_r($_POST);

}

$editor_id = 'custom_editor_box';

$uploaded_csv = 'hello';

?>

<form action="" method="post">

<?php 

wp_editor( $uploaded_csv, $editor_id );

?>

<input type="submit" value="submit" name="submit">

</form>

<?php


How to build a tree from a flat array in PHP>

Build a tree from a flat array in PHP

 

$hello = array

  (

  array("ID" => "1","student" => "Jassi","marks" => "80","parent_id" => ""),

  array("ID" => "2","student" => "Golu","marks" => "80","parent_id" => "1"),

  array("ID" => "3","student" => "Jassi","marks" => "80","parent_id" => "1"),

  array("ID" => "4","student" => "Jai","marks" => "90","parent_id" => "1"),

  array("ID" => "5","student" => "Jassi","marks" => "85","parent_id" => "1"),

  array("ID" => "6","student" => "Ravinder","marks" => "30","parent_id" => "1"),

  array("ID" => "7","student" => "Mukesh","marks" => "45","parent_id" => "1"),

  array("ID" => "8","student" => "Ankush","marks" => "96","parent_id" => "2"),

  array("ID" => "9","student" => "Aman","marks" => "90","parent_id" => "2")

  );


function buildTree($elements, $parentId = 0) {

    $branch = array();


    foreach ($elements as $element) {

        if ($element['parent_id'] == $parentId) {

            $children = buildTree($elements, $element['ID']);

            if ($children) {

                $element['children'] = $children;

            }

            $branch[] = $element;

        }

    }


    return $branch;

}


echo '<pre>';

$tree = buildTree($hello,1);


print_r( $tree );

How to get PHP array group by key value?

“PHP array group by key value"



$hello = array
  (
  array("ID" => "1","student" => "Jassi","marks" => "80","parent_id" => ""),
  array("ID" => "2","student" => "Golu","marks" => "80","parent_id" => "1"),
  array("ID" => "3","student" => "Jassi","marks" => "80","parent_id" => "1"),
  array("ID" => "4","student" => "Jai","marks" => "90","parent_id" => "1"),
  array("ID" => "5","student" => "Jassi","marks" => "85","parent_id" => "1"),
  array("ID" => "6","student" => "Ravinder","marks" => "30","parent_id" => "1"),
  array("ID" => "7","student" => "Mukesh","marks" => "45","parent_id" => "1"),
  array("ID" => "8","student" => "Ankush","marks" => "96","parent_id" => "2"),
  array("ID" => "9","student" => "Aman","marks" => "90","parent_id" => "2")
  );



function group_by($key, $data) {
    $result = array();

    foreach($data as $val) {
        if(array_key_exists($key, $val)){
            $result[$val[$key]][] = $val;
        }else{
            $result[""][] = $val;
        }
    }

    return $result;
}


$byGroup = group_by("student", $hello);

echo '<pre>';

print_r($byGroup);

foreach($byGroup as $sub_cate=>$subarray){
//echo $sub_cate . ' ' . $subarray;


echo 'Subarray = >' . $sub_cate . '<br>';

foreach($subarray as $print){
echo $print['ID'].',';
}
echo '<br>=========================<br>';
}

10 August 2021

How To Add (Create) Pagination in WordPress?

 Add (Create) Pagination in WordPress.




<style>

.cus_pagination ul li .page-numbers {

    background: white;

    padding: 7px 11px;

    margin-left: 8px;

    text-decoration: none;

    border: 1px solid black;

    color: #000;

}

.cus_pagination ul {

    display: flex;

}


.cus_pagination span.page-numbers.current {

    background: #000;

    color: white;

}

</style>

<?php


global $wpdb;

// QUERY HERE TO COUNT TOTAL RECORDS FOR PAGINATION 

$cars = array

  (

  array("student" => "Jassi","marks" => "80"),

  array("student" => "Jai","marks" => "90"),

  array("student" => "Golu","marks" => "85"),

  array("student" => "Ravinder","marks" => "30"),

  array("student" => "Mukesh","marks" => "45"),

  array("student" => "Ankush","marks" => "96"),

  array("student" => "Aman","marks" => "90")

  );

  

 $total = count($cars);


$post_per_page = 1;

$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;

$offset = ( $page * $post_per_page ) - $post_per_page;


// QUERY HERE TO GET OUR RESULTS 

//$results = $wpdb->get_results("SELECT student, marks FROM $cars LIMIT $post_per_page OFFSET $offset");

// or 

$results = array_slice( $cars, $offset, $post_per_page );


/* print_R($results);

die();


 */



// PHP FOR EACH LOOP HERE TO DISPLAY OUR RESULTS

foreach($results as $row)

 {

//print_r($row);


 echo $row['student']." => ".$row['marks']."<br>"; 

 }

// END OUR FOR EACH LOOP


?>

<?php 

echo '<div class="cus_pagination">';

echo paginate_links( array(

'base' => add_query_arg( 'cpage', '%#%' ),

'format' => '',

'prev_text' => __('&laquo;'),

'next_text' => __('&raquo;'),

'total' => ceil($total / $post_per_page),

'current' => $page,

'type' => 'list'

));

echo '</div>';

?>

How to add re-captcha v3 on all Elementor forms using coding?

 Add re-captcha v3 on all Elementor forms using coding add_action('wp_footer',function(){     ?> <script src="https://www...