Blog

  • Disable User Password Change Email/Notification on WordPress

    Wondering, How to disable user password change emails/notifications. Here is your answer.

    Disable Password Change Email to Admin

    Note: Create a Plugin or Add this code inside a custom plugin to make this code work.

    /**
     * Disable Admin Notification of User Password Change
     *
     * @see pluggable.php
     */
    if ( ! function_exists( 'wp_password_change_notification' ) ) {
        function wp_password_change_notification( $user ) {
            return;
        }
    }

    Suppressing this email notification has to handled with a plugin because pluggable.php is loaded earlier than a theme’s functions.php file.

    Disable Password Change Email to Users

    /**
     * Disable User Notification of User Password Change
     */
    add_filter( 'send_password_change_email', '__return_false' );

    Add above line of code in functions.php of your theme to disable user password change emails to users itself.

  • How To Use AJAX In WordPress

    Want to do something without even reloading the page on wordpress ?
    Use WP Ajax.

    Using WP Ajax, You can do stuff asynchronously like Add to Cart, Refresh Fragments, Change Quantity of Products, Load more posts and many more.

    How to create Ajax Calls in WordPress ?

    1. Create Ajax Actions on WordPress.

    Example: Woocoommerce Add to Cart with Quantity using Ajax

    Add this inside your theme’s functions.php file.

    <?php
    // Add AJAX for logged in users.
    add_action("wp_ajax_cmw_add_to_cart", "cmw_add_to_cart");
    
    // Add AJAX for not logged in users.
    add_action("wp_ajax_nopriv_cmw_add_to_cart", "cmw_add_to_cart");
    
    function cmw_add_to_cart() {
    
       // Verify Nonce
       if ( !wp_verify_nonce( $_REQUEST['nonce'], "cmw_add_to_cart_nonce")) {
          die("404 Access Denied");
       }
    
       if(isset($_REQUEST['product_id']) && isset($_REQUEST['quantity'])) {
          $product_id = $_REQUEST['product_id'];
          $quantity = $_REQUEST['quantity'];
          WC()->cart->add_to_cart( $product_id, $quantity);
          echo json_encode(array("added"=>true));
       }
       die();
    }

    2. Create jQuery to Trigger Ajax

    Add this inside your theme’s functions.php file.

    add_action('wp_enqueue_scripts', 'cmw_enqueue_js');
    function cmw_enqueue_js() {
    
        // Register Custom JS File (We will put Ajax Code inside this file);
        // Use get_template_directory_uri for Theme.
        // Use get_stylesheet_directory_uri for Child Theme.
        wp_register_script( "cmw_script", get_template_directory_uri().'/css/cmw_script.js', array('jquery') );
       
        wp_localize_script( 'cmw_script', 'cmw', array( "ajaxurl" => admin_url( 'admin-ajax.php' ), "nonce" => wp_create_nonce("cmw_add_to_cart_nonce")));        
       
       
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'cmw_script' );
    }

    Add this inside your theme’s YOUR_THEME_DIR/js/cmw_script.js file. (Create this file if not exists)

    jQuery(function($) {
        $.post(
            cmw.ajaxurl,
            {
                "action": "",
                "product_id": 1122,
                "quantity": 1
            },
            function(data, status) {
                data = JSON.parse(data);
                if(data.added) {
                    window.alert("Product Added to cart");
                }
            }
        )
    });

    It’s Done!

    Using Ajax to load stuff asynchronously is pretty good idea. Use it to make your website better and faster.

    Do you know ?

    Using Ajax on WordPress reduces load on server as it reduces the number of reloads and loads only needed content.

  • Add/Create Shortcode with Arguments

    Shortcodes are pretty useful in wordpress to show static/dynamic custom content according to your needs. You can use shortcodes to show dynamic content like Posts, Categories, Products, etc. Many WordPress plugins and themes use shortcodes to add specialized content like contact forms, image galleries, sliders, and many more. If you’re thinking to learn WordPress backend, shortcodes would be a good start.

    Simple Shortcode

    Create a Shortcode

    Paste this code inside theme’s functions.php file.

    <?php
    function function_name() {
        echo “This is my first Shortcode!”;
    }
    add_shortcode(“shortcode_name”,”function_name”);
    ?>

    Add Shortcode

    Use shortcode in Text Editor

    [shortcode_name]

    Use shortcode using PHP

    <?= do_shortcode(“[shortcode_name]”) ?>

    Output

    This is my first Shortcode!

    Shortcode with Arguments

    Create Shortcode with Arguments

    Paste this code inside theme’s functions.php file.

    <?php
    function function_name($args) {
        $name = $args[“name”];
        echo “My name is {$name}.”;
    }
    add_shortcode(“shortcode_name”,”function_name”);
    ?>

    Add Shortcode with Arguments

    Use Text Editor to print Shortcode

    [shortcode_name name=‘Sanchit’]

    Use below code to print shortcode using php.

    <?= do_shortcode(“[shortcode_name name=‘Sanchit’]”) ?>

    Output

    My name is Sanchit.