Top Questions:

  • 5 years-11 months

    How to limit characters for textarea including count in jquery

    • Category : java
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    Here are two scenarios where the keyup event will not get fired: The user drags text into the textarea. The user copy-paste text in the textarea with a right click (contextual menu).

    Use the HTML5 input event instead for a more robust solution:

    // jquery
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    
    // html
    <div class="control">
    	<textarea spellcheck="true" maxlength="200" name="question" id="question" class="input-textarea required-entry" cols="5" rows="8" data-validate="{required:true}" aria-required="true"></textarea>
      <div class="limittext"><span class="charnum">200 Characters left</span></div>
    </div>
    
    <script type="text/javascript">
    jQuery(document).ready(function() {
      // edit area start
      var maxLength = 200;
      var textarea_id = "question";
      // edit area end
    
      var html='<span style="color:red;">'+ maxLength + ' characters allowed, excess characters trimmed</span>';
      jQuery("#"+textarea_id).on('input',function(){
          var length = this.value.length;
          if (length >= maxLength) {
            this.value = this.value.substring(0, maxLength);
            jQuery(this).next(".limittext").html(html);
          } else {
            var count=maxLength-length;
            jQuery(this).next(".limittext").html('<span class="charnum">' +count+' Characters left</span>');
          }
      });
    });
    </script>
  • 5 years-11 months

    How to Hide the WordPress Update Notification in dashboard

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    To disable this update notification, there is no switch in the settings menu or anywhere else in the admin dashboard. To prevent WordPress from showing these information snippets on top of your WordPress dashboard, you shall need to add a PHP code snippet to your functions.php file.

    add_filter('pre_site_transient_update_core','__return_null');
    add_filter('pre_site_transient_update_plugins','__return_null');
    add_filter('pre_site_transient_update_themes','__return_null');
     
  • 5 years-11 months

    How to create add to cart with AJAX on single and variable products programmatically

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    default WooCommerce if the user clicks on the add to cart button, that item is added to the cart. if you want to add custom add to cart button please remove default "add to cart" button

    // custom template
    <button type="submit" class="custom_cart_button">add to cart</button>
    <script type="text/javascript">
    jQuery(document).ready(function() {
    var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
        jQuery('.custom_cart_button').submit(function(e) {
            jQuery.ajax({
                url: ajaxurl,
                type: "post",
                data: {				
    		action : 'post_product_variations19'
    	    },
                success: function(data) {   
                	console.log(data);
                }
            });
    	return false;
        });
    });
    </script>
    
    
    // function.php
    add_action( 'wp_ajax_nopriv_post_product_variations19', 'post_product_variations_func19' );
    add_action( 'wp_ajax_post_product_variations19', 'post_product_variations_func19' );
    function post_product_variations_func19() {
    
        global $woocommerce;
    
        $product_id = 364; // your product id
    
        $found = false;
    
        //check if product already in cart
        if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
            foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id ) {
                	//$calc_qty = $values['quantity'] < 1 ? 1 : $values['quantity']+1;
                	//$woocommerce->cart->set_quantity( $cart_item_key, $calc_qty, true );
                    $found = true;
                }
            }
            // if product not found, add it
            //if ( ! $found )
                $woocommerce->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            $woocommerce->cart->add_to_cart( $product_id );
        }
    	
    wp_die();
    }
  • 2 years-11 months

    How to change format of Phone number in input text as you type?

    • Category : php
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Mohit

    I'm currently displaying phone numbers like 18003336523. However, I need the number to be formatted in a more human-readable form, for example: 1-800-333-6523.

    <script>
    
    jQuery('.wpcf7-tel').keydown(function (e) {
      var key = e.charCode || e.keyCode || 0;
      $phone = jQuery(this);
    
      // Auto-format- do not expose the mask as the user begins to type
      if (key !== 8 && key !== 9) {
          
    console.log($phone.val().length);
       if ($phone.val().length === 1) {
        $phone.val($phone.val() + '-');
       }
       if ($phone.val().length === 5) {
        $phone.val($phone.val() + '-');
       }   
       if ($phone.val().length === 9) {
        $phone.val($phone.val() + '-');
       }
       if ($phone.val().length === 15) {
        return false;
       }
      }
    
      // Allow numeric (and tab, backspace, delete) keys only
      return (key == 8 || 
        key == 9 ||
        key == 46 ||
        (key >= 48 && key <= 57) ||
        (key >= 96 && key <= 105)); 
     })
     
     .bind('focus click', function () {
      $phone = jQuery(this);
      
      if ($phone.val().length === 0) {
       //$phone.val('(');
      }
      else {
       var val = $phone.val();
       $phone.val('').val(val); // Ensure cursor remains at the end
      }
     })
     
     .blur(function () {
      $phone = jQuery(this);
      
      if ($phone.val() === '(') {
       $phone.val('');
      }
     });
    </script>
     
  • 2 years-11 months

    How to include simple pagination in a Wordpress Custom Post Type Query

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Mohit

    For a WordPress blog, pagination alludes to showing a predetermined number of posts per page. Pagination permits clients the capacity to explore substantially less demanding and more profound into the chronicles. In this post, I will demonstrate that my past instructional exercise about including Pagination without a module in WordPress, can likewise be utilized on Custom Post Types with no alterations. I will likewise demonstrate the means I took to test my past instructional exercise's pagination code and demonstrate that it is as yet substantial for custom post writes.

    <?php
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;          
    $args = array( 'paged'=> $paged,'post_type' => 'product','orderby' => 'post_date','order'=> 'ASC','posts_per_page' => 10,'post_status'=>'publish');        
    
    $query = new WP_Query($args);
    if ($query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post();
        $id=get_the_ID();
    ?> 
          
    <a href="<?php the_permalink() ?>" class="post-title">
        <h2><?php the_title();?></h2>
    </a>
    <div class="post-content"> <?php echo '<p>'.substr(get_the_content(),0,190).'...</p>'; ?> </div>
    
    <?php
    endwhile; endif;
    ?>
     <?php
     echo '<div class="blogPager">';
     echo '<ul class="pagination"> ';
     if( $paged-1 >= 1){
        echo ' <li class="'.($i == $paged ? 'active ' : '').'page button"><a   href="'.get_pagenum_link(($paged-1 > 0 ? $paged-1 : 1)).'"  > Previous</a></li>';
     }
    
    if($query->max_num_pages > 10) {
        for($i=1;$i<=$query->max_num_pages;$i++){
            echo '<li class="'.($i == $paged ? 'active ' : '').'page button"><a  href="'.
           get_pagenum_link($i).'">'.$i.'</a></li>';
    
        }
    }
    if( $query->max_num_pages > $paged  ) {
        echo '<li class="'.($i == $paged ? 'active ' : '').'page button"> <a   href="'.get_pagenum_link(($paged+1 <= $query->max_num_pages ? $paged+1 :
                $query->max_num_pages)).'">Next </a></li>';
    }
    echo '</ul> ';
    echo '</div> ';
    wp_reset_postdata();
    ?>
     
  • 5 years-11 months

    How will my javascript function executed after the previous ajax call complete ?

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    You can specify a function identifier in a custom property in your .ajax() call and use the value of that in your ajaxComplete() callback.

    For example, get any ajax property while making your .ajax() call:
    function foo() {
        $.ajax({
            url: 'my-page.php',
        });
    }
    
    And then, use any of ajax property to ajaxComplete() like this:
    $('#my-unique-id').ajaxComplete(function(e, xhr, settings) { // run after all ajax complete 
        if (settings.url == 'my-page.php') { // write condition according to your requirement
            // execute you code
        } else {
            // execute you code
        }
    });
     
  • 5 years-11 months

    How to Remove Items from the WordPress Toolbar new from wp admin area

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    According to me if you want to remove all NEW link in wp admin area please follow this code

    add_action( 'admin_bar_menu', 'remove_wp_nodes', 999 );
    
    function remove_wp_nodes() 
    {
        global $wp_admin_bar;   
        $wp_admin_bar->remove_node('new-content');
    }
     
  • 5 years-11 months

    woocommerce backend add items in new or edit orders

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    If you need to change item product prices in Woocommerce Backend Order. Please use the following hook or code

    add_action( 'woocommerce_ajax_add_order_item_meta', 'VP_set_item_quantity', 99, 3 );
    function VP_set_item_quantity( $item_id, $item, $order ) {
        foreach ( $order->get_items() as $order_item_id => $order_item_data ) {
            if ( $order_item_id == $item_id ) {
    
    
                $order_item_data->set_subtotal('20.22');
                $order_item_data->set_total(20.22);
                $order->apply_changes();
                $order->save();
    				
    		}
        }
    }
     
  • 5 years-11 months

    How to change user role after wordpress registration

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    This action hook allows you to access data for a new user immediately after they are added to the database. The user id is passed to hook as an argument.

    add_action( 'user_register', 'eddpremium_register_fields_save');
    add_action( 'profile_update', 'eddpremium_register_fields_save');
    
    function eddpremium_register_fields_save($user_id) {
    	$u = new WP_User($user_id);
    
    	// Remove role
    	$u->remove_role( 'subscriber' );
    
    	// Add role
    	$u->add_role( 'author' );
    }
     
  • 5 years-11 months

    how to add custom menu code in wordpress templates

    • Category : wordpress
    • Location : Chandigarh, India
    • Posted On :
    • Posted by : Vikas

    If you want to add custom navigation menus in your WordPress templates? By default, many WordPress themes come with pre-defined menu locations and layouts. follow this article:

    <?php $defaults = array(
        'menu'            => 'main',
        'container'       => 'div',
        'container_class' => 'navbar-collapse collapse',
        'container_id' => 'navbar',
        'menu_class'      => 'nav navbar-nav navbar-right',
        'echo'            => true,
        'fallback_cb'     => 'wp_page_menu',
        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>'
    );
    ?>
     
    <?php wp_nav_menu( $defaults ); ?>
    
    /* Output */
    
    <div id="navbar" class="navbar-collapse collapse">
    	<ul id="menu-main" class="nav navbar-nav navbar-right">
    		<li id="menu-item-108" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-2 current_page_item menu-item-108"><a href="http://www.example.com/">Home</a></li>
    	</ul>
    </div>