Thursday, 3 October 2019

How To Create Shortcodes In WordPress

We can create own shortcode by using its predified hooks
add_shortcode( 'hello-world', 'techsudhir_hello_world_shortcode' );

1. Write the Shortcode Function

Write a function with a unique name, which will execute the code you’d like the shortcode to trigger:
function techsudhir_hello_world_shortcode() {
   return 'Hello world!';
}
Example: [hello-world]
If we were to use this function normally, it would return Hello world! as a string



2. Shortcode function with parameters
function techsudhir_hello_world_shortcode( $atts ) {
   $a = shortcode_atts( array(
      'name' => 'world'
   ), $atts );
   return 'Hello ' . $a['name'] . !';
}
Example: [hello-world name="Sudhir"]


You can also call shortcode function in PHP using do_shortcode function
Example: do_shortcode('[hello-world]');

Friday, 31 May 2019

How to replace plain URLs with links

Here we will explain how to replace Urls with links from string

Using PHP

$string ='Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world. 
CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg  
CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be';


$string = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $string);







Using Javascript

<script>
function linkify(inputText) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    return replacedText;
}
var textTest = 'Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. He is a currently a professor of physics at Palomar College in San Marcos, CA. Additionally, he is the Founder & President of H.E.R. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world. CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg  CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be';
console.log(linkify(textTest));

</script>

Friday, 19 April 2019

Handling timezone conversion with PHP DateTime

Handling timezone conversion with PHP DateTime

Create function

function convert_to_server_datetime($date, $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC')
{
    try {
        $dateTime = new DateTime ($date, new DateTimeZone($userTimeZone));
        $dateTime->setTimezone(new DateTimeZone($serverTimeZone));
        return $dateTime->format("Y-m-d H:i:s");
    } catch (Exception $e) {
        return '';
    }
}

Example:

$userDate = '2019-04-19 13:20:00';

echo convert_to_server_datetime($userDate);

Other Method

public function convert($clienttimezone = null, $servertimezone = null){
$clientz=timezone_open("$clienttimezone");
$serverdateTime=date_create("now",timezone_open("$servertimezone"));
$offset1 = timezone_offset_get($clientz,$serverdateTime);
$servertz=timezone_open("$servertimezone");
$clientdateTime=date_create("now",timezone_open("$clienttimezone"));
$offset2 = timezone_offset_get($servertz,$clientdateTime);
if(($offset1 > 0 && $offset2 < 0) || ($offset1 < 0 && $offset2 < 0) || ($offset1 < 0 && $offset2 > 0)){
$offset = $offset1 -($offset2);
}else {
$offset = $offset1 +($offset2);
}
return $offset;
//return sprintf( "%s%02d:%02d", ( $offset >= 0 ) ? '+' : '-', abs( $offset / 3600 ), date("i",abs( $offset % 3600 )) );
}

Example
$clienttimezone = "Europe/Paris";
$servertimezone = date_default_timezone_get();

$timediff =  convert($clienttimezone,$servertimezone);
$transfer_date = '2019-04-20 20:04:05';

echo date('d M Y, h:i a',strtotime($transfer_date)+$timediff);

Tuesday, 18 September 2018

How to create jQuery fadeIn fadeOut Animation

How to create jQuery fadeIn fadeOut Animation

Html:
<ul>
<li class="client-testimonial"><img src="demo1.png"></li>
<li class="client-testimonial"><img src="demo2.png"></li>
<li class="client-testimonial"><img src="demo3.png"></li>
<li class="client-testimonial"><img src="demo4.png"></li>
</ul>


jQuery:

jQuery(document).ready(function(){
if(jQuery('.client-testimonial').length > 0){
var showImg = jQuery(".client-testimonial-img");
var quoteIndex = -1;

function showFadeInFadeOut() {
++quoteIndex;
showImg.eq(quoteIndex % showImg.length)
.fadeIn(2000)
.delay(2000)
.fadeOut(2000, showFadeInFadeOut);
}
showFadeInFadeOut();
}

});

Wednesday, 12 September 2018

How to get custom image size url in Wordpress

Here are simple way to get custom image from URL

$imgUrl = 'http://localhost/wptest/wp-content/themes/sydney/images/header.jpg';

 
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $imgUrl ));
$image_id = $attachment[0];
$thumbnail_url = wp_get_attachment_image_src($image_id, array('615','440'), true );

Monday, 3 September 2018

How to set varchar primary key field in Mysql

How to set varchar primary key field in Mysql

Table structure of Users table

CREATE TABLE `users` (
   `id` varchar(36) NOT NULL DEFAULT 'InitiallyEmpty',
   `first_name` varchar(100) NOT NULL,
   `last_name` varchar(100) NULL,
   `email` varchar(100) NOT NULL,
   `password` varchar(100) NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Want to fill id an automatically filled. You need to create trigger


Trigger structure for automatically update

DROP trigger if exists before_insert_users;
delimiter $$
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
    SET new.id = uuid();
END $$

delimiter ;

Now create insert query

insert into users (first_name,last_name,email,password) values ('Sudhir','Pandey','psudhir20@gmail.com','123465');

Thursday, 19 July 2018

How to Add Next Previous links to The Event Calendar

Add Next/Previous links to The Event Calendar Wordpress Plugin

Add code to your child theme’s functions.php file

/**
 * Allows visitors to page forward/backwards in any direction within month view
*/

if ( class_exists( 'Tribe__Events__Main' ) ) {

class ContinualMonthViewPagination {
    public function __construct() {
        add_filter( 'tribe_events_the_next_month_link', array( $this, 'next_month' ) );
        add_filter( 'tribe_events_the_previous_month_link', array( $this, 'previous_month' ) );
    }

    public function next_month() {
        $url = tribe_get_next_month_link();
        $text = tribe_get_next_month_text();
        $date = Tribe__Events__Main::instance()->nextMonth( tribe_get_month_view_date() );
        return '<a data-month="' . $date . '" href="' . $url . '" rel="next">' . $text . ' <span>&raquo;</span></a>';
    }

    public function previous_month() {
        $url = tribe_get_previous_month_link();
        $text = tribe_get_previous_month_text();
        $date = Tribe__Events__Main::instance()->previousMonth( tribe_get_month_view_date() );
        return '<a data-month="' . $date . '" href="' . $url . '" rel="prev"><span>&laquo;</span> ' . $text . ' </a>';
    }
}

new ContinualMonthViewPagination;

}