Tuesday, September 27, 2016

Delete all tables from database using command line

We can drop all tables of a database using an GUI Interface like PHPMYADMIN. But sometimes in some scenarios we do not have access to the GUI Interface like PHPMYADMIN but only tool like command line or command prompt.

In Such scenarios we can drop all the tables of a specific database using the command line. Following are the statements which can be used on the command line to drop all the database tables from the database with ease.

SET FOREIGN_KEY_CHECKS = 0; 

SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
  FROM information_schema.tables 
  WHERE table_schema = 'database_name'; -- specify DB name here.

SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;

That's it all tables from the database will be deleted.

Saturday, May 21, 2016

WordPress get recent posts

In WordPress we can get recently added posts.

To get recently added posts in WordPress user the following function.

<?php
$args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$posts_list = get_posts( $args );
echo '<ul>';
foreach ($posts_list as $post) :  setup_postdata($post); ?>
<li><strong><?php the_date(); ?></strong><br />
<a href="<?php the_permalink(); ?>" title="<?php the_title();?>"> <?php the_title(); ?></a>
</li>
        <?php endforeach; ?>
       </ul>

 The above code can be used anywhere in the website and in any template.

WordPress get current timezone

In WordPress timezone is already set during installation. To get the current timezone in WordPress use the following piece of code.

You can get 2 things.

 a) If you want to get the offset use the following code.

<?php echo get_option('gmt_offset'); ?>

 b) If you want to get timezone in string format use the following code.

<?php echo get_option('timezone_string'); ?>


WordPress get form post data

WordPress supports forms and there are many extension for forms available on the WordPress plugin repository. When form is submitted it is processed and submitted as defined in the plugin.

But sometimes we need the form data and for that we cannot customize the plugin as hacking the core of plugin is bad idea and an plugin update in the future will erase all our changes done.

So, to get the posted form data you can use the following code.

<?php
    foreach($_POST as $key=>$post_data){
        echo "You posted:" . $key . " = " . $post_data . "<br>";
    }
?>

using this you can get the form key and its value.

WordPress get first image from post

To display the first image from the post content use the following function given.

First open your active theme's functions.php file in a code editor. After opening the file in the code editor add the following function at the end of the file.

<?php
function get_first_image() {
 global $post, $posts;
 $first_img = '';
 ob_start();
 ob_end_clean();
 $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
 $first_img = $matches [1] [0];

 if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
 }
 return $first_img;
}
?>

Save the file and upload it back to the server.

The function can be called in the loop or anywhere to display the first image from the post.

<?php echo catch_that_image() ?>


WordPress get parent categories

WordPress supports Categories. A Post can be assigned to single or multiple categories in WordPress. Also categories can be single level or multi level (nested). To get only parent categories and exclude the child categories the following code can be used.

<?php
$args = array(
 'orderby' => 'name',
 'parent' => 0
);

$categories = get_categories($args);
?>

$categories contains all the parent level categories only.

WordPress get featured image by post id

In WordPress there is featured image for Post and Pages and Custom Post types.

To get the feature image of an post or page or custom post type use the following code.

<?php if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
 endif;
?>

Now in $image you have all the feature image information. To display the image you can do it as follows:

<img src="<?php echo $image[0]; ?>" />

And you are done. Now feature image will be displayed.

WordPress get blog name

Wordpress is a blogging CMS. But now WordPress is used for many other purpose like shopping cart, gallery sites, portfolio site etc.

This all have been possible with the available plugins and themes and WordPress support community.

If you want to get the WordPress site name then you can get it in the following way.

<?php echo get_bloginfo( 'name' ); ?>

Using the above line any where in the file will give you the WordPress site/blog name.

WordPress get ajax full url

In Wordpress we can get the full Ajax url as follows:

In PHP we can get it in the following way.

<?php echo admin_url('admin-ajax.php'); ?>

If you want to use it in Javascript then you can use it in following way.

<script type="text/javascript">
    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>


WordPress get user data

In WordPress there are many users with different roles. A user may have a administrator role, author or any other roles from the standard roles defined by WordPress.

If you want to find out user role or user information such as name,ID etc you can use the following function.

<?php
 $user_info = get_userdata(1);
          echo 'Username: ' . $user_info->user_login . "\n";
          echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
          echo 'User ID: ' . $user_info->ID . "\n";
?>

The above will give you user's username, role and ID.

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...