Category: WordPress

How to check featured image exist or not in post

$args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => -1);

$the_query = new WP_Query( $args );
$i = 1;

if ( $the_query->have_posts() ) {

echo ‘<ul>’;

while ( $the_query->have_posts() ) {

$the_query->the_post();

if ( has_post_thumbnail() ) {

$dom_url = get_the_post_thumbnail_url();

$new_url = str_replace(‘site_url()’,’/var/www/html/’,get_the_post_thumbnail_url()); if(!file_exists($new_url))

{

echo ‘<li><b> ‘ .$i. ‘ </b> ‘. get_the_title() . ‘ Page Link:——-‘. get_the_post_thumbnail_url() .'</li>’;

$i++;

}

}

}

echo ‘</ul>’;

wp_reset_postdata();

} else {

// no posts found

}
echo “Number Of Posts: “.$i;

Check root path of image $_SERVER[‘DOCUMENT_ROOT’]

Remove All products or attributes with SQL commands WooCommerce

Remove all attributes from WooCommerce

DELETE FROM wp_terms WHERE term_id IN 
(SELECT term_id FROM wp_term_taxonomy WHERE taxonomy LIKE 'pa_%');
DELETE FROM wp_term_taxonomy WHERE taxonomy LIKE 'pa_%';
DELETE FROM wp_term_relationships WHERE term_taxonomy_id not IN 
(SELECT term_taxonomy_id FROM wp_term_taxonomy);

Delete all WooCommerce products

DELETE FROM wp_term_relationships WHERE object_id IN 
(SELECT ID FROM wp_posts WHERE post_type IN ('product','product_variation'));
DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type IN 
('product','product_variation'));
DELETE FROM wp_posts WHERE post_type IN ('product','product_variation');

Delete orphaned postmeta

DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

Add New Admin User in WordPress by MySql Query

INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES (‘newadmin’, MD5(‘pass123’), ‘firstname lastname’, ‘email@example.com‘, ‘0’);

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), ‘wp_capabilities’, ‘a:1:{s:13:”administrator”;s:1:”1″;}’);

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), ‘wp_user_level’, ’10’);

How to list all Categories, Sub-Categories and Products in WooCommerce

<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>

<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{

if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo "<ul class='category'><li>".$cat->name;
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty

);

$sub_cats = get_categories( $args2 );
if($sub_cats)
{

foreach($sub_cats as $sub_category)
{
echo "<ul class='subcategory'>";
if($sub_cats->$sub_category == 0)
{
echo "<li>".$sub_category->cat_name;

?>
<?php
/*echo "<pre>";
print_r($sub_category);
echo "</pre>";*/

$args = array( 'post_type' => 'product','product_cat' => $sub_category->slug);
$loop = new WP_Query( $args );
echo "<ul class='products'>";
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>
<?php wp_reset_query(); ?>

<?php
}
else
{
echo "</li></ul></li>";
}
echo "</ul>";
}
}
else
{
$args = array( 'post_type' => 'product', 'product_cat' => $cat->slug );
$loop = new WP_Query( $args );
echo "<ul class='products'>";
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul></li></ul>
<?php wp_reset_query();
}
}
else
{
echo "</li></ul>";
}
}
?>

Create WordPress ajax Login within 2 min

Step 1: Create Login form.

?php if (is_user_logged_in()) { ?>
<a class=”login_button” href=”<?php echo wp_logout_url( home_url() ); ?>”>Logout</a>
<?php } else { ?>

<form id=”login” action=”login” method=”post”>
<h1>Site Login</h1>
<p class=”status”></p>
<label for=”username”>Username</label>
<input id=”username” type=”text” name=”username”>
<label for=”password”>Password</label>
<input id=”password” type=”password” name=”password”>
<a class=”lost” href=”<?php echo wp_lostpassword_url(); ?>”>Lost your password?</a>
<input class=”submit_button” type=”submit” value=”Login” name=”submit”>
<?php wp_nonce_field( ‘ajax-login-nonce’, ‘security’ ); ?>
</form>
<?php } ?>

Step 2: Create ajax request.

Note: Paste this code in header.php or footer.php

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

// Show the login dialog box on click
$(‘a#show_login’).on(‘click’, function(e){
$(‘body’).prepend(‘

‘);
$(‘form#login’).fadeIn(500);
$(‘div.login_overlay, form#login a.close’).on(‘click’, function(){
$(‘div.login_overlay’).remove();
$(‘form#login’).hide();
});
e.preventDefault();
});

// Perform AJAX login on form submit
$(‘form#login’).on(‘submit’, function(e){
$(‘form#login p.status’).show().text(ajax_login_object.loadingmessage);
$.ajax({
type: ‘POST’,
dataType: ‘json’,
url: ajax_login_object.ajaxurl,
data: {
‘action’: ‘ajaxlogin’, //calls wp_ajax_nopriv_ajaxlogin
‘username’: $(‘form#login #username’).val(),
‘password’: $(‘form#login #password’).val(),
‘security’: $(‘form#login #security’).val() },
success: function(data){
$(‘form#login p.status’).text(data.message);
if (data.loggedin == true){
document.location.href = ajax_login_object.redirecturl;
}
}
});
e.preventDefault();
});

});

Step 3: Request data from database using function.

Note: Paste this code in function.php file

function ajax_login_init(){

wp_register_script(‘ajax-login-script’, get_template_directory_uri() . ‘/ajax-login-script.js’, array(‘jquery’) );
wp_enqueue_script(‘ajax-login-script’);

wp_localize_script( ‘ajax-login-script’, ‘ajax_login_object’, array(
‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ),
‘redirecturl’ => get_permalink( get_the_ID()),
‘loadingmessage’ => __(‘Sending user info, please wait…’)
));

// Enable the user with no privileges to run ajax_login() in AJAX
add_action( ‘wp_ajax_nopriv_ajaxlogin’, ‘ajax_login’ );
}

// Execute the action only if the user isn’t logged in
if (!is_user_logged_in()) {
add_action(‘init’, ‘ajax_login_init’);
}

function ajax_login(){

// First check the nonce, if it fails the function will break
check_ajax_referer( ‘ajax-login-nonce’, ‘security’ );

// Nonce is checked, get the POST data and sign user on
$info = array();
$info[‘user_login’] = $_POST[‘username’];
$info[‘user_password’] = $_POST[‘password’];
$info[‘remember’] = true;

$user_signon = wp_signon( $info, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array(‘loggedin’=>false, ‘message’=>__(‘Wrong username or password.’)));
} else {
echo json_encode(array(‘loggedin’=>true, ‘message’=>__(‘Login successful, redirecting…’)));
}

die();
}

add_filter(‘show_admin_bar’, ‘__return_false’);