WordPress CheatSheet


Force add SSL / HTTPS without plugin

First make sure that your wordpress site has WordPress Address (URL) and Site Address (URL) set to a https:// version in Settings > General.

Fix redirect loop on WordPress https version

Then if you go into a redirect loop while trying to reach https version, add this code in wp-config.php file:

$_SERVER['HTTPS'] = 'on';

How to enable debug log & debug display in WordPress

You can enable debug in wordpress by adding these codes to wp-config.php file in the root directory of your website (public_html or html etc)

Please note, make sure that there are no other entries with same key in same file, if they are then remove them or change them instead of adding new.

You can add the following code anywhere in the wp-config file, but try to keep it above the database configs.

// will display the debug for fatal errors, warnings etc on site
define('WP_DEBUG', true);

// will save a debug file in wp-content/ folder
define('WP_DEBUG_LOG', true);

Note that you can disable the log by changing true to false for the constants defined above.

These are also helpful when your site gets fatal error or wordpress just shows error 500 or that Your website encountered an error, and you want to see more info. Just add the above mentioned block and refresh that page.

Move enqueue javascript files to footer

The wp_enqueue_script tag has an in_footer parameter, turning it to true will cause the enqueued file to be loaded in the footer.

The sequence of the hook is as follows:

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer);

Add scripts to footer on woocommerce checkout page only

add_action( 'wp_footer', 'custom_jscript_checkout', 9999 );
 
function custom_jscript_checkout() {
   global $wp;
   // for all pages under /checkout/
   if ( is_checkout() ) {
   // or for only main /checkout/ page mainly, needs testing
   // if ( is_checkout() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ) {
      echo '<script>paste your script here!</script>';
   }
}

Ajax callback hook for jquery frontend

To call a PHP function with Ajax when the user clicks a button, we need to add two parts, the backend ajax code and the frontend javascript code. Like following.

You can add the code via custom plugin file or theme functions.php file.

PHP ajax code:

// register the ajax action for authenticated users
add_action('wp_ajax_some_custom_action', 'some_custom_action');

// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_some_custom_action', 'some_custom_action');

// handle the ajax request
function some_custom_action() {
    $input_1 = $_REQUEST['input_1'];

    // add your logic here...

    // in the end, returns success json data
    wp_send_json_success([/* some data here */]);

    // or, on error, return error json data
    wp_send_json_error([/* some data here */]);
}

jQuery code for frontend:

jQuery(document).ready(function($) { 
    $.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
        data: {
            action: 'some_custom_action',
            // add your parameters here
            input_1: 'some value' 
        },
        success: function (output) {
           console.log(output);
        }
    });
});

Minimum code to add Child Theme

Adding a child theme simply needs a single style.css file with following two items in it:

/*
    Theme Name: Custom Theme Name
    Template: parent-theme-folder-name
*/

Add custom menu locations in WordPress

You can do that by adding following code in functions.php file:

function sch_custom_new_menu() {
  register_nav_menus(
    array(
      'custom-menu-1' => __( 'First custom menu title' ),
      'custom-menu-2' => __( 'Second custom menu title' )
    )
  );
}
add_action( 'init', 'sch_custom_new_menu' );

Leave a Reply

Your email address will not be published.