29 February 2020

How to fetch data (messages) in every second with ajax (setinterval) from mysql (PHP) in wordpress?

Fetch data (messages) in every second with ajax (setinterval) from mysql (PHP) in wordpress.


In main file

<script>

jQuery(document).ready(function($){



var own_id = $("#own_id").val();
var user_id = $("#user_id").val();
alert('helo');


function sandeep(){

var data = {
'action':'fetch_message',
'own_id':own_id,
'user_id':user_id,
}


$.post(myajax.ajaxurl,data,function(response){
//$("#response").html(response);
$("#jassi").html(response);
console.log(response);
});


}


setInterval(function(){ sandeep(); }, 500);
});

</script>



<div id="jassi">



</div>


In Funtion.php file 




function fetch_message(){
$msg_content = $_POST['msg_content'];
$own_id = $_POST['own_id'];
$user_id = $_POST['user_id'];
echo $user_id;
global $wpdb;
$table_name13 = $wpdb->prefix . "user_chats";
$results = $wpdb->get_results("select * from $table_name13 where sender_id ='$own_id' AND receiver_id='$user_id'");
$total = count($results);

foreach($results as $result){
echo $result->msg_content."<br>";




}
die();
}

add_action('wp_ajax_nopriv_fetch_message','fetch_message');
add_action('wp_ajax_fetch_message','fetch_message');

How to shrink navbar on scroll ?

Shrink navbar on scroll 



<style>
.nav_logo_scroll{

width: 120px !important;
    transition: all 0.5s linear;
margin-left: 45px
}
.jassi{

width: 100% !important;
  transition: all 0.5s linear;
}
</style>


<script>
jQuery(document).ready(function($){


$(window).scroll(function() {
if($(document).scrollTop() >=1) {
$('.nav').css('position','fixed');
$('.nav_logo').addClass('nav_logo_scroll');
$('.nav_logo').css(' transition','all 0.5s linear');
$('.nav_logo').removeClass('jassi');
}
else if($(document).scrollTop() < 250) {

$('.nav').css('position','relative');
$('.nav').css(' transition','all 0.5s linear');
$('.nav_logo').removeClass('nav_logo_scroll');
$('.nav_logo').addClass('jassi');
}
}
});

});


</script>

How to Alternative to “header” for re-directs in PHP


Alternative to “header” for re-directs in PHP



function redirect($url)
{
    if (!headers_sent())
    {   
        header('Location: '.$url);
        exit;
        }
    else
        { 
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>'; exit;
    }
}

03 February 2020

How to detect the current user has an active subscription in woocommerce?

Detect the current user has an active subscription in Woocommerce.


function has_active_subscription( $user_id=null ) {
    // When a $user_id is not specified, get the current user Id
    if( null == $user_id && is_user_logged_in() )
        $user_id = get_current_user_id();
    // User not logged in we return false
    if( $user_id == 0 )
        return false;

    // Get all active subscriptions for a user ID
    $active_subscriptions = get_posts( array(
        'numberposts' => 1, // Only one is enough
        'meta_key'    => '_customer_user',
        'meta_value'  => $user_id,
        'post_type'   => 'shop_subscription', // Subscription post type
        'post_status' => 'wc-active', // Active subscription
        'fields'      => 'ids', // return only IDs (instead of complete post objects)
    ) );

    return sizeof($active_subscriptions) == 0 ? false : true;
}




Or

if( has_active_subscription() ){ // Current user has an active subscription
    // do something … here goes your code

    // Example of displaying something
    echo '<p>I have active subscription</p>';
}



                                                                             Or


if( has_active_subscription(26) ){ // Defined User ID has an active subscription
    // do something … here goes your code

    // Example of displaying something
    echo '<p>User ID "26" have an active subscription</p>';
}

How to use ob_start() function in php?


PHP | ob_start() Function


PHP is an interpreted language thus each statement is executed one after another, therefore PHP tends to send HTML to browsers in chunks thus reducing performance. Using output buffering the generated HTML gets stored in a buffer or a string variable and is sent to the buffer to render after the execution of the last statement in the PHP script.
But Output Buffering is not enabled by default. In order to enable the Output Buffering one must use the ob_start() function before any echoing any HTML content in a script.



function jassi
{
ob_start();
echo "Hello there!";
$output = ob_get_contents();
ob_end_clean();
}


OR


function jassi
{
ob_start();
echo "Hello there!";
$obc = ob_get_contents();
ob_end_clean();
return $obc;
}

How to check user is logged in woocommerce and wordpress?

Check user is logged in woocommerce and wordpress.





if ( is_user_logged_in() ) {
   // your code for logged in user
} else {
   // your code for logged out user
}

How to add cart product in cart with link (URL) in woocommerce ?

Add cart product in cart with link (URL) in woocommerce





href=”http://yourtsite.com/?add-to-cart=3083″





href=”http://yourtsite.com/?add-to-cart=3084&quantity=3″

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