Sunday, May 14, 2017

How to get the taxonomy values of a custom post type

If we want to fetch the taxonomy term of a specific post then we could do that.


$terms = get_the_terms( $post->ID, 'publication_category' );
if ($terms) {
    foreach($terms as $term) {
      echo $term->name;
    }
}


in this function 
$post->ID :- Your post id. 
 'publication_category'  - its a taxonomy name.


Sunday, May 7, 2017

html 5 password validation

<input id="password" name="password" type="password" pattern="^\S{6,}$" 
onchange="this.setCustomValidity(this.validity.patternMismatch ? 
'Must have at least 6 characters' : ''); if(this.checkValidity()) 
form.password_two.pattern = this.value;" placeholder="Password" required>

<input id="password_two" name="password_two" type="password" 
pattern="^\S{6,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 
'Please enter the same Password as above' : '');" placeholder="Verify Password" required>

Monday, May 1, 2017

How can you check for a #hash in a URL using JavaScript?

if we are using ajax and on the response we want to know the url has # or not. so by this code we can find it it have the # on the url or not.


Method 1:-
if(window.location.hash) {
  // Fragment exists
} else {
  // Fragment doesn't exist
}


Method 2:-
if (location.href.indexOf("#") != -1) {
        // Your code in here accessing the string like this
        // location.href.substr(location.href.indexOf("#"))
    }


Method 3:-

var url = 'example.com/page.html#anchor',
    hash = url.split('#')[1];

if (hash) {
    alert(hash)
} else {
    // do something else
}

Method 4:-
  
var requestedHash = ((window.location.hash.substring(1).split("#",1))+"?").split("?",1);

Thursday, March 30, 2017

Get spreedsheet from the google doc in php

Github Url:- https://github.com/ankitgarg306/googledrive

<?php
require_once __DIR__ . '/vendor/autoload.php';


define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/sheets.googleapis.com-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_id.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-php-quickstart.json
define('SCOPES', implode(' ', array(
  Google_Service_Sheets::SPREADSHEETS_READONLY)
));



/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
  $client = new Google_Client();
  $client->setApplicationName(APPLICATION_NAME);
  $client->setScopes(SCOPES);
  $client->setAuthConfig(CLIENT_SECRET_PATH);
  $client->setAccessType('offline');

  // Load previously authorized credentials from a file.
  $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
  if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
  } else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim($accessToken);

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

    // Store the credentials to disk.
    if(!file_exists(dirname($credentialsPath))) {
      mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf("Credentials saved to %s\n", $credentialsPath);
  }
  $client->setAccessToken($accessToken);

  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  }
  return $client;
}

/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path) {
  $homeDirectory = getenv('HOME');
  if (empty($homeDirectory)) {
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
  }
  return str_replace('~', realpath($homeDirectory), $path);
}

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Sheets($client);

// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
$spreadsheetId = '159CL_jHoTR89dEEV20xfz_4EBcP0f4Exzqz1IlDCUsU';
$range = 'Sheet1!A:B';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();

if (count($values) == 0) {
  print "No data found.\n";
} else {
  print "Name, Major:\n";
  foreach ($values as $row) {
    // Print columns A and E, which correspond to indices 0 and 4.
    printf("%s, %s\n", $row[0], $row[1]);
  }
}
?>

Friday, March 24, 2017

Get Current User Role in SugarCRM /Suitcrm

global $current_user;
include_once(‘modules/ACLRoles/ACLRole.php’);
$roles = getUserRoleNames($current_user->id);
print_r($roles);